[commands] Add cog-level app command error special method

This commit is contained in:
Stocker
2022-07-17 23:45:19 -04:00
committed by GitHub
parent 2067819b75
commit 0546343bcb
3 changed files with 57 additions and 5 deletions

View File

@ -28,7 +28,21 @@ import discord
from discord import app_commands
from discord.utils import maybe_coroutine
from typing import Any, Callable, ClassVar, Dict, Generator, Iterable, List, Optional, TYPE_CHECKING, Tuple, TypeVar, Union
from typing import (
Any,
Callable,
ClassVar,
Coroutine,
Dict,
Generator,
Iterable,
List,
Optional,
TYPE_CHECKING,
Tuple,
TypeVar,
Union,
)
from ._types import _BaseCommand, BotT
@ -254,6 +268,9 @@ class Cog(metaclass=CogMeta):
__cog_listeners__: List[Tuple[str, str]]
__cog_is_app_commands_group__: ClassVar[bool] = False
__cog_app_commands_group__: Optional[app_commands.Group]
__app_commands_error_handler__: Optional[
Callable[[discord.Interaction, app_commands.AppCommandError], Coroutine[Any, Any, None]]
]
def __new__(cls, *args: Any, **kwargs: Any) -> Self:
# For issue 426, we need to store a copy of the command objects
@ -329,6 +346,11 @@ class Cog(metaclass=CogMeta):
self.__cog_app_commands_group__._children = mapping # type: ignore # Variance issue
if Cog._get_overridden_method(self.cog_app_command_error) is not None:
self.__app_commands_error_handler__ = self.cog_app_command_error
else:
self.__app_commands_error_handler__ = None
return self
def get_commands(self) -> List[Command[Self, ..., Any]]:
@ -524,6 +546,25 @@ class Cog(metaclass=CogMeta):
"""
pass
@_cog_special_method
async def cog_app_command_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError) -> None:
"""A special method that is called whenever an error within
an application command is dispatched inside this cog.
This is similar to :func:`discord.app_commands.CommandTree.on_error` except
only applying to the application commands inside this cog.
This **must** be a coroutine.
Parameters
-----------
interaction: :class:`~discord.Interaction`
The interaction that is being handled.
error: :exc:`~discord.app_commands.AppCommandError`
The exception that was raised.
"""
pass
@_cog_special_method
async def cog_before_invoke(self, ctx: Context[BotT]) -> None:
"""A special method that acts as a cog local pre-invoke hook.