From d70458154894f432f45b96fe7139fe31af3f8124 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 20 Oct 2026 15:49:53 -0300 Subject: [PATCH] autocleanup of manually deleted images from media --- bot.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bot.py b/bot.py index edf04c1..a012827 100644 --- a/bot.py +++ b/bot.py @@ -254,12 +254,15 @@ vector_db_ready = False # gets set to True after first rebuild so we don't keep saved_media = set() # file names inside MEDIA_DIR +kept_media_lines = [] with MEDIA_FILE.open("r", encoding="utf-8") as f: for line in f: if "saved media:" in line: name = line.split("saved media:", 1)[1].strip() if (MEDIA_DIR / name).is_file(): # old entries were urls (those expire), they don't match a file so they get dropped saved_media.add(name) + kept_media_lines.append(line) +MEDIA_FILE.write_text("".join(kept_media_lines), encoding="utf-8") # rewritten without the entries of deleted files async def steal_media(url: str, name: str): # discord cdn links are signed and die after a day or so, so we keep our own copy @@ -428,6 +431,9 @@ def sync_reactions(guild): if n not in data[key]: data[key][n] = "" changed = True + for n in [n for n in data["media"] if n not in saved_media]: # file got deleted, drop its entry too + del data["media"][n] + changed = True if changed: REACTIONS_FILE.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")