From 1a22df62289816d5435456b97e942e3fe939d73e Mon Sep 17 00:00:00 2001 From: Gnome Date: Tue, 31 Aug 2021 15:17:49 +0100 Subject: [PATCH] Add group command support --- discord/ext/commands/bot.py | 11 +++++------ discord/ext/commands/core.py | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index a5a727d1..36946c85 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -1081,12 +1081,11 @@ class BotBase(GroupMixin): # Fetch out subcommands from the options command_name = interaction.data['name'] command_options = interaction.data.get('options') or [] - for option in command_options: - if option['type'] in {1, 2}: - command_name = option['name'] - command_options = option.get('options') or [] - - command_name += f'{command_name} ' + while any(o["type"] in {1, 2} for o in command_options): + for option in command_options: + if option['type'] in {1, 2}: + command_name += f' {option["name"]}' + command_options = option.get('options') or [] command = self.get_command(command_name) if command is None: diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index cb411f2c..123a76d6 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -1146,15 +1146,19 @@ class Command(_BaseCommand, Generic[CogT, P, T]): finally: ctx.command = original - def to_application_command(self) -> Optional[EditApplicationCommand]: + def to_application_command(self, nested: int = 0) -> Optional[EditApplicationCommand]: if self.slash_command is False: return + elif nested == 3: + raise ValueError(f"{self.qualified_name} is too deeply nested!") payload = { "name": self.name, "description": self.short_doc or "no description", "options": [] } + if nested != 0: + payload["type"] = 1 option_descriptions = self.extras.get("option_descriptions", {}) for name, param in self.clean_params.items(): @@ -1561,6 +1565,22 @@ class Group(GroupMixin[CogT], Command[CogT, P, T]): view.previous = previous await super().reinvoke(ctx, call_hooks=call_hooks) + def to_application_command(self, nested: int = 0) -> Optional[EditApplicationCommand]: + if self.slash_command is False: + return + elif nested == 2: + raise ValueError(f"{self.qualified_name} is too deeply nested for slash commands!") + + return { # type: ignore + "name": self.name, + "type": int(not (nested - 1)) + 1, + "description": self.short_doc or 'no description', + "options": [ + cmd.to_application_command(nested=nested+1) + for cmd in self.commands + ] + } + # Decorators @overload