[commands] Change GroupMixin.commands to all_commands

This is a breaking change as GroupMixin.commands now returns a set
of unique Command objects.
This commit is contained in:
Rapptz
2017-03-20 23:59:23 -04:00
parent c90e52450a
commit fe588a4d52
3 changed files with 25 additions and 20 deletions

View File

@@ -589,16 +589,21 @@ class GroupMixin:
Attributes
-----------
commands : dict
all_commands: dict
A mapping of command name to :class:`Command` or superclass
objects.
"""
def __init__(self, **kwargs):
self.commands = {}
self.all_commands = {}
super().__init__(**kwargs)
@property
def commands(self):
"""Set[:class:`Command`]: A unique set of commands without aliases that are registered."""
return set(self.all_commands.values())
def recursively_remove_all_commands(self):
for command in self.commands.copy().values():
for command in self.all_commands.copy().values():
if isinstance(command, GroupMixin):
command.recursively_remove_all_commands()
self.remove_command(command.name)
@@ -629,14 +634,14 @@ class GroupMixin:
if isinstance(self, Command):
command.parent = self
if command.name in self.commands:
if command.name in self.all_commands:
raise discord.ClientException('Command {0.name} is already registered.'.format(command))
self.commands[command.name] = command
self.all_commands[command.name] = command
for alias in command.aliases:
if alias in self.commands:
if alias in self.all_commands:
raise discord.ClientException('The alias {} is already an existing command or alias.'.format(alias))
self.commands[alias] = command
self.all_commands[alias] = command
def remove_command(self, name):
"""Remove a :class:`Command` or subclasses from the internal list
@@ -655,7 +660,7 @@ class GroupMixin:
The command that was removed. If the name is not valid then
`None` is returned instead.
"""
command = self.commands.pop(name, None)
command = self.all_commands.pop(name, None)
# does not exist
if command is None:
@@ -667,12 +672,12 @@ class GroupMixin:
# we're not removing the alias so let's delete the rest of them.
for alias in command.aliases:
self.commands.pop(alias, None)
self.all_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()):
for command in tuple(self.all_commands.values()):
yield command
if isinstance(command, GroupMixin):
yield from command.walk_commands()
@@ -699,13 +704,13 @@ class GroupMixin:
"""
names = name.split()
obj = self.commands.get(names[0])
obj = self.all_commands.get(names[0])
if not isinstance(obj, GroupMixin):
return obj
for name in names[1:]:
try:
obj = obj.commands[name]
obj = obj.all_commands[name]
except (AttributeError, KeyError):
return None
@@ -769,7 +774,7 @@ class Group(GroupMixin, Command):
if trigger:
ctx.subcommand_passed = trigger
ctx.invoked_subcommand = self.commands.get(trigger, None)
ctx.invoked_subcommand = self.all_commands.get(trigger, None)
if early_invoke:
injected = hooked_wrapped_callback(self, ctx, self.callback)