mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-08 04:38:42 +00:00
[commands] Better support for retrieving children commands.
* GroupMixin.get_command now supports fully qualified names * Add GroupMixin.walk_commands to get an iterator of all commands.
This commit is contained in:
parent
f06024f236
commit
2a6c240271
@ -519,15 +519,26 @@ class GroupMixin:
|
|||||||
self.commands.pop(alias, None)
|
self.commands.pop(alias, None)
|
||||||
return command
|
return command
|
||||||
|
|
||||||
|
def walk_commands(self):
|
||||||
|
"""An iterator that recursively walks through all commands and subcommands."""
|
||||||
|
for command in tuple(self.commands.values()):
|
||||||
|
yield command
|
||||||
|
if isinstance(command, GroupMixin):
|
||||||
|
yield from command.walk_commands()
|
||||||
|
|
||||||
def get_command(self, name):
|
def get_command(self, name):
|
||||||
"""Get a :class:`Command` or subclasses from the internal list
|
"""Get a :class:`Command` or subclasses from the internal list
|
||||||
of commands.
|
of commands.
|
||||||
|
|
||||||
This could also be used as a way to get aliases.
|
This could also be used as a way to get aliases.
|
||||||
|
|
||||||
|
The name could be fully qualified (e.g. ``'foo bar'``) will get
|
||||||
|
the subcommand ``bar`` of the group command ``foo``. If a
|
||||||
|
subcommand is not found then ``None`` is returned just as usual.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
-----------
|
-----------
|
||||||
name : str
|
name: str
|
||||||
The name of the command to get.
|
The name of the command to get.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
@ -535,7 +546,19 @@ class GroupMixin:
|
|||||||
Command or subclass
|
Command or subclass
|
||||||
The command that was requested. If not found, returns ``None``.
|
The command that was requested. If not found, returns ``None``.
|
||||||
"""
|
"""
|
||||||
return self.commands.get(name, None)
|
|
||||||
|
names = name.split()
|
||||||
|
obj = self.commands.get(names[0])
|
||||||
|
if not isinstance(obj, GroupMixin):
|
||||||
|
return obj
|
||||||
|
|
||||||
|
for name in names[1:]:
|
||||||
|
try:
|
||||||
|
obj = obj.commands[name]
|
||||||
|
except (AttributeError, KeyError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
def command(self, *args, **kwargs):
|
def command(self, *args, **kwargs):
|
||||||
"""A shortcut decorator that invokes :func:`command` and adds it to
|
"""A shortcut decorator that invokes :func:`command` and adds it to
|
||||||
|
Loading…
x
Reference in New Issue
Block a user