Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set guild_ids for all commands in a cog #1202

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", [])
Middledot marked this conversation as resolved.
Show resolved Hide resolved

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