Skip to content

Commit 15e6391

Browse files
committed
fix: cache for snooze timer
1 parent b60ddbe commit 15e6391

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

cogs/modmail.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,52 @@ class Modmail(commands.Cog):
2929

3030
def __init__(self, bot):
3131
self.bot = bot
32+
self._snoozed_cache = []
33+
self._auto_unsnooze_task = self.bot.loop.create_task(self.auto_unsnooze_task())
34+
35+
async def auto_unsnooze_task(self):
36+
await self.bot.wait_until_ready()
37+
last_db_query = 0
38+
while not self.bot.is_closed():
39+
now = datetime.now(timezone.utc)
40+
try:
41+
# Query DB every 2 minutes
42+
if (now.timestamp() - last_db_query) > 120:
43+
snoozed_threads = await self.bot.api.logs.find(
44+
{"snooze_until": {"$gte": now.isoformat()}}
45+
).to_list(None)
46+
self._snoozed_cache = snoozed_threads or []
47+
last_db_query = now.timestamp()
48+
# Check cache every 10 seconds
49+
to_unsnooze = []
50+
for thread_data in list(self._snoozed_cache):
51+
snooze_until = thread_data.get("snooze_until")
52+
thread_id = int(thread_data.get("recipient.id"))
53+
if snooze_until:
54+
try:
55+
dt = parser.isoparse(snooze_until)
56+
except Exception:
57+
continue
58+
if now >= dt:
59+
to_unsnooze.append(thread_data)
60+
for thread_data in to_unsnooze:
61+
thread_id = int(thread_data.get("recipient.id"))
62+
thread = self.bot.threads.cache.get(thread_id) or await self.bot.threads.find(
63+
id=thread_id
64+
)
65+
if thread and thread.snoozed:
66+
await thread.restore_from_snooze()
67+
logging.info(f"[AUTO-UNSNOOZE] Thread {thread_id} auto-unsnoozed.")
68+
try:
69+
channel = thread.channel
70+
if channel:
71+
await channel.send("⏰ This thread has been automatically unsnoozed.")
72+
except Exception:
73+
pass
74+
self._snoozed_cache.remove(thread_data)
75+
except Exception as e:
76+
logging.error(f"Error in auto_unsnooze_task: {e}")
77+
await asyncio.sleep(10)
3278

3379
def _resolve_user(self, user_str):
3480
"""Helper to resolve a user from mention, ID, or username."""

0 commit comments

Comments
 (0)