mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-06 01:51:59 +00:00
Add error decorator to app_commands.Group
Co-authored-by: Danny <Rapptz@users.noreply.github.com>
This commit is contained in:
parent
5e98626d41
commit
5dd828cdac
@ -70,6 +70,8 @@ if TYPE_CHECKING:
|
|||||||
# reference the other to prevent type checking errors in callbacks
|
# reference the other to prevent type checking errors in callbacks
|
||||||
from discord.ext.commands import Cog
|
from discord.ext.commands import Cog
|
||||||
|
|
||||||
|
ErrorFunc = Callable[[Interaction, AppCommandError], Coroutine[Any, Any, None]]
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Command',
|
'Command',
|
||||||
'ContextMenu',
|
'ContextMenu',
|
||||||
@ -1283,6 +1285,35 @@ class Group:
|
|||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def error(self, coro: ErrorFunc) -> ErrorFunc:
|
||||||
|
"""A decorator that registers a coroutine as a local error handler.
|
||||||
|
|
||||||
|
The local error handler is called whenever an exception is raised in a child 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, or is an invalid coroutine.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not inspect.iscoroutinefunction(coro):
|
||||||
|
raise TypeError('The error handler must be a coroutine.')
|
||||||
|
|
||||||
|
params = inspect.signature(coro).parameters
|
||||||
|
if len(params) != 2:
|
||||||
|
raise TypeError('The error handler must have 2 parameters.')
|
||||||
|
|
||||||
|
self.on_error = coro # type: ignore
|
||||||
|
return coro
|
||||||
|
|
||||||
async def interaction_check(self, interaction: Interaction) -> bool:
|
async def interaction_check(self, interaction: Interaction) -> bool:
|
||||||
"""|coro|
|
"""|coro|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user