Add some basic error handling for registration

This commit is contained in:
Gnome 2021-09-02 13:07:07 +01:00
parent 41e3d13eaf
commit fe780a04a2
3 changed files with 25 additions and 6 deletions

View File

@ -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

View File

@ -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,

View File

@ -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.