[commands] Add guild-based permission checks
This commit is contained in:
parent
1b9108ffd7
commit
7972570eb6
@ -56,6 +56,8 @@ __all__ = (
|
||||
'guild_only',
|
||||
'is_owner',
|
||||
'is_nsfw',
|
||||
'has_guild_permissions',
|
||||
'bot_has_guild_permissions'
|
||||
)
|
||||
|
||||
def wrap_callback(coro):
|
||||
@ -1497,6 +1499,9 @@ def has_permissions(**perms):
|
||||
"""A :func:`.check` that is added that checks if the member has all of
|
||||
the permissions necessary.
|
||||
|
||||
Note that this check operates on the current channel permissions, not the
|
||||
guild wide permissions.
|
||||
|
||||
The permissions passed in must be exactly like the properties shown under
|
||||
:class:`.discord.Permissions`.
|
||||
|
||||
@ -1553,6 +1558,49 @@ def bot_has_permissions(**perms):
|
||||
|
||||
return check(predicate)
|
||||
|
||||
def has_guild_permissions(**perms):
|
||||
"""Similar to :func:`.has_permissions`, but operates on guild wide
|
||||
permissions instead of the current channel permissions.
|
||||
|
||||
If this check is called in a DM context, it will raise an
|
||||
exception, :exc:`.NoPrivateMessage`.
|
||||
|
||||
.. versionadded:: 1.3.0
|
||||
"""
|
||||
def predicate(ctx):
|
||||
if not ctx.guild:
|
||||
raise NoPrivateMessage
|
||||
|
||||
permissions = ctx.author.guild_permissions
|
||||
missing = [perm for perm, value in perms.items() if getattr(permissions, perm, None) != value]
|
||||
|
||||
if not missing:
|
||||
return True
|
||||
|
||||
raise MissingPermissions(missing)
|
||||
|
||||
return check(predicate)
|
||||
|
||||
def bot_has_guild_permissions(**perms):
|
||||
"""Similar to :func:`.has_guild_permissions`, but checks the bot
|
||||
members guild permissions.
|
||||
|
||||
.. versionadded:: 1.3.0
|
||||
"""
|
||||
def predicate(ctx):
|
||||
if not ctx.guild:
|
||||
raise NoPrivateMessage
|
||||
|
||||
permissions = ctx.me.guild_permissions
|
||||
missing = [perm for perm, value in perms.items() if getattr(permissions, perm, None) != value]
|
||||
|
||||
if not missing:
|
||||
return True
|
||||
|
||||
raise BotMissingPermissions(missing)
|
||||
|
||||
return check(predicate)
|
||||
|
||||
def dm_only():
|
||||
"""A :func:`.check` that indicates this command must only be used in a
|
||||
DM context. Only private messages are allowed when
|
||||
|
Loading…
x
Reference in New Issue
Block a user