Reduce config calls when changing white/blacklist and use sets for their cache (#3910)

* optimise use of config ctx manager to reduce calls to config

* fine you potato

* since jack said yes i'll abuse it

* Apply suggestions from code review

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* difference_update and update

* Apply suggestions from code review

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/core/settings_caches.py

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* one last tweak

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Draper
2020-07-08 18:25:14 +01:00
committed by GitHub
parent 14349d0649
commit d30e83b5fc
2 changed files with 73 additions and 95 deletions

View File

@@ -1877,7 +1877,7 @@ class Core(commands.Cog, CoreLogic):
await ctx.send_help()
return
uids = [getattr(user, "id", user) for user in users]
uids = {getattr(user, "id", user) for user in users}
await self.bot._whiteblacklist_cache.add_to_whitelist(None, uids)
await ctx.send(_("Users added to whitelist."))
@@ -1909,7 +1909,7 @@ class Core(commands.Cog, CoreLogic):
await ctx.send_help()
return
uids = [getattr(user, "id", user) for user in users]
uids = {getattr(user, "id", user) for user in users}
await self.bot._whiteblacklist_cache.remove_from_whitelist(None, uids)
await ctx.send(_("Users have been removed from whitelist."))
@@ -1948,7 +1948,7 @@ class Core(commands.Cog, CoreLogic):
await ctx.send(_("You cannot blacklist an owner!"))
return
uids = [getattr(user, "id", user) for user in users]
uids = {getattr(user, "id", user) for user in users}
await self.bot._whiteblacklist_cache.add_to_blacklist(None, uids)
await ctx.send(_("User added to blacklist."))
@@ -1980,7 +1980,7 @@ class Core(commands.Cog, CoreLogic):
await ctx.send_help()
return
uids = [getattr(user, "id", user) for user in users]
uids = {getattr(user, "id", user) for user in users}
await self.bot._whiteblacklist_cache.remove_from_blacklist(None, uids)
await ctx.send(_("Users have been removed from blacklist."))
@@ -2016,7 +2016,7 @@ class Core(commands.Cog, CoreLogic):
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}
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))
current_whitelist = 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):
@@ -2027,7 +2027,7 @@ class Core(commands.Cog, CoreLogic):
"please ensure to add yourself to the whitelist first."
)
)
await self.bot._whiteblacklist_cache.add_to_whitelist(ctx.guild, list(uids))
await self.bot._whiteblacklist_cache.add_to_whitelist(ctx.guild, uids)
await ctx.send(_("{names} added to whitelist.").format(names=humanize_list(names)))
@@ -2063,7 +2063,7 @@ class Core(commands.Cog, CoreLogic):
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}
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))
current_whitelist = 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):
@@ -2073,7 +2073,7 @@ class Core(commands.Cog, CoreLogic):
"remove your ability to run commands."
)
)
await self.bot._whiteblacklist_cache.remove_from_whitelist(ctx.guild, list(uids))
await self.bot._whiteblacklist_cache.remove_from_whitelist(ctx.guild, uids)
await ctx.send(
_("{names} removed from the local whitelist.").format(names=humanize_list(names))
@@ -2119,7 +2119,7 @@ class Core(commands.Cog, CoreLogic):
await ctx.send(_("You cannot blacklist a bot owner!"))
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]
uids = {getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles}
await self.bot._whiteblacklist_cache.add_to_blacklist(ctx.guild, uids)
await ctx.send(
@@ -2156,7 +2156,7 @@ 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]
uids = {getattr(u_or_r, "id", u_or_r) for u_or_r in users_or_roles}
await self.bot._whiteblacklist_cache.remove_from_blacklist(ctx.guild, uids)
await ctx.send(