From 798ad5e6721ec0cef4720d74bbde2d30d27e821f Mon Sep 17 00:00:00 2001 From: Middledot <78228142+Middledot@users.noreply.github.com> Date: Fri, 8 Apr 2022 13:16:05 -0400 Subject: [PATCH] Ability to set guild_ids for all commands in a cog (#1202) * Add ability to set guild_ids per cog * Document guild_ids per cog * Create consistency * Apply code suggestions --- discord/cog.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/discord/cog.py b/discord/cog.py index 00459e3c6d..80ebb9d899 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -126,17 +126,25 @@ async def foo(self, ctx): @commands.command(hidden=False) async def bar(self, ctx): pass # hidden -> False + + guild_ids: Optional[List[:class:`int`]] + A shortcut to command_attrs, what guild_ids should all application commands have + in the cog. You can override this by setting guild_ids per command. + + .. versionadded:: 2.0 """ __cog_name__: str __cog_settings__: Dict[str, Any] __cog_commands__: List[ApplicationCommand] __cog_listeners__: List[Tuple[str, str]] + __cog_guild_ids__: List[int] def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: name, bases, attrs = args attrs["__cog_name__"] = kwargs.pop("name", name) attrs["__cog_settings__"] = kwargs.pop("command_attrs", {}) + attrs["__cog_guild_ids__"] = kwargs.pop("guild_ids", []) description = kwargs.pop("description", None) if description is None: @@ -213,6 +221,8 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: # Update the Command instances dynamically as well for command in new_cls.__cog_commands__: + if isinstance(command, ApplicationCommand) and command.guild_ids is None and len(new_cls.__cog_guild_ids__) != 0: + command.guild_ids = new_cls.__cog_guild_ids__ if not isinstance(command, SlashCommandGroup): setattr(new_cls, command.callback.__name__, command) parent = command.parent @@ -254,6 +264,7 @@ class Cog(metaclass=CogMeta): __cog_settings__: ClassVar[Dict[str, Any]] __cog_commands__: ClassVar[List[ApplicationCommand]] __cog_listeners__: ClassVar[List[Tuple[str, str]]] + __cog_guild_ids__: ClassVar[List[int]] def __new__(cls: Type[CogT], *args: Any, **kwargs: Any) -> CogT: # For issue 426, we need to store a copy of the command objects