From 84098ed82445bdaebc6636800644a01c52644f60 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sun, 28 Jun 2020 09:48:07 +0200 Subject: [PATCH] [commands] Add a new exception class for command registration errors --- discord/ext/commands/core.py | 11 +++++++---- discord/ext/commands/errors.py | 24 +++++++++++++++++++++++- docs/ext/commands/api.rst | 5 +++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 7bfe61969..6af996221 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1080,6 +1080,9 @@ class GroupMixin: This is usually not called, instead the :meth:`~.GroupMixin.command` or :meth:`~.GroupMixin.group` shortcut decorators are used instead. + .. versionchanged:: 1.4 + Raise :exc:`.CommandRegistrationError` instead of generic :exc:`.ClientException` + Parameters ----------- command: :class:`Command` @@ -1087,8 +1090,8 @@ class GroupMixin: Raises ------- - :exc:`.ClientException` - If the command is already registered. + :exc:`.CommandRegistrationError` + If the command or its alias is already registered by different command. TypeError If the command passed is not a subclass of :class:`.Command`. """ @@ -1100,12 +1103,12 @@ class GroupMixin: command.parent = self if command.name in self.all_commands: - raise discord.ClientException('Command {0.name} is already registered.'.format(command)) + raise CommandRegistrationError(command.name) self.all_commands[command.name] = command for alias in command.aliases: if alias in self.all_commands: - raise discord.ClientException('The alias {} is already an existing command or alias.'.format(alias)) + raise CommandRegistrationError(alias, alias_conflict=True) self.all_commands[alias] = command def remove_command(self, name): diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py index 34b0b979e..a1b3b262b 100644 --- a/discord/ext/commands/errors.py +++ b/discord/ext/commands/errors.py @@ -24,7 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from discord.errors import DiscordException +from discord.errors import ClientException, DiscordException __all__ = ( @@ -62,6 +62,7 @@ __all__ = ( 'NoEntryPointError', 'ExtensionFailed', 'ExtensionNotFound', + 'CommandRegistrationError', ) class CommandError(DiscordException): @@ -583,3 +584,24 @@ class ExtensionNotFound(ExtensionError): self.original = None fmt = 'Extension {0!r} could not be loaded.' super().__init__(fmt.format(name), name=name) + +class CommandRegistrationError(ClientException): + """An exception raised when the command can't be added + because the name is already taken by a different command. + + This inherits from :exc:`discord.ClientException` + + .. versionadded:: 1.4 + + Attributes + ---------- + name: :class:`str` + The command name that had the error. + alias_conflict: :class:`bool` + Whether the name that conflicts is an alias of the command we try to add. + """ + def __init__(self, name, *, alias_conflict=False): + self.name = name + self.alias_conflict = alias_conflict + type_ = 'alias' if alias_conflict else 'command' + super().__init__('The {} {} is already an existing command or alias.'.format(type_, name)) diff --git a/docs/ext/commands/api.rst b/docs/ext/commands/api.rst index 61488c08f..d049e20b9 100644 --- a/docs/ext/commands/api.rst +++ b/docs/ext/commands/api.rst @@ -377,6 +377,9 @@ Exceptions .. autoexception:: discord.ext.commands.ExtensionNotFound :members: +.. autoexception:: discord.ext.commands.CommandRegistrationError + :members: + Exception Hierarchy +++++++++++++++++++++ @@ -418,3 +421,5 @@ Exception Hierarchy - :exc:`~.commands.NoEntryPointError` - :exc:`~.commands.ExtensionFailed` - :exc:`~.commands.ExtensionNotFound` + - :exc:`~.ClientException` + - :exc:`~.commands.CommandRegistrationError`