mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 20:28:38 +00:00
Add error handler support for context menus
This commit is contained in:
parent
97fe07edb2
commit
f7c664e3e2
@ -88,9 +88,10 @@ else:
|
|||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
GroupT = TypeVar('GroupT', bound='Union[Group, Cog]')
|
GroupT = TypeVar('GroupT', bound='Union[Group, Cog]')
|
||||||
Coro = Coroutine[Any, Any, T]
|
Coro = Coroutine[Any, Any, T]
|
||||||
|
UnboundError = Callable[['Interaction', AppCommandError], Coro[Any]]
|
||||||
Error = Union[
|
Error = Union[
|
||||||
Callable[[GroupT, 'Interaction', AppCommandError], Coro[Any]],
|
Callable[[GroupT, 'Interaction', AppCommandError], Coro[Any]],
|
||||||
Callable[['Interaction', AppCommandError], Coro[Any]],
|
UnboundError,
|
||||||
]
|
]
|
||||||
Check = Callable[['Interaction'], Union[bool, Coro[bool]]]
|
Check = Callable[['Interaction'], Union[bool, Coro[bool]]]
|
||||||
|
|
||||||
@ -727,6 +728,7 @@ class ContextMenu:
|
|||||||
self._annotation = annotation
|
self._annotation = annotation
|
||||||
self.module: Optional[str] = callback.__module__
|
self.module: Optional[str] = callback.__module__
|
||||||
self._guild_ids = guild_ids
|
self._guild_ids = guild_ids
|
||||||
|
self.on_error: Optional[UnboundError] = None
|
||||||
self.checks: List[Check] = getattr(callback, '__discord_app_commands_checks__', [])
|
self.checks: List[Check] = getattr(callback, '__discord_app_commands_checks__', [])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -746,6 +748,7 @@ class ContextMenu:
|
|||||||
self._annotation = annotation
|
self._annotation = annotation
|
||||||
self.module = callback.__module__
|
self.module = callback.__module__
|
||||||
self._guild_ids = None
|
self._guild_ids = None
|
||||||
|
self.on_error = None
|
||||||
self.checks = getattr(callback, '__discord_app_commands_checks__', [])
|
self.checks = getattr(callback, '__discord_app_commands_checks__', [])
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -774,6 +777,32 @@ class ContextMenu:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise CommandInvokeError(self, e) from e
|
raise CommandInvokeError(self, e) from e
|
||||||
|
|
||||||
|
def error(self, coro: UnboundError) -> UnboundError:
|
||||||
|
"""A decorator that registers a coroutine as a local error handler.
|
||||||
|
|
||||||
|
The local error handler is called whenever an exception is raised in the body
|
||||||
|
of the command or during handling of the command. The error handler must take
|
||||||
|
2 parameters, the interaction and the error.
|
||||||
|
|
||||||
|
The error passed will be derived from :exc:`AppCommandError`.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
-----------
|
||||||
|
coro: :ref:`coroutine <coroutine>`
|
||||||
|
The coroutine to register as the local error handler.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
-------
|
||||||
|
TypeError
|
||||||
|
The coroutine passed is not actually a coroutine.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not inspect.iscoroutinefunction(coro):
|
||||||
|
raise TypeError('The error handler must be a coroutine.')
|
||||||
|
|
||||||
|
self.on_error = coro
|
||||||
|
return coro
|
||||||
|
|
||||||
def add_check(self, func: Check, /) -> None:
|
def add_check(self, func: Check, /) -> None:
|
||||||
"""Adds a check to the command.
|
"""Adds a check to the command.
|
||||||
|
|
||||||
|
@ -963,6 +963,8 @@ class CommandTree(Generic[ClientT]):
|
|||||||
try:
|
try:
|
||||||
await ctx_menu._invoke(interaction, value)
|
await ctx_menu._invoke(interaction, value)
|
||||||
except AppCommandError as e:
|
except AppCommandError as e:
|
||||||
|
if ctx_menu.on_error is not None:
|
||||||
|
await ctx_menu.on_error(interaction, e)
|
||||||
await self.on_error(interaction, ctx_menu, e)
|
await self.on_error(interaction, ctx_menu, e)
|
||||||
|
|
||||||
async def call(self, interaction: Interaction) -> None:
|
async def call(self, interaction: Interaction) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user