Skip to content

Commit 02e346d

Browse files
Makiyu-pyLulalaby
authored andcommitted
add walk cmd iterator on slash groups and on discord.Bot
1 parent 7c0eeeb commit 02e346d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

discord/bot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Any,
3636
Callable,
3737
Coroutine,
38+
Generator,
3839
List,
3940
Optional,
4041
Type,
@@ -670,6 +671,19 @@ def inner(cls: Type[SlashCommandGroup]) -> SlashCommandGroup:
670671

671672
slash_group = group
672673

674+
def walk_application_commands(self) -> Generator[ApplicationCommand, None, None]:
675+
"""An iterator that recursively walks through all application commands and subcommands.
676+
677+
Yields
678+
------
679+
:class:`.ApplicationCommand`
680+
An application command from the internal list of application commands.
681+
"""
682+
for command in self.application_commands:
683+
if isinstance(command, SlashCommandGroup):
684+
yield from command.walk_commands()
685+
yield command
686+
673687
async def get_application_context(
674688
self, interaction: Interaction, cls=None
675689
) -> ApplicationContext:

discord/commands/commands.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import re
3333
import types
3434
from collections import OrderedDict
35-
from typing import Any, Callable, Dict, Generic, List, Optional, Type, TypeVar, Union, TYPE_CHECKING
35+
from typing import Any, Callable, Dict, Generator, Generic, List, Optional, Type, TypeVar, Union, TYPE_CHECKING
3636

3737
from .context import ApplicationContext, AutocompleteContext
3838
from .errors import ApplicationCommandError, CheckFailure, ApplicationCommandInvokeError
@@ -959,6 +959,19 @@ async def invoke_autocomplete_callback(self, ctx: AutocompleteContext) -> None:
959959
ctx.interaction.data = option
960960
await command.invoke_autocomplete_callback(ctx)
961961

962+
def walk_commands(self) -> Generator[SlashCommand, None, None]:
963+
"""An iterator that recursively walks through all slash commands in this group.
964+
965+
Yields
966+
------
967+
:class:`.SlashCommand`
968+
A slash command from the group.
969+
"""
970+
for command in self.subcommands:
971+
if isinstance(command, SlashCommandGroup):
972+
yield from command.walk_commands()
973+
yield command
974+
962975
def copy(self):
963976
"""Creates a copy of this command group.
964977

0 commit comments

Comments
 (0)