aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
authordavid <[email protected]>2026-10-20 15:49:53 -0300
committerdavid <[email protected]>2026-10-20 15:49:53 -0300
commitd70458154894f432f45b96fe7139fe31af3f8124 (patch)
treee44c21e9d47ad63ffb3d800dc36f3d2c40f2c903 /bot.py
parent7958daa50debe471830aecf2c43af5054d682101 (diff)
downloadfaggot-chan-d70458154894f432f45b96fe7139fe31af3f8124.tar.gz
faggot-chan-d70458154894f432f45b96fe7139fe31af3f8124.zip
autocleanup of manually deleted images from media
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py6
1 files changed, 6 insertions, 0 deletions
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")