Fix issues with the black/whitelist cache (#3643)

* Fix issues with the black/whitelist cache

* Address review

* address review

* or
This commit is contained in:
TrustyJAID 2020-03-21 11:14:49 -06:00 committed by GitHub
parent 15e3437001
commit d957e44e1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -130,7 +130,7 @@ class WhitelistBlacklistManager:
ret = self._cached_whitelist[gid].copy() ret = self._cached_whitelist[gid].copy()
else: else:
if gid is not None: if gid is not None:
ret = await self._config.guild_from_id(gid).whitelsit() ret = await self._config.guild_from_id(gid).whitelist()
if not ret: if not ret:
ret = [] ret = []
else: else:
@ -143,26 +143,31 @@ class WhitelistBlacklistManager:
async def add_to_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]): async def add_to_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
role_or_user = role_or_user or [] role_or_user = role_or_user or []
if not isinstance(role_or_user, list) and not all( if not isinstance(role_or_user, list) or not all(
isinstance(r_or_u, str) for r_or_u in role_or_user isinstance(r_or_u, int) for r_or_u in role_or_user
): ):
raise TypeError("Whitelisted objects must be a list of ints") raise TypeError("Whitelisted objects must be a list of ints")
if gid is None: if gid is None:
if gid not in self._cached_whitelist:
self._cached_whitelist[gid] = await self._config.whitelist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_whitelist: if obj_id not in self._cached_whitelist[gid]:
self._cached_whitelist[gid].append(obj_id) self._cached_whitelist[gid].append(obj_id)
async with self._config.whitelist() as curr_list: async with self._config.whitelist() as curr_list:
curr_list.append(obj_id) curr_list.append(obj_id)
else: else:
if gid not in self._cached_whitelist:
self._cached_whitelist[gid] = await self._config.guild_from_id(gid).whitelist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_whitelist: if obj_id not in self._cached_whitelist[gid]:
self._cached_whitelist[gid].append(obj_id) self._cached_whitelist[gid].append(obj_id)
async with self._config.guild_from_id(gid).whitelist() as curr_list: async with self._config.guild_from_id(gid).whitelist() as curr_list:
curr_list.append(obj_id) curr_list.append(obj_id)
async def clear_whitelist(self, guild: Optional[discord.Guild] = None): async def clear_whitelist(self, guild: Optional[discord.Guild] = None):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
self._cached_whitelist[gid] self._cached_whitelist[gid] = []
if gid is None: if gid is None:
await self._config.whitelist.clear() await self._config.whitelist.clear()
else: else:
@ -171,19 +176,24 @@ class WhitelistBlacklistManager:
async def remove_from_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]): async def remove_from_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
role_or_user = role_or_user or [] role_or_user = role_or_user or []
if not isinstance(role_or_user, list) and not all( if not isinstance(role_or_user, list) or not all(
isinstance(r_or_u, str) for r_or_u in role_or_user isinstance(r_or_u, int) for r_or_u in role_or_user
): ):
raise TypeError("Whitelisted objects must be a list of ints") raise TypeError("Whitelisted objects must be a list of ints")
if gid is None: if gid is None:
if gid not in self._cached_whitelist:
self._cached_whitelist[gid] = await self._config.whitelist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id in self._cached_whitelist: if obj_id in self._cached_whitelist[gid]:
self._cached_whitelist[gid].remove(obj_id) self._cached_whitelist[gid].remove(obj_id)
async with self._config.whitelist() as curr_list: async with self._config.whitelist() as curr_list:
curr_list.remove(obj_id) curr_list.remove(obj_id)
else: else:
if gid not in self._cached_whitelist:
self._cached_whitelist[gid] = await self._config.guild_from_id(gid).whitelist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_whitelist: if obj_id in self._cached_whitelist[gid]:
self._cached_whitelist[gid].remove(obj_id) self._cached_whitelist[gid].remove(obj_id)
async with self._config.guild_from_id(gid).whitelist() as curr_list: async with self._config.guild_from_id(gid).whitelist() as curr_list:
curr_list.remove(obj_id) curr_list.remove(obj_id)
@ -197,7 +207,7 @@ class WhitelistBlacklistManager:
ret = self._cached_blacklist[gid].copy() ret = self._cached_blacklist[gid].copy()
else: else:
if gid is not None: if gid is not None:
ret = await self._config.guild_from_id(gid).whitelsit() ret = await self._config.guild_from_id(gid).blacklist()
if not ret: if not ret:
ret = [] ret = []
else: else:
@ -210,26 +220,30 @@ class WhitelistBlacklistManager:
async def add_to_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]): async def add_to_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
role_or_user = role_or_user or [] role_or_user = role_or_user or []
if not isinstance(role_or_user, list) and not all( if not isinstance(role_or_user, list) or not all(
isinstance(r_or_u, str) for r_or_u in role_or_user isinstance(r_or_u, int) for r_or_u in role_or_user
): ):
raise TypeError("Blacklisted objects must be a list of ints") raise TypeError("Blacklisted objects must be a list of ints")
if gid is None: if gid is None:
if gid not in self._cached_blacklist:
self._cached_blacklist[gid] = await self._config.blacklist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_blacklist: if obj_id not in self._cached_blacklist[gid]:
self._cached_blacklist[gid].append(obj_id) self._cached_blacklist[gid].append(obj_id)
async with self._config.blacklist() as curr_list: async with self._config.blacklist() as curr_list:
curr_list.append(obj_id) curr_list.append(obj_id)
else: else:
if gid not in self._cached_blacklist:
self._cached_blacklist[gid] = self._config.guild_from_id(gid).blacklist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_blacklist: if obj_id not in self._cached_blacklist[gid]:
self._cached_blacklist[gid].append(obj_id) self._cached_blacklist[gid].append(obj_id)
async with self._config.guild_from_id(gid).blacklist() as curr_list: async with self._config.guild_from_id(gid).blacklist() as curr_list:
curr_list.append(obj_id) curr_list.append(obj_id)
async def clear_blacklist(self, guild: Optional[discord.Guild] = None): async def clear_blacklist(self, guild: Optional[discord.Guild] = None):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
self._cached_whitelist[gid] self._cached_blacklist[gid] = []
if gid is None: if gid is None:
await self._config.blacklist.clear() await self._config.blacklist.clear()
else: else:
@ -238,19 +252,23 @@ class WhitelistBlacklistManager:
async def remove_from_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]): async def remove_from_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]):
gid: Optional[int] = guild.id if guild else None gid: Optional[int] = guild.id if guild else None
role_or_user = role_or_user or [] role_or_user = role_or_user or []
if not isinstance(role_or_user, list) and not all( if not isinstance(role_or_user, list) or not all(
isinstance(r_or_u, str) for r_or_u in role_or_user isinstance(r_or_u, int) for r_or_u in role_or_user
): ):
raise TypeError("Blacklisted objects must be a list of ints") raise TypeError("Blacklisted objects must be a list of ints")
if gid is None: if gid is None:
if gid not in self._cached_blacklist:
self._cached_blacklist[gid] = await self._config.blacklist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id in self._cached_blacklist: if obj_id in self._cached_blacklist[gid]:
self._cached_blacklist[gid].remove(obj_id) self._cached_blacklist[gid].remove(obj_id)
async with self._config.blacklist() as curr_list: async with self._config.blacklist() as curr_list:
curr_list.remove(obj_id) curr_list.remove(obj_id)
else: else:
if gid not in self._cached_blacklist:
self._cached_blacklist[gid] = self._config.guild_from_id(gid).blacklist()
for obj_id in role_or_user: for obj_id in role_or_user:
if obj_id not in self._cached_blacklist: if obj_id in self._cached_blacklist[gid]:
self._cached_blacklist[gid].remove(obj_id) self._cached_blacklist[gid].remove(obj_id)
async with self._config.guild_from_id(gid).blacklist() as curr_list: async with self._config.guild_from_id(gid).blacklist() as curr_list:
curr_list.remove(obj_id) curr_list.remove(obj_id)