aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-02-06 21:04:33 -0500
committerRapptz <[email protected]>2017-02-06 21:08:29 -0500
commit2a6c240271efd47323015f49fa4c4e79a582a4fd (patch)
tree5f37d99f8a3c2de1f11ac8ddda61af9ade597b43
parentFix potential chunking woe. (diff)
downloaddiscord.py-2a6c240271efd47323015f49fa4c4e79a582a4fd.tar.xz
discord.py-2a6c240271efd47323015f49fa4c4e79a582a4fd.zip
[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.
-rw-r--r--discord/ext/commands/core.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py
index e3f7148f..ee525386 100644
--- a/discord/ext/commands/core.py
+++ b/discord/ext/commands/core.py
@@ -519,15 +519,26 @@ class GroupMixin:
self.commands.pop(alias, None)
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):
"""Get a :class:`Command` or subclasses from the internal list
of commands.
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
-----------
- name : str
+ name: str
The name of the command to get.
Returns
@@ -535,7 +546,19 @@ class GroupMixin:
Command or subclass
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):
"""A shortcut decorator that invokes :func:`command` and adds it to