diff --git a/discord/bot.py b/discord/bot.py index e7601d6283..719c242289 100644 --- a/discord/bot.py +++ b/discord/bot.py @@ -37,6 +37,7 @@ Coroutine, List, Optional, + Type, TypeVar, Union, ) @@ -86,7 +87,7 @@ class ApplicationCommandMixin: def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self._pending_application_commands = [] - self.application_commands = {} + self._application_commands = {} @property def pending_application_commands(self): @@ -94,11 +95,15 @@ def pending_application_commands(self): @property def commands(self) -> List[Union[ApplicationCommand, Any]]: - commands = list(self.application_commands.values()) + commands = self.application_commands if self._supports_prefixed_commands: commands += self.prefixed_commands return commands + @property + def application_commands(self) -> List[ApplicationCommand]: + return list(self._application_commands.values()) + def add_application_command(self, command: ApplicationCommand) -> None: """Adds a :class:`.ApplicationCommand` into the internal list of commands. @@ -112,7 +117,6 @@ def add_application_command(self, command: ApplicationCommand) -> None: command: :class:`.ApplicationCommand` The command to add. """ - if self.debug_guilds and command.guild_ids is None: command.guild_ids = self.debug_guilds self._pending_application_commands.append(command) @@ -136,7 +140,54 @@ def remove_application_command( The command that was removed. If the name is not valid then ``None`` is returned instead. """ - return self.application_commands.pop(command.id) + return self._application_commands.pop(command.id) + + @property + def get_command(self): + """Shortcut for :meth:`.get_application_command`. + + .. note:: + Overridden in :class:`ext.commands.Bot`. + + .. versionadded:: 2.0 + """ + # TODO: Do something like we did in self.commands for this + return self.get_application_command + + def get_application_command( + self, + name: str, + guild_ids: Optional[List[int]] = None, + type: Type[ApplicationCommand] = SlashCommand, + ) -> Optional[ApplicationCommand]: + """Get a :class:`.ApplicationCommand` from the internal list + of commands. + + .. versionadded:: 2.0 + + Parameters + ----------- + name: :class:`str` + The name of the command to get. + guild_ids: List[:class:`int`] + The guild ids associated to the command to get. + type: Type[:class:`.ApplicationCommand`] + The type of the command to get. Defaults to :class:`.SlashCommand`. + + Returns + -------- + Optional[:class:`.ApplicationCommand`] + The command that was requested. If not found, returns ``None``. + """ + + for command in self._application_commands.values(): + if ( + command.name == name + and isinstance(command, type) + ): + if guild_ids is not None and command.guild_ids != guild_ids: + return + return command async def sync_commands(self) -> None: """|coro| @@ -199,7 +250,7 @@ async def register_commands(self) -> None: type=i["type"], ) cmd.id = i["id"] - self.application_commands[cmd.id] = cmd + self._application_commands[cmd.id] = cmd # Permissions (Roles will be converted to IDs just before Upsert for Global Commands) global_permissions.append({"id": i["id"], "permissions": cmd.permissions}) @@ -234,7 +285,7 @@ async def register_commands(self) -> None: for i in cmds: cmd = find(lambda cmd: cmd.name == i["name"] and cmd.type == i["type"] and int(i["guild_id"]) in cmd.guild_ids, self.pending_application_commands) cmd.id = i["id"] - self.application_commands[cmd.id] = cmd + self._application_commands[cmd.id] = cmd # Permissions permissions = [ @@ -325,7 +376,7 @@ async def register_commands(self) -> None: if len(new_cmd_perm["permissions"]) > 10: print( "Command '{name}' has more than 10 permission overrides in guild ({guild_id}).\nwill only use the first 10 permission overrides.".format( - name=self.application_commands[new_cmd_perm["id"]].name, + name=self._application_commands[new_cmd_perm["id"]].name, guild_id=guild_id, ) ) @@ -375,7 +426,7 @@ async def process_application_commands(self, interaction: Interaction) -> None: return try: - command = self.application_commands[interaction.data["id"]] + command = self._application_commands[interaction.data["id"]] except KeyError: self.dispatch("unknown_command", interaction) else: