Skip to content

Commit a97119d

Browse files
committed
Added command validation to autotrigger
1 parent 63461bb commit a97119d

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ however, insignificant breaking changes do not guarantee a major version bump, s
88

99
# v3.7.14-dev0
1010

11-
### Fixed
11+
### Added
1212

13-
- Mentioned `competing` as an activity type ([PR #2902](https://github.com/kyb3r/modmail/pull/2902))
13+
- `update_notifications` configuration option to toggle bot autoupdate notifications. ([GH #2896](https://github.com/kyb3r/modmail/issues/2896))
1414

15-
### Added
15+
### Improved
16+
17+
- Added command validation to `autotrigger add/edit`.
18+
19+
### Fixed
1620

17-
- `update_notifications` configuration option to toggle bot autoupdate notifications ([GH #2896](https://github.com/kyb3r/modmail/issues/2896))
21+
- Mentioned `competing` as an activity type. ([PR #2902](https://github.com/kyb3r/modmail/pull/2902))
1822

1923
# v3.7.13
2024

bot.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,6 @@ async def trigger_auto_triggers(self, message, channel, *, cls=commands.Context)
937937
invoked_prefix = self.prefix
938938
invoker = None
939939

940-
# Check if there is any aliases being called.
941940
if self.config.get("use_regex_autotrigger"):
942941
trigger = next(
943942
filter(lambda x: re.match(x, message.content), self.auto_triggers.keys())
@@ -1528,7 +1527,9 @@ async def autoupdate(self):
15281527
channel = self.update_channel
15291528
if self.hosting_method == HostingMethod.PM2:
15301529
embed = discord.Embed(title="Bot has been updated", color=self.main_color)
1531-
embed.set_footer(text=f"Updating Modmail v{self.version} " f"-> v{latest.version}")
1530+
embed.set_footer(
1531+
text=f"Updating Modmail v{self.version} " f"-> v{latest.version}"
1532+
)
15321533
if self.bot.config["update_notifications"]:
15331534
await channel.send(embed=embed)
15341535
else:
@@ -1537,7 +1538,9 @@ async def autoupdate(self):
15371538
description="If you do not have an auto-restart setup, please manually start the bot.",
15381539
color=self.main_color,
15391540
)
1540-
embed.set_footer(text=f"Updating Modmail v{self.version} " f"-> v{latest.version}")
1541+
embed.set_footer(
1542+
text=f"Updating Modmail v{self.version} " f"-> v{latest.version}"
1543+
)
15411544
if self.bot.config["update_notifications"]:
15421545
await channel.send(embed=embed)
15431546
await self.logout()

cogs/utility.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,14 +1727,30 @@ async def autotrigger_add(self, ctx, keyword, *, command):
17271727
description=f"Another autotrigger with the same name already exists: `{keyword}`.",
17281728
)
17291729
else:
1730-
self.bot.auto_triggers[keyword] = command
1731-
await self.bot.config.update()
1730+
# command validation
1731+
valid = False
1732+
split_cmd = command.split(" ")
1733+
for n in range(1, len(split_cmd) + 1):
1734+
if self.bot.get_command(" ".join(split_cmd[0:n])):
1735+
print(self.bot.get_command(" ".join(split_cmd[0:n])))
1736+
valid = True
1737+
break
1738+
1739+
if valid:
1740+
self.bot.auto_triggers[keyword] = command
1741+
await self.bot.config.update()
17321742

1733-
embed = discord.Embed(
1734-
title="Success",
1735-
color=self.bot.main_color,
1736-
description=f"Keyword `{keyword}` has been linked to `{command}`.",
1737-
)
1743+
embed = discord.Embed(
1744+
title="Success",
1745+
color=self.bot.main_color,
1746+
description=f"Keyword `{keyword}` has been linked to `{command}`.",
1747+
)
1748+
else:
1749+
embed = discord.Embed(
1750+
title="Error",
1751+
color=self.bot.error_color,
1752+
description="Invalid command. Note that autotriggers do not work with aliases.",
1753+
)
17381754

17391755
await ctx.send(embed=embed)
17401756

@@ -1747,14 +1763,30 @@ async def autotrigger_edit(self, ctx, keyword, *, command):
17471763
keyword, self.bot.auto_triggers.keys(), "Autotrigger"
17481764
)
17491765
else:
1750-
self.bot.auto_triggers[keyword] = command
1751-
await self.bot.config.update()
1766+
# command validation
1767+
valid = False
1768+
split_cmd = command.split(" ")
1769+
for n in range(1, len(split_cmd) + 1):
1770+
if self.bot.get_command(" ".join(split_cmd[0:n])):
1771+
print(self.bot.get_command(" ".join(split_cmd[0:n])))
1772+
valid = True
1773+
break
1774+
1775+
if valid:
1776+
self.bot.auto_triggers[keyword] = command
1777+
await self.bot.config.update()
17521778

1753-
embed = discord.Embed(
1754-
title="Success",
1755-
color=self.bot.main_color,
1756-
description=f"Keyword `{keyword}` has been linked to `{command}`.",
1757-
)
1779+
embed = discord.Embed(
1780+
title="Success",
1781+
color=self.bot.main_color,
1782+
description=f"Keyword `{keyword}` has been linked to `{command}`.",
1783+
)
1784+
else:
1785+
embed = discord.Embed(
1786+
title="Error",
1787+
color=self.bot.error_color,
1788+
description="Invalid command. Note that autotriggers do not work with aliases.",
1789+
)
17581790

17591791
await ctx.send(embed=embed)
17601792

0 commit comments

Comments
 (0)