aboutsummaryrefslogtreecommitdiff
path: root/discord/ext/commands
diff options
context:
space:
mode:
authorRapptz <[email protected]>2016-01-15 17:24:27 -0500
committerRapptz <[email protected]>2016-01-15 17:24:27 -0500
commit3ebe64c7766f467dac1b19ae28b3b91e709d24d1 (patch)
tree294faa515dcf81c6b9d88121a663f9f45f007b0d /discord/ext/commands
parentUse Queue instead of LifoQueue in LogsFromIterator. (diff)
downloaddiscord.py-3ebe64c7766f467dac1b19ae28b3b91e709d24d1.tar.xz
discord.py-3ebe64c7766f467dac1b19ae28b3b91e709d24d1.zip
[commands] Add checks for checking bot roles and permissions.
There was a bug with has_permissions that checked the bot's permissions instead of the message author which was also corrected. The docstring itself hinted that it checked for the author rather than the bot.
Diffstat (limited to 'discord/ext/commands')
-rw-r--r--discord/ext/commands/core.py40
1 files changed, 38 insertions, 2 deletions
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py
index dc23daf6..f5bb81aa 100644
--- a/discord/ext/commands/core.py
+++ b/discord/ext/commands/core.py
@@ -782,8 +782,44 @@ def has_permissions(**perms):
def predicate(ctx):
msg = ctx.message
ch = msg.channel
- me = msg.server.me if not ch.is_private else ctx.bot.user
- permissions = ch.permissions_for(me)
+ permissions = ch.permissions_for(msg.author)
return all(getattr(permissions, perm, None) == value for perm, value in perms.items())
return check(predicate)
+
+def bot_has_role(name):
+ """Similar to :func:`has_role` except checks if the bot itself has the
+ role.
+ """
+ def predicate(ctx):
+ ch = ctx.message.channel
+ if ch.is_private:
+ return False
+ me = ch.server.me
+ role = discord.utils.get(me.roles, name=name)
+ return role is not None
+ return check(predicate)
+
+def bot_has_any_role(*names):
+ """Similar to :func:`has_any_role` except checks if the bot itself has
+ any of the roles listed.
+ """
+ def predicate(ctx):
+ ch = ctx.message.channel
+ if ch.is_private:
+ return False
+ me = ch.server.me
+ getter = functools.partial(discord.utils.get, me.roles)
+ return any(getter(name=name) is not None for name in names)
+ return check(predicate)
+
+def bot_has_permissions(**perms):
+ """Similar to :func:`has_permissions` except checks if the bot itself has
+ the permissions listed.
+ """
+ def predicate(ctx):
+ ch = ctx.message.channel
+ me = msg.server.me if not ch.is_private else ctx.bot.user
+ permissions = ch.permissions_for(msg.author)
+ return all(getattr(permissions, perm, None) == value for perm, value in perms.items())
+ return check(predicate)