Skip to content

Commit f8ad5fd

Browse files
committed
Process snippets in get_contexts
1 parent 459b81d commit f8ad5fd

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

bot.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,16 @@ async def process_dm_modmail(self, message: discord.Message) -> None:
955955
await self.add_reaction(message, sent_emoji)
956956
self.dispatch("thread_reply", thread, False, message, False, False)
957957

958+
def _get_snippet_command(self) -> commands.Command:
959+
"""Get the correct reply command based on the snippet config"""
960+
modifiers = "f"
961+
if self.config["plain_snippets"]:
962+
modifiers += "p"
963+
if self.config["anonymous_snippets"]:
964+
modifiers += "a"
965+
966+
return self.get_command(f'{modifiers}reply')
967+
958968
async def get_contexts(self, message, *, cls=commands.Context):
959969
"""
960970
Returns all invocation contexts from the message.
@@ -986,18 +996,40 @@ async def get_contexts(self, message, *, cls=commands.Context):
986996
self.aliases.pop(invoker)
987997

988998
for alias in aliases:
989-
view = StringView(invoked_prefix + alias)
999+
command = None
1000+
try:
1001+
snippet_text = self.snippets[alias]
1002+
except KeyError:
1003+
command_invocation_text = alias
1004+
else:
1005+
command = self._get_snippet_command()
1006+
command_invocation_text = f'{invoked_prefix}{command} {snippet_text}'
1007+
1008+
view = StringView(invoked_prefix + command_invocation_text)
9901009
ctx_ = cls(prefix=self.prefix, view=view, bot=self, message=message)
9911010
ctx_.thread = thread
9921011
discord.utils.find(view.skip_string, prefixes)
9931012
ctx_.invoked_with = view.get_word().lower()
994-
ctx_.command = self.all_commands.get(ctx_.invoked_with)
1013+
ctx_.command = command or self.all_commands.get(ctx_.invoked_with)
9951014
ctxs += [ctx_]
9961015
return ctxs
9971016

9981017
ctx.thread = thread
999-
ctx.invoked_with = invoker
1000-
ctx.command = self.all_commands.get(invoker)
1018+
1019+
try:
1020+
snippet_text = self.snippets[invoker]
1021+
except KeyError:
1022+
# Process regular commands
1023+
ctx.command = self.all_commands.get(invoker)
1024+
ctx.invoked_with = invoker
1025+
else:
1026+
# Process snippets
1027+
ctx.command = self._get_snippet_command()
1028+
reply_view = StringView(f'{invoked_prefix}{ctx.command} {snippet_text}')
1029+
discord.utils.find(reply_view.skip_string, prefixes)
1030+
ctx.invoked_with = reply_view.get_word().lower()
1031+
ctx.view = reply_view
1032+
10011033
return [ctx]
10021034

10031035
async def trigger_auto_triggers(self, message, channel, *, cls=commands.Context):
@@ -1139,20 +1171,6 @@ async def process_commands(self, message):
11391171
if isinstance(message.channel, discord.DMChannel):
11401172
return await self.process_dm_modmail(message)
11411173

1142-
if message.content.startswith(self.prefix):
1143-
cmd = message.content[len(self.prefix) :].strip()
1144-
1145-
# Process snippets
1146-
cmd = cmd.lower()
1147-
if cmd in self.snippets:
1148-
snippet = self.snippets[cmd]
1149-
modifiers = "f"
1150-
if self.config["plain_snippets"]:
1151-
modifiers += "p"
1152-
if self.config["anonymous_snippets"]:
1153-
modifiers += "a"
1154-
message.content = f"{self.prefix}{modifiers}reply {snippet}"
1155-
11561174
ctxs = await self.get_contexts(message)
11571175
for ctx in ctxs:
11581176
if ctx.command:

0 commit comments

Comments
 (0)