From aabbd5a446214166298d2f5166065d9b213bfb3b Mon Sep 17 00:00:00 2001 From: Dante Dam <37320033+laggycomputer@users.noreply.github.com> Date: Tue, 9 Apr 2019 09:23:02 -0700 Subject: [PATCH] [commands] Added dm_only check Raises PrivateMessageOnly on failure. --- discord/ext/commands/core.py | 18 +++++++++++++++++- discord/ext/commands/errors.py | 18 ++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index e0357f386..8eda0a874 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -40,7 +40,7 @@ from .cog import Cog __all__ = ['Command', 'Group', 'GroupMixin', 'command', 'group', 'has_role', 'has_permissions', 'has_any_role', 'check', '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): @functools.wraps(coro) @@ -1433,6 +1433,22 @@ def bot_has_permissions(**perms): 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(): """A :func:`.check` that indicates this command must only be used in a guild context only. Basically, no private messages are allowed when diff --git a/discord/ext/commands/errors.py b/discord/ext/commands/errors.py index 00fa6e3f9..b98014e37 100644 --- a/discord/ext/commands/errors.py +++ b/discord/ext/commands/errors.py @@ -28,15 +28,15 @@ from discord.errors import DiscordException __all__ = ['CommandError', 'MissingRequiredArgument', 'BadArgument', - 'NoPrivateMessage', 'CheckFailure', 'CommandNotFound', - 'DisabledCommand', 'CommandInvokeError', 'TooManyArguments', - 'UserInputError', 'CommandOnCooldown', 'NotOwner', - 'MissingPermissions', 'BotMissingPermissions', 'ConversionError', - 'BadUnionArgument', 'ArgumentParsingError', + 'PrivateMessageOnly', 'NoPrivateMessage', 'CheckFailure', + 'CommandNotFound' ,'DisabledCommand', 'CommandInvokeError', + 'TooManyArguments', 'UserInputError', 'CommandOnCooldown', + 'NotOwner', 'MissingPermissions', 'BotMissingPermissions', + 'ConversionError', 'BadUnionArgument', 'ArgumentParsingError', 'UnexpectedQuoteError', 'InvalidEndOfQuotedStringError', 'ExpectedClosingQuoteError', 'ExtensionError', 'ExtensionAlreadyLoaded', 'ExtensionNotLoaded', 'NoEntryPointError', 'ExtensionFailed', - 'ExtensionNotFound' ] + 'ExtensionNotFound'] class CommandError(DiscordException): 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.""" pass +class PrivateMessageOnly(CheckFailure): + """Exception raised when an operation does not work outside of private + message contexts. + """ + pass + class NoPrivateMessage(CheckFailure): """Exception raised when an operation does not work in private message contexts.