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: 8 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,20 +1013,20 @@ async def trigger_auto_triggers(self, message, channel, *, cls=commands.Context)
alias = self.auto_triggers[trigger]

ctxs = []

if alias is not None:
ctxs = []
aliases = normalize_alias(alias)
if not aliases:
logger.warning("Alias %s is invalid as called in autotrigger.", invoker)

for alias in aliases:
view = StringView(invoked_prefix + alias)
ctx_ = cls(prefix=self.prefix, view=view, bot=self, message=message)
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_]
message.author = thread.recipient # Allow for get_contexts to work

for alias in aliases:
message.content = invoked_prefix + alias
ctxs += await self.get_contexts(message)

message.author = self.modmail_guild.me # Fix message so commands execute properly

for ctx in ctxs:
if ctx.command:
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