aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2016-01-07 17:59:26 -0500
committerRapptz <[email protected]>2016-01-07 17:59:26 -0500
commit25588955e4e3bee9f9bbe8efaf8ade1b29a1de5a (patch)
treedd897e5f77d4ada423c263d8fbe7b2f689e7f754
parent[commands] Special handling for when the converter is bool. (diff)
downloaddiscord.py-25588955e4e3bee9f9bbe8efaf8ade1b29a1de5a.tar.xz
discord.py-25588955e4e3bee9f9bbe8efaf8ade1b29a1de5a.zip
[commands] Add a way to enable or disable certain commands.
-rw-r--r--discord/ext/commands/core.py39
-rw-r--r--discord/ext/commands/errors.py7
2 files changed, 39 insertions, 7 deletions
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py
index 06c64b1b..b86211de 100644
--- a/discord/ext/commands/core.py
+++ b/discord/ext/commands/core.py
@@ -66,6 +66,11 @@ class Command:
pass_context : bool
A boolean that indicates that the current :class:`Context` should
be passed as the **first parameter**. Defaults to `False`.
+ enabled : bool
+ A boolean that indicates if the command is currently enabled.
+ If the command is invoked while it is disabled, then
+ :exc:`DisabledCommand` is raised to the :func:`on_command_error`
+ event. Defaults to ``True``.
checks
A list of predicates that verifies if the command could be executed
with the given :class:`Context` as the sole parameter. If an exception
@@ -77,6 +82,7 @@ class Command:
def __init__(self, name, callback, **kwargs):
self.name = name
self.callback = callback
+ self.enabled = kwargs.get('enabled', True)
self.help = kwargs.get('help')
self.brief = kwargs.get('brief')
self.aliases = kwargs.get('aliases', [])
@@ -206,15 +212,18 @@ class Command:
return True
def _verify_checks(self, ctx):
- predicates = self.checks
- if predicates:
- try:
+ try:
+ if not self.enabled:
+ raise DisabledCommand('{0.name} command is disabled'.format(self))
+
+ predicates = self.checks
+ if predicates:
check = all(predicate(ctx) for predicate in predicates)
if not check:
raise CheckFailure('The check functions for command {0.name} failed.'.format(self))
- except CommandError as exc:
- ctx.bot.dispatch('command_error', exc, ctx)
- return False
+ except CommandError as exc:
+ ctx.bot.dispatch('command_error', exc, ctx)
+ return False
return True
@@ -291,6 +300,24 @@ class GroupMixin:
"""
return self.commands.pop(name, None)
+ 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.
+
+ Parameters
+ -----------
+ name : str
+ The name of the command to get.
+
+ Returns
+ --------
+ Command or subclass
+ The command that was requested. If not found, returns ``None``.
+ """
+ return self.commands.get(name, None)
+
def command(self, *args, **kwargs):
"""A shortcut decorator that invokes :func:`command` and adds it to
the internal command list via :meth:`add_command`.
diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py
index 9ffb6bd4..08545ade 100644
--- a/discord/ext/commands/errors.py
+++ b/discord/ext/commands/errors.py
@@ -27,7 +27,8 @@ from discord.errors import DiscordException
__all__ = [ 'CommandError', 'MissingRequiredArgument', 'BadArgument',
- 'NoPrivateMessage', 'CheckFailure', 'CommandNotFound' ]
+ 'NoPrivateMessage', 'CheckFailure', 'CommandNotFound',
+ 'DisabledCommand' ]
class CommandError(DiscordException):
"""The base exception type for all command related errors.
@@ -69,3 +70,7 @@ class NoPrivateMessage(CommandError):
class CheckFailure(CommandError):
"""Exception raised when the predicates in :attr:`Command.checks` have failed."""
pass
+
+class DisabledCommand(CommandError):
+ """Exception raised when the command being invoked is disabled."""
+ pass