[commands] Added dm_only check

Raises PrivateMessageOnly on failure.
This commit is contained in:
Dante Dam
2019-04-09 09:23:02 -07:00
committed by Rapptz
parent 50dcdac5ca
commit aabbd5a446
2 changed files with 29 additions and 7 deletions

View File

@ -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