Skip to content

Commit dc1c4af

Browse files
authored
fix: internals
Signed-off-by: lorenzo132 <[email protected]>
1 parent edc9daa commit dc1c4af

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

core/clients.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ async def append_log(
648648
message: Message,
649649
*,
650650
message_id: str = "",
651+
log_key: str = None,
651652
channel_id: str = "",
652653
type_: str = "thread_message",
653654
) -> dict:
@@ -678,14 +679,31 @@ async def append_log(
678679
],
679680
}
680681

681-
return await self.logs.find_one_and_update(
682-
{"channel_id": channel_id}, {"$push": {"messages": data}}, return_document=True
683-
)
682+
# Use log_key if provided, fallback to channel_id for legacy support
683+
if log_key:
684+
return await self.logs.find_one_and_update(
685+
{"key": log_key}, {"$push": {"messages": data}}, return_document=True
686+
)
687+
else:
688+
return await self.logs.find_one_and_update(
689+
{"channel_id": channel_id}, {"$push": {"messages": data}}, return_document=True
690+
)
684691

685-
async def post_log(self, channel_id: Union[int, str], data: dict) -> dict:
686-
return await self.logs.find_one_and_update(
687-
{"channel_id": str(channel_id)}, {"$set": data}, return_document=True
688-
)
692+
async def get_log_by_key(self, log_key: str) -> dict:
693+
return await self.logs.find_one({"key": log_key})
694+
695+
async def post_log_by_key(self, log_key: str, data: dict) -> dict:
696+
return await self.logs.find_one_and_update({"key": log_key}, {"$set": data}, return_document=True)
697+
698+
async def post_log(
699+
self, channel_id: Union[int, str] = None, data: dict = None, log_key: str = None
700+
) -> dict:
701+
if log_key:
702+
return await self.post_log_by_key(log_key, data)
703+
else:
704+
return await self.logs.find_one_and_update(
705+
{"channel_id": str(channel_id)}, {"$set": data}, return_document=True
706+
)
689707

690708
async def search_closed_by(self, user_id: Union[int, str]):
691709
return await self.logs.find(

core/thread.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(
6969
# --- SNOOZE STATE ---
7070
self.snoozed = False # True if thread is snoozed
7171
self.snooze_data = None # Dict with channel/category/position/messages for restoration
72+
self.log_key = None # Unique log key for this thread
7273

7374
def __repr__(self):
7475
return f'Thread(recipient="{self.recipient or self.id}", channel={self.channel.id}, other_recipients={len(self._other_recipients)})'
@@ -342,11 +343,11 @@ async def setup(self, *, creator=None, category=None, initial_message=None):
342343
self._channel = channel
343344

344345
try:
345-
log_url, log_data = await asyncio.gather(
346-
self.bot.api.create_log_entry(recipient, channel, creator or recipient),
347-
self.bot.api.get_user_logs(recipient.id),
348-
)
349-
346+
# create_log_entry now returns the log key (URL)
347+
log_url = await self.bot.api.create_log_entry(recipient, channel, creator or recipient)
348+
# Extract the log key from the URL
349+
self.log_key = log_url.rstrip("/").split("/")[-1]
350+
log_data = await self.bot.api.get_user_logs(recipient.id)
350351
log_count = sum(1 for log in log_data if not log["open"])
351352
except Exception:
352353
logger.error("An error occurred while posting logs to the database.", exc_info=True)
@@ -583,7 +584,25 @@ async def _close(self, closer, silent=False, delete_channel=True, message=None,
583584
self.bot.config["notification_squad"].pop(str(self.id), None)
584585

585586
# Logging
586-
if self.channel:
587+
if self.log_key:
588+
log_data = await self.bot.api.post_log(
589+
log_key=self.log_key,
590+
data={
591+
"open": False,
592+
"title": match_title(self.channel.topic) if self.channel else None,
593+
"closed_at": str(discord.utils.utcnow()),
594+
"nsfw": self.channel.nsfw if self.channel else None,
595+
"close_message": message,
596+
"closer": {
597+
"id": str(closer.id),
598+
"name": closer.name,
599+
"discriminator": closer.discriminator,
600+
"avatar_url": closer.display_avatar.url,
601+
"mod": True,
602+
},
603+
},
604+
)
605+
elif self.channel:
587606
log_data = await self.bot.api.post_log(
588607
self.channel.id,
589608
{
@@ -608,9 +627,8 @@ async def _close(self, closer, silent=False, delete_channel=True, message=None,
608627
prefix = self.bot.config["log_url_prefix"].strip("/")
609628
if prefix == "NONE":
610629
prefix = ""
611-
log_url = (
612-
f"{self.bot.config['log_url'].strip('/')}{'/' + prefix if prefix else ''}/{log_data['key']}"
613-
)
630+
log_key = log_data.get("key") or self.log_key
631+
log_url = f"{self.bot.config['log_url'].strip('/')}{'/' + prefix if prefix else ''}/{log_key}"
614632

615633
if log_data["title"]:
616634
sneak_peak = log_data["title"]
@@ -967,7 +985,9 @@ async def note(
967985

968986
# Log as 'internal' type for logviewer visibility
969987
self.bot.loop.create_task(
970-
self.bot.api.append_log(message, message_id=msg.id, channel_id=self.channel.id, type_="internal")
988+
self.bot.api.append_log(
989+
message, message_id=msg.id, log_key=self.log_key, channel_id=self.channel.id, type_="internal"
990+
)
971991
)
972992

973993
return msg
@@ -1041,6 +1061,7 @@ async def reply(
10411061
self.bot.api.append_log(
10421062
message,
10431063
message_id=msg.id,
1064+
log_key=self.log_key,
10441065
channel_id=self.channel.id,
10451066
type_="anonymous" if anonymous else "thread_message",
10461067
)
@@ -1094,7 +1115,9 @@ async def send(
10941115
await self.wait_until_ready()
10951116

10961117
if not from_mod and not note:
1097-
self.bot.loop.create_task(self.bot.api.append_log(message, channel_id=self.channel.id))
1118+
self.bot.loop.create_task(
1119+
self.bot.api.append_log(message, log_key=self.log_key, channel_id=self.channel.id)
1120+
)
10981121

10991122
destination = destination or self.channel
11001123

0 commit comments

Comments
 (0)