[commands] Added dm_only check
Raises PrivateMessageOnly on failure.
This commit is contained in:
		| @@ -40,7 +40,7 @@ from .cog import Cog | |||||||
| __all__ = ['Command', 'Group', 'GroupMixin', 'command', 'group', | __all__ = ['Command', 'Group', 'GroupMixin', 'command', 'group', | ||||||
|            'has_role', 'has_permissions', 'has_any_role', 'check', |            'has_role', 'has_permissions', 'has_any_role', 'check', | ||||||
|            'bot_has_role', 'bot_has_permissions', 'bot_has_any_role', |            'bot_has_role', 'bot_has_permissions', 'bot_has_any_role', | ||||||
|            'cooldown', 'guild_only', 'is_owner', 'is_nsfw'] |            'cooldown', 'dm_only', 'guild_only', 'is_owner', 'is_nsfw'] | ||||||
|  |  | ||||||
| def wrap_callback(coro): | def wrap_callback(coro): | ||||||
|     @functools.wraps(coro) |     @functools.wraps(coro) | ||||||
| @@ -1433,6 +1433,22 @@ def bot_has_permissions(**perms): | |||||||
|  |  | ||||||
|     return check(predicate) |     return check(predicate) | ||||||
|  |  | ||||||
|  | def dm_only(): | ||||||
|  |     """A :func:`.check` that indicates this command must only be used in a | ||||||
|  |     DM context only. Only private messages are allowed when | ||||||
|  |     using the command. | ||||||
|  |  | ||||||
|  |     This check raises a special exception, :exc:`.PrivateMessageOnly` | ||||||
|  |     that is inherited from :exc:`.CheckFailure`. | ||||||
|  |     """ | ||||||
|  |  | ||||||
|  |     def predicate(ctx): | ||||||
|  |         if ctx.guild is not None: | ||||||
|  |             raise PrivateMessageOnly('This command cannot be used in private messages.') | ||||||
|  |         return True | ||||||
|  |  | ||||||
|  |     return check(predicate) | ||||||
|  |  | ||||||
| def guild_only(): | def guild_only(): | ||||||
|     """A :func:`.check` that indicates this command must only be used in a |     """A :func:`.check` that indicates this command must only be used in a | ||||||
|     guild context only. Basically, no private messages are allowed when |     guild context only. Basically, no private messages are allowed when | ||||||
|   | |||||||
| @@ -28,15 +28,15 @@ from discord.errors import DiscordException | |||||||
|  |  | ||||||
|  |  | ||||||
| __all__ = ['CommandError', 'MissingRequiredArgument', 'BadArgument', | __all__ = ['CommandError', 'MissingRequiredArgument', 'BadArgument', | ||||||
|            'NoPrivateMessage', 'CheckFailure', 'CommandNotFound', |            'PrivateMessageOnly', 'NoPrivateMessage', 'CheckFailure', | ||||||
|            'DisabledCommand', 'CommandInvokeError', 'TooManyArguments', |            'CommandNotFound' ,'DisabledCommand', 'CommandInvokeError', | ||||||
|            'UserInputError', 'CommandOnCooldown', 'NotOwner', |            'TooManyArguments', 'UserInputError', 'CommandOnCooldown', | ||||||
|            'MissingPermissions', 'BotMissingPermissions', 'ConversionError', |            'NotOwner', 'MissingPermissions', 'BotMissingPermissions', | ||||||
|            'BadUnionArgument', 'ArgumentParsingError', |            'ConversionError', 'BadUnionArgument', 'ArgumentParsingError', | ||||||
|            'UnexpectedQuoteError', 'InvalidEndOfQuotedStringError', |            'UnexpectedQuoteError', 'InvalidEndOfQuotedStringError', | ||||||
|            'ExpectedClosingQuoteError', 'ExtensionError', 'ExtensionAlreadyLoaded', |            'ExpectedClosingQuoteError', 'ExtensionError', 'ExtensionAlreadyLoaded', | ||||||
|            'ExtensionNotLoaded', 'NoEntryPointError', 'ExtensionFailed', |            'ExtensionNotLoaded', 'NoEntryPointError', 'ExtensionFailed', | ||||||
|            'ExtensionNotFound' ] |            'ExtensionNotFound'] | ||||||
|  |  | ||||||
| class CommandError(DiscordException): | class CommandError(DiscordException): | ||||||
|     r"""The base exception type for all command related errors. |     r"""The base exception type for all command related errors. | ||||||
| @@ -118,6 +118,12 @@ class CheckFailure(CommandError): | |||||||
|     """Exception raised when the predicates in :attr:`.Command.checks` have failed.""" |     """Exception raised when the predicates in :attr:`.Command.checks` have failed.""" | ||||||
|     pass |     pass | ||||||
|  |  | ||||||
|  | class PrivateMessageOnly(CheckFailure): | ||||||
|  |     """Exception raised when an operation does not work outside of private | ||||||
|  |     message contexts. | ||||||
|  |     """ | ||||||
|  |     pass | ||||||
|  |  | ||||||
| class NoPrivateMessage(CheckFailure): | class NoPrivateMessage(CheckFailure): | ||||||
|     """Exception raised when an operation does not work in private message |     """Exception raised when an operation does not work in private message | ||||||
|     contexts. |     contexts. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user