From 3ebe64c7766f467dac1b19ae28b3b91e709d24d1 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 15 Jan 2016 17:24:27 -0500 Subject: [PATCH] [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. --- discord/ext/commands/core.py | 40 ++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index dc23daf64..f5bb81aa5 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)