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
This commit is contained in:
Michael H 2019-09-28 16:58:40 -04:00 committed by GitHub
parent e38c08ab12
commit 83483abfa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1 @@
Reserves some command names for internal Red use. These are available programatically as ``redbot.core.commands.RESERVED_COMMAND_NAMES``

View File

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

View File

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

View File

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