Skip to content

Commit

Permalink
fix: walk_commands()
Browse files Browse the repository at this point in the history
  • Loading branch information
Middledot committed Feb 9, 2022
1 parent 105a3cc commit dbaf8ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
11 changes: 8 additions & 3 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import discord.utils
from . import errors
from .commands import _BaseCommand, ApplicationCommand, ApplicationContext
from .commands import _BaseCommand, ApplicationCommand, ApplicationContext, SlashCommandGroup

__all__ = (
'CogMeta',
Expand Down Expand Up @@ -275,8 +275,13 @@ def walk_commands(self) -> Generator[ApplicationCommand, None, None]:
A command or group from the cog.
"""
for command in self.__cog_commands__:
if command.parent is None:
yield command
if isinstance(command, SlashCommandGroup):
for subcommand in command.subcommands:
if subcommand.parent is not None:
for sub_subcommand in subcommand.subcommands:
yield sub_subcommand
else:
yield subcommand

def get_listeners(self) -> List[Tuple[str, Callable[..., Any]]]:
"""Returns a :class:`list` of (name, function) listener pairs that are defined in this cog.
Expand Down
3 changes: 3 additions & 0 deletions discord/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ def qualified_name(self) -> str:
else:
return self.name

def __str__(self) -> str:
return self.qualified_name

def _set_cog(self, cog):
self.cog = cog

Expand Down
19 changes: 15 additions & 4 deletions discord/ext/commands/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from typing import Any, Callable, Generator, TYPE_CHECKING, List, TypeVar, Type, Union

from ...commands import ApplicationCommand
from ...commands import ApplicationCommand, SlashCommandGroup

if TYPE_CHECKING:
from .core import Command
Expand Down Expand Up @@ -59,14 +59,25 @@ def walk_commands(self) -> Generator[Command, None, None]:
A command or group from the cog.
"""
from .core import GroupMixin
print(self.__cog_commands__)
for command in self.__cog_commands__:
if isinstance(command, ApplicationCommand):
yield command
else:
if not isinstance(command, ApplicationCommand):
if command.parent is None:
yield command
if isinstance(command, GroupMixin):
yield from command.walk_commands()
elif isinstance(command, SlashCommandGroup):
for subcommand in command.subcommands:
print(subcommand)
print(subcommand.parent)
if subcommand.parent is not None and isinstance(subcommand, SlashCommandGroup):
print("reach")
for sub_subcommand in subcommand.subcommands:
yield sub_subcommand
else:
yield subcommand
else:
yield command

def get_commands(self) -> List[Union[ApplicationCommand, Command]]:
r"""
Expand Down

0 comments on commit dbaf8ff

Please sign in to comment.