Skip to content

Commit d8c4da6

Browse files
committed
Add SlashCommandGroup.subgroup
1 parent 1f28c1b commit d8c4da6

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

discord/commands/commands.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import functools
3131
import inspect
3232
from collections import OrderedDict
33-
from typing import Any, Callable, Dict, List, Optional, Union, TYPE_CHECKING
33+
from typing import Any, Callable, Dict, List, Optional, Type, Union, TYPE_CHECKING
3434

3535
from ..enums import SlashCommandOptionType, ChannelType
3636
from ..member import Member
@@ -786,7 +786,7 @@ def wrap(func) -> SlashCommand:
786786

787787
return wrap
788788

789-
def command_group(self, name, description) -> SlashCommandGroup:
789+
def create_group(self, name, description) -> SlashCommandGroup:
790790
if self.parent is not None:
791791
# TODO: Improve this error message
792792
raise Exception("Subcommands can only be nested once")
@@ -795,6 +795,47 @@ def command_group(self, name, description) -> SlashCommandGroup:
795795
self.subcommands.append(sub_command_group)
796796
return sub_command_group
797797

798+
def subgroup(
799+
self,
800+
name: str,
801+
description: str = None,
802+
guild_ids: Optional[List[int]] = None,
803+
) -> Callable[[Type[SlashCommandGroup]], SlashCommandGroup]:
804+
"""A shortcut decorator that initializes the provided subclass of :class:`.SlashCommandGroup`
805+
as a subgroup.
806+
807+
.. versionadded:: 2.0
808+
809+
Parameters
810+
----------
811+
name: :class:`str`
812+
The name of the group to create.
813+
description: Optional[:class:`str`]
814+
The description of the group to create.
815+
guild_ids: Optional[List[:class:`int`]]
816+
A list of the IDs of each guild this group should be added to, making it a guild command.
817+
This will be a global command if ``None`` is passed.
818+
819+
Returns
820+
--------
821+
Callable[[Type[SlashCommandGroup]], SlashCommandGroup]
822+
The slash command group that was created.
823+
"""
824+
def inner(cls: Type[SlashCommandGroup]) -> SlashCommandGroup:
825+
group = cls(
826+
name,
827+
description or (
828+
inspect.cleandoc(cls.__doc__).splitlines()[0]
829+
if cls.__doc__ is not None
830+
else "No description provided"
831+
),
832+
guild_ids=guild_ids,
833+
parent=self,
834+
)
835+
self.subcommands.append(group)
836+
return group
837+
return inner
838+
798839
async def _invoke(self, ctx: ApplicationContext) -> None:
799840
option = ctx.interaction.data["options"][0]
800841
command = find(lambda x: x.name == option["name"], self.subcommands)

0 commit comments

Comments
 (0)