mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-10-24 10:02:56 +00:00
[commands] Add cog-level app command error special method
This commit is contained in:
@@ -598,7 +598,7 @@ class Command(Generic[GroupT, P, T]):
|
||||
|
||||
return base
|
||||
|
||||
async def _invoke_error_handler(self, interaction: Interaction, error: AppCommandError) -> None:
|
||||
async def _invoke_error_handlers(self, interaction: Interaction, error: AppCommandError) -> None:
|
||||
# These type ignores are because the type checker can't narrow this type properly.
|
||||
if self.on_error is not None:
|
||||
if self.binding is not None:
|
||||
@@ -613,6 +613,10 @@ class Command(Generic[GroupT, P, T]):
|
||||
if parent.parent is not None:
|
||||
await parent.parent.on_error(interaction, error)
|
||||
|
||||
cog_error = getattr(self.binding, '__app_commands_error_handler__', None)
|
||||
if cog_error is not None:
|
||||
await cog_error(interaction, error)
|
||||
|
||||
def _has_any_error_handlers(self) -> bool:
|
||||
if self.on_error is not None:
|
||||
return True
|
||||
|
||||
@@ -992,12 +992,19 @@ class CommandTree(Generic[ClientT]):
|
||||
|
||||
return [AppCommand(data=d, state=self._state) for d in data]
|
||||
|
||||
def _from_interaction(self, interaction: Interaction):
|
||||
async def _dispatch_error(self, interaction: Interaction, error: AppCommandError, /) -> None:
|
||||
command = interaction.command
|
||||
if isinstance(command, Command):
|
||||
await command._invoke_error_handlers(interaction, error)
|
||||
else:
|
||||
await self.on_error(interaction, error)
|
||||
|
||||
def _from_interaction(self, interaction: Interaction) -> None:
|
||||
async def wrapper():
|
||||
try:
|
||||
await self.call(interaction)
|
||||
except AppCommandError as e:
|
||||
await self.on_error(interaction, e)
|
||||
await self._dispatch_error(interaction, e)
|
||||
|
||||
self.client.loop.create_task(wrapper(), name='CommandTree-invoker')
|
||||
|
||||
@@ -1167,5 +1174,5 @@ class CommandTree(Generic[ClientT]):
|
||||
try:
|
||||
await command._invoke_with_namespace(interaction, namespace)
|
||||
except AppCommandError as e:
|
||||
await command._invoke_error_handler(interaction, e)
|
||||
await command._invoke_error_handlers(interaction, e)
|
||||
await self.on_error(interaction, e)
|
||||
|
||||
Reference in New Issue
Block a user