Skip to content

Commit 021a7bd

Browse files
committed
fix: unsnooze bug
1 parent 19ff839 commit 021a7bd

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

cogs/modmail.py

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,9 @@ async def snooze(self, ctx, *, after: UserFriendlyTime = None):
22852285
snooze_for = max_snooze
22862286
else:
22872287
snooze_for = max_snooze
2288-
now = datetime.utcnow()
2288+
2289+
# Storing snooze_start and snooze_for in the log entry
2290+
now = datetime.now(timezone.utc)
22892291
await self.bot.api.logs.update_one(
22902292
{"recipient.id": str(thread.id)},
22912293
{"$set": {"snooze_start": now.isoformat(), "snooze_for": snooze_for}},
@@ -2296,7 +2298,7 @@ async def snooze(self, ctx, *, after: UserFriendlyTime = None):
22962298
color=self.bot.error_color,
22972299
)
22982300
await ctx.send(embed=embed)
2299-
ok = await thread.snooze(moderator=ctx.author)
2301+
ok = await thread.snooze(moderator=ctx.author, snooze_for=snooze_for)
23002302
if ok:
23012303
logging.info(
23022304
f"[SNOOZE] Thread for {getattr(thread.recipient, 'id', None)} snoozed for {snooze_for}s."
@@ -2341,6 +2343,13 @@ async def unsnooze(self, ctx, *, user: str = None):
23412343
await ctx.send("This thread is not snoozed.")
23422344
logging.info(f"[UNSNOOZE] Thread for {getattr(thread.recipient, 'id', None)} is not snoozed.")
23432345
return
2346+
2347+
# Manually fetch snooze_data if the thread object doesn't have it
2348+
if not thread.snooze_data:
2349+
log_entry = await self.bot.api.logs.find_one({"recipient.id": str(thread.id), "snoozed": True})
2350+
if log_entry:
2351+
thread.snooze_data = log_entry.get("snooze_data")
2352+
23442353
ok = await thread.restore_from_snooze()
23452354
if ok:
23462355
self.bot.threads.cache[thread.id] = thread
@@ -2360,37 +2369,44 @@ async def snoozed(self, ctx):
23602369
"""
23612370
List all currently snoozed threads/users.
23622371
"""
2363-
snoozed = await self.bot.api.logs.find({"snoozed": True}).to_list(None)
2364-
if not snoozed:
2372+
snoozed_threads = [thread for thread in self.bot.threads.cache.values() if thread.snoozed]
2373+
if not snoozed_threads:
23652374
await ctx.send("No threads are currently snoozed.")
23662375
return
2376+
23672377
lines = []
2368-
now = datetime.utcnow()
2369-
for entry in snoozed:
2370-
user = entry.get("recipient", {}).get("name", "Unknown")
2371-
user_id = entry.get("recipient", {}).get("id", "?")
2372-
since = entry.get("snooze_start")
2373-
duration = entry.get("snooze_for")
2374-
if since:
2375-
try:
2376-
since_dt = datetime.fromisoformat(since)
2377-
since_str = f"<t:{int(since_dt.timestamp())}:R>" # Discord relative timestamp
2378-
except Exception as e:
2379-
since_str = "?"
2380-
logging.warning(f"[SNOOZED] Invalid snooze_start for {user_id}: {since} ({e})")
2381-
else:
2382-
since_str = "?"
2383-
logging.warning(f"[SNOOZED] Missing snooze_start for {user_id}")
2384-
if duration and since_str != "?":
2385-
try:
2386-
until_dt = datetime.fromisoformat(since) + timedelta(seconds=int(duration))
2387-
until_str = f"<t:{int(until_dt.timestamp())}:R>"
2388-
except Exception as e:
2389-
until_str = "?"
2390-
logging.warning(f"[SNOOZED] Invalid until time for {user_id}: {since} + {duration} ({e})")
2391-
else:
2392-
until_str = "?"
2378+
now = datetime.now(timezone.utc)
2379+
for thread in snoozed_threads:
2380+
user = thread.recipient.name if thread.recipient else "Unknown"
2381+
user_id = thread.id
2382+
2383+
since_str = "?"
2384+
until_str = "?"
2385+
2386+
if thread.snooze_data:
2387+
since = thread.snooze_data.get("snooze_start")
2388+
duration = thread.snooze_data.get("snooze_for")
2389+
2390+
if since:
2391+
try:
2392+
since_dt = datetime.fromisoformat(since)
2393+
since_str = f"<t:{int(since_dt.timestamp())}:R>" # Discord relative timestamp
2394+
except (ValueError, TypeError) as e:
2395+
logging.warning(f"[SNOOZED] Invalid snooze_start for {user_id}: {since} ({e})")
2396+
else:
2397+
logging.warning(f"[SNOOZED] Missing snooze_start for {user_id}")
2398+
2399+
if duration and since_str != "?":
2400+
try:
2401+
until_dt = datetime.fromisoformat(since) + timedelta(seconds=int(duration))
2402+
until_str = f"<t:{int(until_dt.timestamp())}:R>"
2403+
except (ValueError, TypeError) as e:
2404+
logging.warning(
2405+
f"[SNOOZED] Invalid until time for {user_id}: {since} + {duration} ({e})"
2406+
)
2407+
23932408
lines.append(f"- {user} (`{user_id}`) since {since_str}, until {until_str}")
2409+
23942410
await ctx.send("Snoozed threads:\n" + "\n".join(lines))
23952411

23962412
async def cog_load(self):

core/thread.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import traceback
99
import typing
1010
import warnings
11-
from datetime import timedelta
11+
from datetime import timedelta, datetime, timezone
1212
from types import SimpleNamespace
1313

1414
import isodate
@@ -130,7 +130,7 @@ def cancelled(self, flag: bool):
130130
for i in self.wait_tasks:
131131
i.cancel()
132132

133-
async def snooze(self, moderator=None, command_used=None):
133+
async def snooze(self, moderator=None, command_used=None, snooze_for=None):
134134
"""
135135
Save channel/category/position/messages to DB, mark as snoozed, delete channel.
136136
"""
@@ -151,6 +151,7 @@ async def snooze(self, moderator=None, command_used=None):
151151
if log_entry and "key" in log_entry:
152152
self.log_key = log_entry["key"]
153153

154+
now = datetime.now(timezone.utc)
154155
self.snooze_data = {
155156
"category_id": channel.category_id,
156157
"position": channel.position,
@@ -166,7 +167,6 @@ async def snooze(self, moderator=None, command_used=None):
166167
"attachments": [a.url for a in m.attachments],
167168
"embeds": [e.to_dict() for e in m.embeds],
168169
"created_at": m.created_at.isoformat(),
169-
# Only use 'mod_only' if this is an internal note (note command), safe check for embed author
170170
"type": (
171171
"mod_only"
172172
if (
@@ -182,7 +182,6 @@ async def snooze(self, moderator=None, command_used=None):
182182
"author_name": getattr(m.author, "name", None),
183183
}
184184
async for m in channel.history(limit=None, oldest_first=True)
185-
# Only include if not already internal/note, safe check for embed author
186185
if not (
187186
m.embeds
188187
and getattr(m.embeds[0], "author", None)
@@ -195,7 +194,9 @@ async def snooze(self, moderator=None, command_used=None):
195194
],
196195
"snoozed_by": getattr(moderator, "name", None) if moderator else None,
197196
"snooze_command": command_used,
198-
"log_key": self.log_key, # Preserve the log_key
197+
"log_key": self.log_key,
198+
"snooze_start": now.isoformat(),
199+
"snooze_for": snooze_for,
199200
}
200201
self.snoozed = True
201202
# Save to DB (robust: try recipient.id, then channel_id)
@@ -289,19 +290,19 @@ async def restore_from_snooze(self):
289290
if self.log_key:
290291
result = await self.bot.api.logs.update_one(
291292
{"key": self.log_key},
292-
{"$set": {"snoozed": False, "channel_id": str(channel.id)}, "$unset": {"snooze_data": ""}},
293+
{"$set": {"channel_id": str(channel.id)}, "$unset": {"snoozed": "", "snooze_data": ""}},
293294
)
294295
else:
295296
result = await self.bot.api.logs.update_one(
296297
{"recipient.id": str(self.id)},
297-
{"$set": {"snoozed": False, "channel_id": str(channel.id)}, "$unset": {"snooze_data": ""}},
298+
{"$set": {"channel_id": str(channel.id)}, "$unset": {"snoozed": "", "snooze_data": ""}},
298299
)
299300
if result.modified_count == 0:
300301
result = await self.bot.api.logs.update_one(
301302
{"channel_id": str(channel.id)},
302303
{
303-
"$set": {"snoozed": False, "channel_id": str(channel.id)},
304-
"$unset": {"snooze_data": ""},
304+
"$set": {"channel_id": str(channel.id)},
305+
"$unset": {"snoozed": "", "snooze_data": ""},
305306
},
306307
)
307308
import logging

0 commit comments

Comments
 (0)