From d957e44e1e87dab1bd34b1b9ea11d5d6e000b115 Mon Sep 17 00:00:00 2001 From: TrustyJAID Date: Sat, 21 Mar 2020 11:14:49 -0600 Subject: [PATCH] Fix issues with the black/whitelist cache (#3643) * Fix issues with the black/whitelist cache * Address review * address review * or --- redbot/core/settings_caches.py | 58 ++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/redbot/core/settings_caches.py b/redbot/core/settings_caches.py index 63d0de606..07032f406 100644 --- a/redbot/core/settings_caches.py +++ b/redbot/core/settings_caches.py @@ -130,7 +130,7 @@ class WhitelistBlacklistManager: ret = self._cached_whitelist[gid].copy() else: 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: ret = [] else: @@ -143,26 +143,31 @@ class WhitelistBlacklistManager: async def add_to_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]): gid: Optional[int] = guild.id if guild else None role_or_user = role_or_user or [] - if not isinstance(role_or_user, list) and not all( - isinstance(r_or_u, str) for r_or_u in role_or_user + if not isinstance(role_or_user, list) or not all( + isinstance(r_or_u, int) for r_or_u in role_or_user ): raise TypeError("Whitelisted objects must be a list of ints") + 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: - if obj_id not in self._cached_whitelist: + if obj_id not in self._cached_whitelist[gid]: self._cached_whitelist[gid].append(obj_id) async with self._config.whitelist() as curr_list: curr_list.append(obj_id) 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: - if obj_id not in self._cached_whitelist: + if obj_id not in self._cached_whitelist[gid]: self._cached_whitelist[gid].append(obj_id) async with self._config.guild_from_id(gid).whitelist() as curr_list: curr_list.append(obj_id) async def clear_whitelist(self, guild: Optional[discord.Guild] = None): gid: Optional[int] = guild.id if guild else None - self._cached_whitelist[gid] + self._cached_whitelist[gid] = [] if gid is None: await self._config.whitelist.clear() else: @@ -171,19 +176,24 @@ class WhitelistBlacklistManager: async def remove_from_whitelist(self, guild: Optional[discord.Guild], role_or_user: List[int]): gid: Optional[int] = guild.id if guild else None role_or_user = role_or_user or [] - if not isinstance(role_or_user, list) and not all( - isinstance(r_or_u, str) for r_or_u in role_or_user + if not isinstance(role_or_user, list) or not all( + isinstance(r_or_u, int) for r_or_u in role_or_user ): raise TypeError("Whitelisted objects must be a list of ints") + 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: - if obj_id in self._cached_whitelist: + if obj_id in self._cached_whitelist[gid]: self._cached_whitelist[gid].remove(obj_id) async with self._config.whitelist() as curr_list: curr_list.remove(obj_id) 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: - if obj_id not in self._cached_whitelist: + if obj_id in self._cached_whitelist[gid]: self._cached_whitelist[gid].remove(obj_id) async with self._config.guild_from_id(gid).whitelist() as curr_list: curr_list.remove(obj_id) @@ -197,7 +207,7 @@ class WhitelistBlacklistManager: ret = self._cached_blacklist[gid].copy() else: 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: ret = [] else: @@ -210,26 +220,30 @@ class WhitelistBlacklistManager: async def add_to_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]): gid: Optional[int] = guild.id if guild else None role_or_user = role_or_user or [] - if not isinstance(role_or_user, list) and not all( - isinstance(r_or_u, str) for r_or_u in role_or_user + if not isinstance(role_or_user, list) or not all( + isinstance(r_or_u, int) for r_or_u in role_or_user ): raise TypeError("Blacklisted objects must be a list of ints") 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: - if obj_id not in self._cached_blacklist: + if obj_id not in self._cached_blacklist[gid]: self._cached_blacklist[gid].append(obj_id) async with self._config.blacklist() as curr_list: curr_list.append(obj_id) 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: - if obj_id not in self._cached_blacklist: + if obj_id not in self._cached_blacklist[gid]: self._cached_blacklist[gid].append(obj_id) async with self._config.guild_from_id(gid).blacklist() as curr_list: curr_list.append(obj_id) async def clear_blacklist(self, guild: Optional[discord.Guild] = None): gid: Optional[int] = guild.id if guild else None - self._cached_whitelist[gid] + self._cached_blacklist[gid] = [] if gid is None: await self._config.blacklist.clear() else: @@ -238,19 +252,23 @@ class WhitelistBlacklistManager: async def remove_from_blacklist(self, guild: Optional[discord.Guild], role_or_user: List[int]): gid: Optional[int] = guild.id if guild else None role_or_user = role_or_user or [] - if not isinstance(role_or_user, list) and not all( - isinstance(r_or_u, str) for r_or_u in role_or_user + if not isinstance(role_or_user, list) or not all( + isinstance(r_or_u, int) for r_or_u in role_or_user ): raise TypeError("Blacklisted objects must be a list of ints") 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: - if obj_id in self._cached_blacklist: + if obj_id in self._cached_blacklist[gid]: self._cached_blacklist[gid].remove(obj_id) async with self._config.blacklist() as curr_list: curr_list.remove(obj_id) 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: - if obj_id not in self._cached_blacklist: + if obj_id in self._cached_blacklist[gid]: self._cached_blacklist[gid].remove(obj_id) async with self._config.guild_from_id(gid).blacklist() as curr_list: curr_list.remove(obj_id)