Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-trigger alias support #3081

Merged
merged 13 commits into from
Aug 21, 2021
16 changes: 14 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,11 +1021,23 @@ async def trigger_auto_triggers(self, message, channel, *, cls=commands.Context)

for alias in aliases:
view = StringView(invoked_prefix + alias)
invoked_with = view.get_word().lower()[1:]
found_command = self.all_commands.get(invoked_with)

# Check for alias
if not found_command:
invoked_with = self.aliases.get(invoked_with)[1:-1] # Get command linked to alias
view = StringView(invoked_prefix + invoked_with) # Create StringView for new command
invoked_with = view.get_word().lower()[1:] # Parse the new command
found_command = self.all_commands.get(invoked_with) # Get the command function

ctx_ = cls(prefix=self.prefix, view=view, bot=self, message=message)
ctx_.command = found_command

ctx_.invoked_with = invoked_with
ctx_.thread = thread
discord.utils.find(view.skip_string, await self.get_prefix())
ctx_.invoked_with = view.get_word().lower()
ctx_.command = self.all_commands.get(ctx_.invoked_with)

ctxs += [ctx_]

for ctx in ctxs:
Expand Down
2 changes: 2 additions & 0 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,9 @@ async def reply(self, ctx, *, msg: str = ""):
Supports attachments and images as well as
automatically embedding image URLs.
"""

ctx.message.content = msg

async with ctx.typing():
await ctx.thread.reply(ctx.message)

Expand Down
19 changes: 17 additions & 2 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,13 @@ async def autotrigger_add(self, ctx, keyword, *, command):
valid = True
break

if not valid and self.bot.aliases:
for n in range(1, len(split_cmd) + 1):
if self.bot.aliases.get(" ".join(split_cmd[0:n])):
print(self.bot.aliases.get(" ".join(split_cmd[0:n])))
valid = True
break

if valid:
self.bot.auto_triggers[keyword] = command
await self.bot.config.update()
Expand All @@ -1764,7 +1771,7 @@ async def autotrigger_add(self, ctx, keyword, *, command):
embed = discord.Embed(
title="Error",
color=self.bot.error_color,
description="Invalid command. Note that autotriggers do not work with aliases.",
description="Invalid command. Please provide a valid command or alias.",
)

await ctx.send(embed=embed)
Expand All @@ -1784,6 +1791,14 @@ async def autotrigger_edit(self, ctx, keyword, *, command):
valid = True
break

if not valid and self.bot.aliases:
for n in range(1, len(split_cmd) + 1):
print(" ".join(split_cmd[0:n]), self.bot.aliases.get(" ".join(split_cmd[0:n])))
if self.bot.aliases.get(" ".join(split_cmd[0:n])):
print(self.bot.aliases.get(" ".join(split_cmd[0:n])))
valid = True
break

if valid:
self.bot.auto_triggers[keyword] = command
await self.bot.config.update()
Expand All @@ -1797,7 +1812,7 @@ async def autotrigger_edit(self, ctx, keyword, *, command):
embed = discord.Embed(
title="Error",
color=self.bot.error_color,
description="Invalid command. Note that autotriggers do not work with aliases.",
description="Invalid command. Please provide a valid command or alias.",
)

await ctx.send(embed=embed)
Expand Down
1 change: 1 addition & 0 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class Author:
async def activate_auto_triggers():
if initial_message:
message = DummyMessage(copy.copy(initial_message))

try:
return await self.bot.trigger_auto_triggers(message, channel)
except RuntimeError:
Expand Down
29 changes: 28 additions & 1 deletion plugins/registry.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
{
"close_message": {
"repository": "python-discord/modmail-plugins",
"branch": "main",
"description": "Add a ?closemessage command that will close the thread after 15 minutes with a default message.",
"bot_version": "2.20.1",
"title": "Close message",
"icon_url": "https://i.imgur.com/ev7BFMz.png",
"thumbnail_url": "https://i.imgur.com/ev7BFMz.png"
},
"mdlink": {
"repository": "python-discord/modmail-plugins",
"branch": "main",
"description": "Generate a ready to paste link to the thread logs.",
"bot_version": "2.20.1",
"title": "MDLink",
"icon_url": "https://i.imgur.com/JA2E63R.png",
"thumbnail_url": "https://i.imgur.com/JA2E63R.png"
},
"reply_cooldown": {
"repository": "python-discord/modmail-plugins",
"branch": "main",
"description": "Forbid you from sending the same message twice in ten seconds.",
"bot_version": "2.20.1",
"title": "Reply cooldown",
"icon_url": "https://i.imgur.com/FtRQveT.png",
"thumbnail_url": "https://i.imgur.com/FtRQveT.png"
},
"dragory-migrate": {
"repository": "kyb3r/modmail-plugins",
"branch": "master",
Expand Down Expand Up @@ -154,7 +181,7 @@
"suggest": {
"repository": "realcyguy/modmail-plugins",
"branch": "master",
"description": "Send suggestions to a selected server! It even has moderation...",
"description": "Send suggestions to a selected server! It has accepting, denying, and moderation-ing.",
"bot_version": "3.4.1",
"title": "Suggest stuff.",
"icon_url": "https://i.imgur.com/qtE7AH8.png",
Expand Down