Skip to content

Commit

Permalink
Ability to set guild_ids for all commands in a cog (#1202)
Browse files Browse the repository at this point in the history
* Add ability to set guild_ids per cog

* Document guild_ids per cog

* Create consistency

* Apply code suggestions
  • Loading branch information
Middledot authored Apr 8, 2022
1 parent 8d52bc8 commit 798ad5e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 798ad5e

Please sign in to comment.