mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Avoid potential memory leak in Filter cog (#5578)
This commit is contained in:
parent
0338e8e0a8
commit
eeffbf8231
@ -2,7 +2,7 @@ import asyncio
|
||||
import discord
|
||||
import re
|
||||
from datetime import timezone
|
||||
from typing import Union, Set, Literal
|
||||
from typing import Union, Set, Literal, Optional
|
||||
|
||||
from redbot.core import checks, Config, modlog, commands
|
||||
from redbot.core.bot import Red
|
||||
@ -346,12 +346,14 @@ class Filter(commands.Cog):
|
||||
else:
|
||||
await ctx.send(_("Names and nicknames will now be filtered."))
|
||||
|
||||
def invalidate_cache(self, guild: discord.Guild, channel: discord.TextChannel = None):
|
||||
def invalidate_cache(
|
||||
self, guild: discord.Guild, channel: Optional[discord.TextChannel] = None
|
||||
) -> None:
|
||||
""" Invalidate a cached pattern"""
|
||||
self.pattern_cache.pop((guild, channel), None)
|
||||
self.pattern_cache.pop((guild.id, channel and channel.id), None)
|
||||
if channel is None:
|
||||
for keyset in list(self.pattern_cache.keys()): # cast needed, no remove
|
||||
if guild in keyset:
|
||||
if guild.id == keyset[0]:
|
||||
self.pattern_cache.pop(keyset, None)
|
||||
|
||||
async def add_to_filter(
|
||||
@ -408,7 +410,7 @@ class Filter(commands.Cog):
|
||||
hits: Set[str] = set()
|
||||
|
||||
try:
|
||||
pattern = self.pattern_cache[(guild, channel)]
|
||||
pattern = self.pattern_cache[(guild.id, channel and channel.id)]
|
||||
except KeyError:
|
||||
word_list = set(await self.config.guild(guild).filter())
|
||||
if channel:
|
||||
@ -421,7 +423,7 @@ class Filter(commands.Cog):
|
||||
else:
|
||||
pattern = None
|
||||
|
||||
self.pattern_cache[(guild, channel)] = pattern
|
||||
self.pattern_cache[(guild.id, channel and channel.id)] = pattern
|
||||
|
||||
if pattern:
|
||||
hits |= set(pattern.findall(text))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user