From 83483abfa599aaad0d39a4ef45a23c3bb446adcf Mon Sep 17 00:00:00 2001 From: Michael H Date: Sat, 28 Sep 2019 16:58:40 -0400 Subject: [PATCH] Reserves command names (#2977) * Reserves command names - Currently, only reserving ``cancel`` - This should only impact matching command qualified names - This also checks aliases - This makes cc and alias use the new module constant with info about this - Module constant is available for use by 3rd party cogs which may dynamically create responses. * Change misleading var name * style * Thanks Flame! * Handles issues with CC --- changelog.d/2973.breaking.rst | 1 + redbot/cogs/alias/alias.py | 6 +++++- redbot/cogs/customcom/customcom.py | 5 ++++- redbot/core/commands/commands.py | 12 ++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2973.breaking.rst diff --git a/changelog.d/2973.breaking.rst b/changelog.d/2973.breaking.rst new file mode 100644 index 000000000..8a36fcc1f --- /dev/null +++ b/changelog.d/2973.breaking.rst @@ -0,0 +1 @@ +Reserves some command names for internal Red use. These are available programatically as ``redbot.core.commands.RESERVED_COMMAND_NAMES`` \ No newline at end of file diff --git a/redbot/cogs/alias/alias.py b/redbot/cogs/alias/alias.py index 19042e539..d695c7bd0 100644 --- a/redbot/cogs/alias/alias.py +++ b/redbot/cogs/alias/alias.py @@ -89,8 +89,12 @@ class Alias(commands.Cog): return False, None def is_command(self, alias_name: str) -> bool: + """ + The logic here is that if this returns true, the name shouldnt be used for an alias + The function name can be changed when alias is reworked + """ command = self.bot.get_command(alias_name) - return command is not None + return command is not None or alias_name in commands.RESERVED_COMMAND_NAMES @staticmethod def is_valid_alias_name(alias_name: str) -> bool: diff --git a/redbot/cogs/customcom/customcom.py b/redbot/cogs/customcom/customcom.py index 355bbeb4c..3bfa88bba 100644 --- a/redbot/cogs/customcom/customcom.py +++ b/redbot/cogs/customcom/customcom.py @@ -222,6 +222,9 @@ class CustomCommands(commands.Cog): Note: This command is interactive. """ + if command in (*self.bot.all_commands, *commands.RESERVED_COMMAND_NAMES): + await ctx.send(_("There already exists a bot command with the same name.")) + return responses = await self.commandobj.get_responses(ctx=ctx) try: await self.commandobj.create(ctx=ctx, command=command, response=responses) @@ -241,7 +244,7 @@ class CustomCommands(commands.Cog): Example: - `[p]customcom create simple yourcommand Text you want` """ - if command in self.bot.all_commands: + if command in (*self.bot.all_commands, *commands.RESERVED_COMMAND_NAMES): await ctx.send(_("There already exists a bot command with the same name.")) return try: diff --git a/redbot/core/commands/commands.py b/redbot/core/commands/commands.py index e253c82ad..43fe5230d 100644 --- a/redbot/core/commands/commands.py +++ b/redbot/core/commands/commands.py @@ -28,8 +28,14 @@ __all__ = [ "GroupMixin", "command", "group", + "RESERVED_COMMAND_NAMES", ] +#: The following names are reserved for various reasons +RESERVED_COMMAND_NAMES = ( + "cancel", # reserved due to use in ``redbot.core.utils.MessagePredicate`` +) + _ = Translator("commands.commands", __file__) @@ -155,6 +161,12 @@ class Command(CogCommandMixin, commands.Command): super().__init__(*args, **kwargs) self._help_override = kwargs.pop("help_override", None) self.translator = kwargs.pop("i18n", None) + if self.parent is None: + for name in (self.name, *self.aliases): + if name in RESERVED_COMMAND_NAMES: + raise RuntimeError( + f"The name `{name}` cannot be set as a command name. It is reserved for internal use." + ) def _ensure_assignment_on_copy(self, other): super()._ensure_assignment_on_copy(other)