From b50f7b1271840ea214f431c418c00da2caa1771c Mon Sep 17 00:00:00 2001 From: Middledot Date: Sun, 27 Mar 2022 13:38:04 -0400 Subject: [PATCH 1/4] Add ability to set guild_ids per cog --- discord/cog.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/discord/cog.py b/discord/cog.py index 9c7469ebf6..1568faec05 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -135,6 +135,7 @@ async def bar(self, ctx): def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: name, bases, attrs = args + guild_ids = kwargs.pop("guild_ids", []) attrs["__cog_name__"] = kwargs.pop("name", name) attrs["__cog_settings__"] = kwargs.pop("command_attrs", {}) @@ -213,6 +214,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(guild_ids) != 0: + command.guild_ids = guild_ids if not isinstance(command, SlashCommandGroup): setattr(new_cls, command.callback.__name__, command) parent = command.parent From f4d4cc2973573d74e5490ad5586aa56492f90606 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sun, 27 Mar 2022 13:43:06 -0400 Subject: [PATCH 2/4] Document guild_ids per cog --- discord/cog.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/discord/cog.py b/discord/cog.py index 1568faec05..da77880356 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -126,6 +126,12 @@ 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 From a0696d1bd4ff466b009a875275e11b317b51c93e Mon Sep 17 00:00:00 2001 From: Middledot Date: Sun, 27 Mar 2022 13:44:05 -0400 Subject: [PATCH 3/4] Create consistency --- discord/cog.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/discord/cog.py b/discord/cog.py index da77880356..2bfaa3486d 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -141,9 +141,9 @@ async def bar(self, ctx): def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: name, bases, attrs = args - guild_ids = kwargs.pop("guild_ids", []) 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: @@ -220,8 +220,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(guild_ids) != 0: - command.guild_ids = guild_ids + if isinstance(command, ApplicationCommand) and command.guild_ids is None and len(new_cls.guild_ids) != 0: + command.guild_ids = new_cls.guild_ids if not isinstance(command, SlashCommandGroup): setattr(new_cls, command.callback.__name__, command) parent = command.parent From bd264c93ebd006a97ab5e6679b236c487fa8fe79 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sun, 27 Mar 2022 16:12:02 -0400 Subject: [PATCH 4/4] Apply code suggestions --- discord/cog.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/discord/cog.py b/discord/cog.py index 2bfaa3486d..9e6bda74d4 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -138,6 +138,7 @@ async def bar(self, ctx): __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 @@ -220,8 +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.guild_ids) != 0: - command.guild_ids = new_cls.guild_ids + 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 @@ -263,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