Skip to content

Commit 51d110b

Browse files
authored
Ability to reload application command callbacks (#647)
* Set command.id when reloaded * Process reloaded commands
1 parent 22a99ee commit 51d110b

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

discord/bot.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ def add_application_command(self, command: ApplicationCommand) -> None:
124124

125125
if self.debug_guilds and command.guild_ids is None:
126126
command.guild_ids = self.debug_guilds
127-
self._pending_application_commands.append(command)
127+
128+
for cmd in self.pending_application_commands:
129+
if cmd == command:
130+
command.id = cmd.id
131+
self._application_commands[command.id] = command
132+
break
133+
else:
134+
self._pending_application_commands.append(command)
128135

129136
def remove_application_command(
130137
self, command: ApplicationCommand
@@ -483,25 +490,32 @@ async def process_application_commands(self, interaction: Interaction) -> None:
483490
try:
484491
command = self._application_commands[interaction.data["id"]]
485492
except KeyError:
486-
self.dispatch("unknown_command", interaction)
487-
else:
488-
if interaction.type is InteractionType.auto_complete:
489-
ctx = await self.get_autocomplete_context(interaction)
490-
ctx.command = command
491-
return await command.invoke_autocomplete_callback(ctx)
492-
493-
ctx = await self.get_application_context(interaction)
493+
for cmd in self.application_commands:
494+
if (
495+
cmd.name == interaction.data["name"]
496+
and interaction.data["guild_id"] in cmd.guild_ids
497+
):
498+
command = cmd
499+
break
500+
else:
501+
return self.dispatch("unknown_command", interaction)
502+
if interaction.type is InteractionType.auto_complete:
503+
ctx = await self.get_autocomplete_context(interaction)
494504
ctx.command = command
495-
self.dispatch("application_command", ctx)
496-
try:
497-
if await self.can_run(ctx, call_once=True):
498-
await ctx.command.invoke(ctx)
499-
else:
500-
raise CheckFailure("The global check once functions failed.")
501-
except DiscordException as exc:
502-
await ctx.command.dispatch_error(ctx, exc)
505+
return await command.invoke_autocomplete_callback(ctx)
506+
507+
ctx = await self.get_application_context(interaction)
508+
ctx.command = command
509+
self.dispatch("application_command", ctx)
510+
try:
511+
if await self.can_run(ctx, call_once=True):
512+
await ctx.command.invoke(ctx)
503513
else:
504-
self.dispatch("application_command_completion", ctx)
514+
raise CheckFailure("The global check once functions failed.")
515+
except DiscordException as exc:
516+
await ctx.command.dispatch_error(ctx, exc)
517+
else:
518+
self.dispatch("application_command_completion", ctx)
505519

506520
def slash_command(self, **kwargs):
507521
"""A shortcut decorator that invokes :func:`.ApplicationCommandMixin.command` and adds it to

0 commit comments

Comments
 (0)