From fe780a04a230c006136a6a84956e847dca4f630e Mon Sep 17 00:00:00 2001 From: Gnome Date: Thu, 2 Sep 2021 13:07:07 +0100 Subject: [PATCH] Add some basic error handling for registration --- discord/ext/commands/bot.py | 2 -- discord/ext/commands/core.py | 10 ++++++---- discord/ext/commands/errors.py | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index e1a553e9..885fa3b3 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -1130,8 +1130,6 @@ class BotBase(GroupMixin): command = self.get_command(command_name) if command is None: raise errors.CommandNotFound(f'Command "{command_name}" is not found') - elif not command.slash_command: - return # Ensure the interaction channel is usable channel = interaction.channel diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index ea94a9b5..b51d67d2 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1211,10 +1211,12 @@ class Command(_BaseCommand, Generic[CogT, P, T]): annotation, origin = annotation.__args__[0], None if origin is None: - option["type"] = next( - (num for t, num in application_option_type_lookup.items() - if issubclass(annotation, t)), 3 - ) + generator = (num for t, num in application_option_type_lookup.items() if issubclass(annotation, t)) + try: + option["type"] = next(generator, 3) + except Exception as err: + raise ApplicationCommandRegistrationError(self) + elif origin is Literal and len(origin.__args__) <= 25: # type: ignore option["choices"] = [{ "name": literal_value, diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py index 93834385..01dbaee2 100644 --- a/discord/ext/commands/errors.py +++ b/discord/ext/commands/errors.py @@ -33,6 +33,7 @@ if TYPE_CHECKING: from .converter import Converter from .context import Context + from .core import Command from .cooldowns import Cooldown, BucketType from .flags import Flag from discord.abc import GuildChannel @@ -93,6 +94,7 @@ __all__ = ( 'ExtensionFailed', 'ExtensionNotFound', 'CommandRegistrationError', + 'ApplicationCommandRegistrationError', 'FlagError', 'BadFlagArgument', 'MissingFlagArgument', @@ -915,6 +917,23 @@ class CommandRegistrationError(ClientException): type_ = 'alias' if alias_conflict else 'command' super().__init__(f'The {type_} {name} is already an existing command or alias.') +class ApplicationCommandRegistrationError(ClientException): + """An exception raised when a command cannot be converted to an + application command. + + This inherits from :exc:`discord.ClientException` + + .. versionadded:: 2.0 + + Attributes + ---------- + command: :class:`Command` + The command that failed to be converted. + """ + def __init__(self, command: Command) -> None: + self.command = command + super().__init__(f"{command.qualified_name} failed to converted to an application command.") + class FlagError(BadArgument): """The base exception type for all flag parsing related errors.