From bd19ad05e73d102d1c0bfe8b961bd93689b4dc47 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 16 Aug 2022 19:57:44 -0400 Subject: [PATCH] Fix app_command_completion triggering on certain errors --- discord/app_commands/tree.py | 6 +++++- discord/ext/commands/hybrid.py | 1 + discord/interactions.py | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/discord/app_commands/tree.py b/discord/app_commands/tree.py index 4265e474e..6a38b4894 100644 --- a/discord/app_commands/tree.py +++ b/discord/app_commands/tree.py @@ -1077,6 +1077,7 @@ class CommandTree(Generic[ClientT]): async def _dispatch_error(self, interaction: Interaction, error: AppCommandError, /) -> None: command = interaction.command + interaction.command_failed = True if isinstance(command, Command): await command._invoke_error_handlers(interaction, error) else: @@ -1205,6 +1206,7 @@ class CommandTree(Generic[ClientT]): async def _call(self, interaction: Interaction) -> None: if not await self.interaction_check(interaction): + interaction.command_failed = True return data: ApplicationCommandInteractionData = interaction.data # type: ignore @@ -1237,7 +1239,9 @@ class CommandTree(Generic[ClientT]): try: await command._invoke_with_namespace(interaction, namespace) except AppCommandError as e: + interaction.command_failed = True await command._invoke_error_handlers(interaction, e) await self.on_error(interaction, e) else: - self.client.dispatch('app_command_completion', interaction, command) + if not interaction.command_failed: + self.client.dispatch('app_command_completion', interaction, command) diff --git a/discord/ext/commands/hybrid.py b/discord/ext/commands/hybrid.py index 7b0432a95..d7eb05230 100644 --- a/discord/ext/commands/hybrid.py +++ b/discord/ext/commands/hybrid.py @@ -462,6 +462,7 @@ class HybridAppCommand(discord.app_commands.Command[CogT, P, T]): if not ctx.command_failed: bot.dispatch('command_completion', ctx) + interaction.command_failed = ctx.command_failed return value diff --git a/discord/interactions.py b/discord/interactions.py index 58e1ec4d8..a2f459849 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -116,6 +116,9 @@ class Interaction: A dictionary that can be used to store extraneous data for use during interaction processing. The library will not touch any values or keys within this dictionary. + command_failed: :class:`bool` + Whether the command associated with this interaction failed to execute. + This includes checks and execution. """ __slots__: Tuple[str, ...] = ( @@ -132,6 +135,7 @@ class Interaction: 'locale', 'guild_locale', 'extras', + 'command_failed', '_permissions', '_app_permissions', '_state', @@ -155,6 +159,7 @@ class Interaction: # an interaction. This is mainly for internal purposes and it gives it a free-for-all slot. self._baton: Any = MISSING self.extras: Dict[Any, Any] = {} + self.command_failed: bool = False self._from_data(data) def _from_data(self, data: InteractionPayload):