aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2016-06-20 21:38:17 -0400
committerRapptz <[email protected]>2016-06-20 21:38:17 -0400
commit324d10c9d97ce069f79c9055ea1492ea755ddc7c (patch)
tree5eb3131461bac54511dc3b745be1796da64ed601
parentRaise TypeError if "after" parameter is not a callable. (diff)
downloaddiscord.py-324d10c9d97ce069f79c9055ea1492ea755ddc7c.tar.xz
discord.py-324d10c9d97ce069f79c9055ea1492ea755ddc7c.zip
[commands] Add Command.ignore_extra attribute to ignore extra arguments
This allows you to strictly require a number of arguments. The default behaviour in this case is still `True`, since it would be a breaking change otherwise and is a sane default. However if someone would want to set this to `False`, they would receive an exception of type `TooManyArguments` if too many arguments are passed to a command. Hopefully this removes the uses of `ctx.message.content == 'stuff'` inside commands.
-rw-r--r--discord/ext/commands/core.py15
-rw-r--r--discord/ext/commands/errors.py8
2 files changed, 20 insertions, 3 deletions
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py
index 330f2606..39dffaf6 100644
--- a/discord/ext/commands/core.py
+++ b/discord/ext/commands/core.py
@@ -100,10 +100,10 @@ class Command:
description : str
The message prefixed into the default help command.
hidden : bool
- If ``True``, the default help command does not show this in the
+ If ``True``\, the default help command does not show this in the
help output.
no_pm : bool
- If ``True``, then the command is not allowed to be executed in
+ If ``True``\, then the command is not allowed to be executed in
private messages. Defaults to ``False``. Note that if it is executed
in private messages, then :func:`on_command_error` and local error handlers
are called with the :exc:`NoPrivateMessage` error.
@@ -114,6 +114,11 @@ class Command:
regular matter rather than passing the rest completely raw. If ``True``
then the keyword-only argument will pass in the rest of the arguments
in a completely raw matter. Defaults to ``False``.
+ ignore_extra : bool
+ If ``True``\, ignores extraneous strings passed to a command if all its
+ requirements are met (e.g. ``?foo a b c`` when only expecting ``a``
+ and ``b``). Otherwise :func:`on_command_error` and local error handlers
+ are called with :exc:`TooManyArguments`. Defaults to ``True``.
"""
def __init__(self, name, callback, **kwargs):
self.name = name
@@ -134,6 +139,7 @@ class Command:
self.checks = kwargs.get('checks', [])
self.module = inspect.getmodule(callback)
self.no_pm = kwargs.get('no_pm', False)
+ self.ignore_extra = kwargs.get('ignore_extra', True)
self.instance = None
self.parent = None
@@ -388,6 +394,11 @@ class Command:
except RuntimeError:
break
+ if not self.ignore_extra:
+ if not view.eof:
+ raise TooManyArguments('Too many arguments passed to ' + self.qualified_name)
+
+
def _verify_checks(self, ctx):
if not self.enabled:
raise DisabledCommand('{0.name} command is disabled'.format(self))
diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py
index a2fef1fe..7c95b118 100644
--- a/discord/ext/commands/errors.py
+++ b/discord/ext/commands/errors.py
@@ -28,7 +28,7 @@ from discord.errors import DiscordException
__all__ = [ 'CommandError', 'MissingRequiredArgument', 'BadArgument',
'NoPrivateMessage', 'CheckFailure', 'CommandNotFound',
- 'DisabledCommand', 'CommandInvokeError' ]
+ 'DisabledCommand', 'CommandInvokeError', 'TooManyArguments' ]
class CommandError(DiscordException):
"""The base exception type for all command related errors.
@@ -94,3 +94,9 @@ class CommandInvokeError(CommandError):
def __init__(self, e):
self.original = e
super().__init__('Command raised an exception: {0.__class__.__name__}: {0}'.format(e))
+
+class TooManyArguments(CommandError):
+ """Exception raised when the command was passed too many arguments and its
+ :attr:`Command.ignore_extra` attribute was not set to ``True``.
+ """
+ pass