[commands] Add CommandNotFound error.
This commit is contained in:
		| @@ -31,6 +31,7 @@ import inspect | |||||||
| from .core import GroupMixin | from .core import GroupMixin | ||||||
| from .view import StringView | from .view import StringView | ||||||
| from .context import Context | from .context import Context | ||||||
|  | from .errors import CommandNotFound | ||||||
|  |  | ||||||
| class Bot(GroupMixin, discord.Client): | class Bot(GroupMixin, discord.Client): | ||||||
|     """Represents a discord bot. |     """Represents a discord bot. | ||||||
| @@ -203,18 +204,21 @@ class Bot(GroupMixin, discord.Client): | |||||||
|  |  | ||||||
|         view.skip_ws() |         view.skip_ws() | ||||||
|         invoker = view.get_word() |         invoker = view.get_word() | ||||||
|  |         tmp = { | ||||||
|  |             'bot': self, | ||||||
|  |             'invoked_with': invoker, | ||||||
|  |             'message': message, | ||||||
|  |             'view': view, | ||||||
|  |         } | ||||||
|  |         ctx = Context(**tmp) | ||||||
|  |         del tmp | ||||||
|         if invoker in self.commands: |         if invoker in self.commands: | ||||||
|             command = self.commands[invoker] |             command = self.commands[invoker] | ||||||
|             tmp = { |             ctx.command = command | ||||||
|                 'bot': self, |  | ||||||
|                 'invoked_with': invoker, |  | ||||||
|                 'message': message, |  | ||||||
|                 'view': view, |  | ||||||
|                 'command': command |  | ||||||
|             } |  | ||||||
|             ctx = Context(**tmp) |  | ||||||
|             del tmp |  | ||||||
|             yield from command.invoke(ctx) |             yield from command.invoke(ctx) | ||||||
|  |         else: | ||||||
|  |             exc = CommandNotFound('Command "{}" is not found'.format(invoker)) | ||||||
|  |             self.dispatch('command_error', exc, ctx) | ||||||
|  |  | ||||||
|     @asyncio.coroutine |     @asyncio.coroutine | ||||||
|     def on_message(self, message): |     def on_message(self, message): | ||||||
|   | |||||||
| @@ -334,7 +334,6 @@ class Group(GroupMixin, Command): | |||||||
|         if ctx.invoked_subcommand: |         if ctx.invoked_subcommand: | ||||||
|             yield from ctx.invoked_subcommand.invoke(ctx) |             yield from ctx.invoked_subcommand.invoke(ctx) | ||||||
|  |  | ||||||
|  |  | ||||||
| # Decorators | # Decorators | ||||||
|  |  | ||||||
| def command(name=None, cls=None, **attrs): | def command(name=None, cls=None, **attrs): | ||||||
|   | |||||||
| @@ -27,7 +27,7 @@ from discord.errors import DiscordException | |||||||
|  |  | ||||||
|  |  | ||||||
| __all__ = [ 'CommandError', 'MissingRequiredArgument', 'BadArgument', | __all__ = [ 'CommandError', 'MissingRequiredArgument', 'BadArgument', | ||||||
|            'NoPrivateMessage', 'CheckFailure' ] |            'NoPrivateMessage', 'CheckFailure', 'CommandNotFound' ] | ||||||
|  |  | ||||||
| class CommandError(DiscordException): | class CommandError(DiscordException): | ||||||
|     """The base exception type for all command related errors. |     """The base exception type for all command related errors. | ||||||
| @@ -40,6 +40,14 @@ class CommandError(DiscordException): | |||||||
|     """ |     """ | ||||||
|     pass |     pass | ||||||
|  |  | ||||||
|  | class CommandNotFound(CommandError): | ||||||
|  |     """Exception raised when a command is attempted to be invoked | ||||||
|  |     but no command under that name is found. | ||||||
|  |  | ||||||
|  |     This is not raised for invalid subcommands, rather just the | ||||||
|  |     initial main command that is attempted to be invoked. | ||||||
|  |     """ | ||||||
|  |  | ||||||
| class MissingRequiredArgument(CommandError): | class MissingRequiredArgument(CommandError): | ||||||
|     """Exception raised when parsing a command and a parameter |     """Exception raised when parsing a command and a parameter | ||||||
|     that is required is not encountered. |     that is required is not encountered. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user