diff --git a/README.md b/README.md index caac0c2..c130ef5 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ python3 src/main.py * **v1.13** * improved scan `%words` + * remove old and unused logs at start and guild leaving * **v1.12** * more scans: `%words` * concurrent `fast` analysis diff --git a/src/logs/guild_logs.py b/src/logs/guild_logs.py index 88d8823..6fe0201 100644 --- a/src/logs/guild_logs.py +++ b/src/logs/guild_logs.py @@ -15,6 +15,7 @@ from utils import code_message, delta, deltas LOG_DIR = "logs" +LOG_EXT = ".logz" current_analysis = [] current_analysis_lock = threading.Lock() @@ -23,7 +24,10 @@ current_analysis_lock = threading.Lock() ALREADY_RUNNING = -100 CANCELLED = -200 +# 5 minutes, assume 'fast' arg MIN_MODIFICATION_TIME = 5 * 60 +# ~6 months, remove log file +MAX_MODIFICATION_TIME = 6 * 30.5 * 24 * 60 * 60 class Worker: @@ -54,7 +58,7 @@ class GuildLogs: def __init__(self, guild: discord.Guild): self.id = guild.id self.guild = guild - self.log_file = os.path.join(LOG_DIR, f"{guild.id}.logz") + self.log_file = os.path.join(LOG_DIR, f"{guild.id}{LOG_EXT}") self.channels = {} self.locked = False @@ -322,3 +326,21 @@ class GuildLogs: f"No cancellable analysis are currently running on this server", reference=message, ) + + @staticmethod + def check_logs(guilds: List[discord.Guild]): + logging.info(f"checking logs...") + guild_ids = [str(guild.id) for guild in guilds] + for item in os.listdir(LOG_DIR): + path = os.path.join(LOG_DIR, item) + name, ext = os.path.splitext(item) + if os.path.isfile(path) and ext == LOG_EXT: + if ( + name in guild_ids + and (time.time() - os.path.getmtime(path)) > MAX_MODIFICATION_TIME + ): + logging.info(f"> removing old log '{path}'") + os.unlink(path) + elif name not in guild_ids: + logging.info(f"> removing unused log '{path}'") + os.unlink(path) diff --git a/src/main.py b/src/main.py index 4ac9b5f..69f6b0a 100644 --- a/src/main.py +++ b/src/main.py @@ -39,6 +39,20 @@ bot = Bot( bot.log_calls = True + +async def on_ready(): + GuildLogs.check_logs(bot.client.guilds) + return True + + +async def on_guild_remove(): + GuildLogs.check_logs(bot.client.guilds) + return True + + +bot.register_event(on_ready) +bot.register_event(on_guild_remove) + bot.register_command( "(cancel|stop)", GuildLogs.cancel,