async def add_guild_options(self, guild_id: int, options: Options) -> None: """ Set a guild's options to a custom set, rather then the base level set used and defined in ASH initialization Warnings -------- If using/modifying ``AntiSpamHandler.options`` to give to this method you will **also** be modifying the overall options. To get an options item you can modify freely call :py:meth:`antispam.AntiSpamHandler.get_options` this method will give you an instance of the current options you are free to modify however you like. Notes ===== This will override any current settings, if you wish to continue using existing settings and merely change some I suggest using the ``get_options`` method first and then giving those values back to this method with the changed arguments """ if not isinstance(options, Options): raise ValueError("Expected options of type Options") try: guild = await self.cache.get_guild(guild_id=guild_id) except GuildNotFound: log.warning( "I cannot ensure I have permissions to kick/ban ban people in Guild(id=%s)", guild_id, ) guild = Guild(id=guild_id, options=options) await self.cache.set_guild(guild) log.info("Created Guild(id=%s)", guild.id) else: guild.options = options log.info("Set custom options for guild(%s)", guild_id)
async def clean_cache(self, strict=False) -> None: """ Cleans the internal cache, pruning any old/un-needed entries. Non Strict mode: - Member deletion criteria: - warn_count == default - kick_count == default - duplicate_counter == default - duplicate_channel_counter_dict == default - addons dict == default - Also must have no active messages after cleaning. - Guild deletion criteria: - options are not custom - log_channel_id is not set - addons dict == default - Also must have no members stored Strict mode: - Member deletion criteria - Has no active messages - Guild deletion criteria - Does not have custom options - log_channel_id is not set - Has no active members Parameters ---------- strict : bool Toggles the above Notes ----- This is expensive, and likely only required to be run every so often depending on how high traffic your bot is. """ # In a nutshell, # Get entire cache and loop over # Build a list of 'still valid' cache entries # Drop the previous cache # Insert the new list of 'still valid' entries, this is now the cache # Ideally I don't want to load this cache into memory cache = [] async for guild in self.cache.get_all_guilds(): new_guild = Guild(guild.id) for member in guild.members.values(): FactoryBuilder.clean_old_messages(member, get_aware_time(), self.options) if strict and len(member.messages) != 0: new_guild.members[member.id] = member elif (len(member.messages) != 0 or member.kick_count != 0 or member.warn_count != 0 or member.duplicate_counter != 1 or bool(member.duplicate_channel_counter_dict) or bool(member.addons)): new_guild.members[member.id] = member # Clean guild predicate = (guild.options != Options() or guild.log_channel_id is not None or bool(new_guild.members)) if strict and predicate: new_guild.options = guild.options new_guild.log_channel_id = guild.log_channel_id cache.append(new_guild) elif predicate or bool(guild.addons): new_guild.addons = guild.addons new_guild.options = guild.options new_guild.log_channel_id = guild.log_channel_id cache.append(new_guild) await self.cache.drop() for guild in cache: await self.cache.set_guild(guild) log.info("Cleaned the internal cache")