From e0b922c9493ff5ab01c39af0d1b9fe48d7816e73 Mon Sep 17 00:00:00 2001 From: Draper <27962761+Drapersniper@users.noreply.github.com> Date: Tue, 7 Jul 2020 19:08:06 +0100 Subject: [PATCH] Make `localwhitelist` check if caller will still be able to use bot after changes (#3903) * Check invokers theoretical perms in localwhitelist add before completing command * remove unnecessary code * add check to remove * ignore bot owner and server owner * Update core_commands.py * lets not crash shit --- redbot/core/core_commands.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index c39fe73bf..4d16ca4a7 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -2014,8 +2014,20 @@ class Core(commands.Cog, CoreLogic): return names = [getattr(u_or_r, "name", u_or_r) for u_or_r in users_or_roles] - uids = [getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles] - await self.bot._whiteblacklist_cache.add_to_whitelist(ctx.guild, uids) + uids = {getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles} + if not (ctx.guild.owner == ctx.author or await self.bot.is_owner(ctx.author)): + current_whitelist = set(await self.bot._whiteblacklist_cache.get_whitelist(ctx.guild)) + theoretical_whitelist = current_whitelist.union(uids) + ids = {i for i in (ctx.author.id, *(getattr(ctx.author, "_roles", [])))} + if ids.isdisjoint(theoretical_whitelist): + return await ctx.send( + _( + "I cannot allow you to do this, as it would " + "remove your ability to run commands, " + "please ensure to add yourself to the whitelist first." + ) + ) + await self.bot._whiteblacklist_cache.add_to_whitelist(ctx.guild, list(uids)) await ctx.send(_("{names} added to whitelist.").format(names=humanize_list(names))) @@ -2049,8 +2061,19 @@ class Core(commands.Cog, CoreLogic): return names = [getattr(u_or_r, "name", u_or_r) for u_or_r in users_or_roles] - uids = [getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles] - await self.bot._whiteblacklist_cache.remove_from_whitelist(ctx.guild, uids) + uids = {getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles} + if not (ctx.guild.owner == ctx.author or await self.bot.is_owner(ctx.author)): + current_whitelist = set(await self.bot._whiteblacklist_cache.get_whitelist(ctx.guild)) + theoretical_whitelist = current_whitelist - uids + ids = {i for i in (ctx.author.id, *(getattr(ctx.author, "_roles", [])))} + if theoretical_whitelist and ids.isdisjoint(theoretical_whitelist): + return await ctx.send( + _( + "I cannot allow you to do this, as it would " + "remove your ability to run commands." + ) + ) + await self.bot._whiteblacklist_cache.remove_from_whitelist(ctx.guild, list(uids)) await ctx.send( _("{names} removed from the local whitelist.").format(names=humanize_list(names))