From 48658828a8afba2f12943a3ab63dbe4d06a81860 Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Thu, 25 May 2023 17:37:47 +0200 Subject: [PATCH 1/2] Add guild(s) kwarg to Bot.add_command --- discord/ext/commands/bot.py | 63 +++++++++++++++++++++++++++++++------ discord/ext/commands/cog.py | 2 +- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 363b6656c..3783a6c58 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -241,8 +241,51 @@ class BotBase(GroupMixin[None]): # GroupMixin overrides - @discord.utils.copy_doc(GroupMixin.add_command) - def add_command(self, command: Command[Any, ..., Any], /) -> None: + def add_command( + self, + command: Command[Any, ..., Any], + /, + *, + guild: Optional[Snowflake] = MISSING, + guilds: Sequence[Snowflake] = MISSING, + ) -> None: + """Adds a :class:`.Command` into the internal list of commands. + + 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` + + .. versionchanged:: 2.0 + + ``command`` parameter is now positional-only. + + Parameters + ----------- + command: :class:`Command` + The command to add. + guild: Optional[:class:`~discord.abc.Snowflake`] + If the command is a hybrid command with an application command attached, then this + will be the guild the app command will be added to. + If not given, the app command will be global. + + .. versionadded:: 2.3 + guilds: List[:class:`~discord.abc.Snowflake`] + If the command is a hybrid command with an application command attached, then these + would be the guilds the app command will be added to. + If not given, the app command will be global. Cannot be mixed with + ``guild``. + + .. versionadded:: 2.3 + + Raises + ------- + 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`. Or, guild and guilds were both given. + """ super().add_command(command) if isinstance(command, (HybridCommand, HybridGroup)) and command.app_command: # If a cog is also inheriting from app_commands.Group then it'll also @@ -250,7 +293,7 @@ class BotBase(GroupMixin[None]): # hybrid commands as slash commands. This check just terminates that recursion # from happening if command.cog is None or not command.cog.__cog_is_app_commands_group__: - self.tree.add_command(command.app_command) + self.tree.add_command(command.app_command, guild=guild, guilds=guilds) @discord.utils.copy_doc(GroupMixin.remove_command) def remove_command(self, name: str, /) -> Optional[Command[Any, ..., Any]]: @@ -743,15 +786,15 @@ class BotBase(GroupMixin[None]): .. versionadded:: 2.0 guild: Optional[:class:`~discord.abc.Snowflake`] - If the cog is an application command group, then this would be the - guild where the cog group would be added to. If not given then - it becomes a global command instead. + If the cog has application commands, then this would + the guild the app commands will be added to. + If not given, all app commands will be global. .. versionadded:: 2.0 guilds: List[:class:`~discord.abc.Snowflake`] - If the cog is an application command group, then this would be the - guilds where the cog group would be added to. If not given then - it becomes a global command instead. Cannot be mixed with + If the cog has application commands, then these + would the guilds the app commands will be added to. + If not given, all app commands will be global. Cannot be mixed with ``guild``. .. versionadded:: 2.0 @@ -759,7 +802,7 @@ class BotBase(GroupMixin[None]): Raises ------- TypeError - The cog does not inherit from :class:`.Cog`. + The cog does not inherit from :class:`.Cog`. Or, guild and guilds were both given. CommandError An error happened during loading. ClientException diff --git a/discord/ext/commands/cog.py b/discord/ext/commands/cog.py index 319f85b80..85d35eded 100644 --- a/discord/ext/commands/cog.py +++ b/discord/ext/commands/cog.py @@ -691,7 +691,7 @@ class Cog(metaclass=CogMeta): command.cog = self if command.parent is None: try: - bot.add_command(command) + bot.add_command(command, guild=guild, guilds=guilds) except Exception as e: # undo our additions for to_undo in self.__cog_commands__[:index]: From 0578a99ad0d68829d1f74a482598046d478f109b Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Thu, 25 May 2023 17:43:59 +0200 Subject: [PATCH 2/2] Add versionchanged doc to add_cog --- discord/ext/commands/bot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 3783a6c58..8b337ca42 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -790,6 +790,8 @@ class BotBase(GroupMixin[None]): the guild the app commands will be added to. If not given, all app commands will be global. + .. versionchanged:: 2.3 + This kwarg is now also used by hybrid app commands. .. versionadded:: 2.0 guilds: List[:class:`~discord.abc.Snowflake`] If the cog has application commands, then these @@ -797,6 +799,8 @@ class BotBase(GroupMixin[None]): If not given, all app commands will be global. Cannot be mixed with ``guild``. + .. versionchanged:: 2.3 + This kwarg is now also used by hybrid app commands. .. versionadded:: 2.0 Raises