Skip to content

Commit e620a03

Browse files
committed
fix: not rely on mod_color as originally was made.
This will avoid crashes when the mod_color get changed.
1 parent 20af225 commit e620a03

File tree

1 file changed

+50
-99
lines changed

1 file changed

+50
-99
lines changed

core/thread.py

Lines changed: 50 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,33 @@ async def find_linked_messages(
13851385
if message1.embeds[0].footer.text.startswith("[PLAIN]"):
13861386
is_plain = True
13871387

1388+
if is_plain:
1389+
creation_time = message1.created_at
1390+
1391+
mod_tag = message1.embeds[0].footer.text.replace("[PLAIN]", "", 1).strip()
1392+
author_name = message1.embeds[0].author.name
1393+
desc = message1.embeds[0].description or ""
1394+
prefix = f"**{mod_tag} " if mod_tag else "**"
1395+
plain_content_expected = f"{prefix}{author_name}:** {desc}"
1396+
1397+
messages = [message1]
1398+
for user in self.recipients:
1399+
async for msg in user.history(limit=50, around=creation_time):
1400+
if abs((msg.created_at - creation_time).total_seconds()) > 15:
1401+
continue
1402+
if msg.author != self.bot.user:
1403+
continue
1404+
if msg.embeds:
1405+
continue
1406+
1407+
if msg.content == plain_content_expected:
1408+
messages.append(msg)
1409+
break
1410+
1411+
if len(messages) > 1:
1412+
return messages
1413+
raise ValueError("Linked Plain DM message not found.")
1414+
13881415
if not is_plain and not (
13891416
message1.embeds
13901417
and message1.embeds[0].author.url
@@ -1397,106 +1424,38 @@ async def find_linked_messages(
13971424
raise ValueError("Thread message not found.")
13981425

13991426
if message1.embeds[0].footer and "Internal Message" in message1.embeds[0].footer.text:
1400-
if not note:
1401-
logger.warning(
1402-
f"Message {message_id} is an internal message, but note deletion not requested."
1403-
)
1404-
raise ValueError("Thread message is an internal message, not a note.")
1427+
logger.warning(
1428+
f"Message {message_id} is an internal message, but note deletion not requested."
1429+
)
1430+
raise ValueError("Thread message is an internal message, not a note.")
14051431
# Internal bot-only message treated similarly; keep None sentinel
14061432
return message1, None
14071433

1408-
if (
1409-
not is_plain
1410-
and message1.embeds[0].color.value != self.bot.mod_color
1411-
and not (either_direction and message1.embeds[0].color.value == self.bot.recipient_color)
1412-
):
1413-
logger.warning("Message color does not match mod/recipient colors.")
1414-
raise ValueError("Thread message not found.")
1415-
else:
1416-
async for message1 in self.channel.history():
1417-
if (
1418-
message1.embeds
1419-
and (
1420-
(
1421-
message1.embeds[0].author.url
1422-
and message1.embeds[0].color
1423-
and (
1424-
message1.embeds[0].color.value == self.bot.mod_color
1425-
or (
1426-
either_direction
1427-
and message1.embeds[0].color.value == self.bot.recipient_color
1428-
)
1429-
)
1430-
and message1.embeds[0].author.url.split("#")[-1].isdigit()
1431-
)
1432-
or (
1433-
message1.embeds[0].footer
1434-
and message1.embeds[0].footer.text
1435-
and message1.embeds[0].footer.text.startswith("[PLAIN]")
1436-
)
1437-
)
1438-
and message1.author == self.bot.user
1439-
):
1440-
break
1441-
else:
1442-
raise ValueError("Thread message not found.")
1443-
1444-
is_plain = False
1445-
if message1.embeds and message1.embeds[0].footer and message1.embeds[0].footer.text:
1446-
if message1.embeds[0].footer.text.startswith("[PLAIN]"):
1447-
is_plain = True
1448-
1449-
if is_plain:
1434+
try:
1435+
joint_id = int(message1.embeds[0].author.url.split("#")[-1])
1436+
except ValueError:
1437+
raise ValueError("Malformed thread message.")
1438+
14501439
messages = [message1]
1451-
creation_time = message1.created_at
1452-
1453-
target_content = message1.embeds[0].description
1454-
14551440
for user in self.recipients:
1456-
async for msg in user.history(limit=50, around=creation_time):
1457-
if abs((msg.created_at - creation_time).total_seconds()) > 15:
1458-
continue
1441+
async for msg in user.history():
1442+
if either_direction:
1443+
if msg.id == joint_id:
1444+
return message1, msg
14591445

1460-
if msg.author != self.bot.user:
1446+
if not (msg.embeds and msg.embeds[0].author.url):
14611447
continue
1462-
1463-
if msg.embeds:
1448+
try:
1449+
if int(msg.embeds[0].author.url.split("#")[-1]) == joint_id:
1450+
messages.append(msg)
1451+
break
1452+
except ValueError:
14641453
continue
14651454

1466-
if target_content and target_content in msg.content:
1467-
messages.append(msg)
1468-
break
1469-
14701455
if len(messages) > 1:
14711456
return messages
14721457

1473-
raise ValueError("Linked Plain DM message not found.")
1474-
1475-
try:
1476-
joint_id = int(message1.embeds[0].author.url.split("#")[-1])
1477-
except ValueError:
1478-
raise ValueError("Malformed thread message.")
1479-
1480-
messages = [message1]
1481-
for user in self.recipients:
1482-
async for msg in user.history():
1483-
if either_direction:
1484-
if msg.id == joint_id:
1485-
return message1, msg
1486-
1487-
if not (msg.embeds and msg.embeds[0].author.url):
1488-
continue
1489-
try:
1490-
if int(msg.embeds[0].author.url.split("#")[-1]) == joint_id:
1491-
messages.append(msg)
1492-
break
1493-
except ValueError:
1494-
continue
1495-
1496-
if len(messages) > 1:
1497-
return messages
1498-
1499-
raise ValueError("DM message not found.")
1458+
raise ValueError("DM message not found.")
15001459

15011460
async def edit_message(self, message_id: typing.Optional[int], message: str) -> None:
15021461
try:
@@ -1521,17 +1480,9 @@ async def edit_message(self, message_id: typing.Optional[int], message: str) ->
15211480
else:
15221481
for m2 in message2:
15231482
if m2 is not None:
1524-
if is_plain:
1525-
if ":** " in m2.content:
1526-
prefix = m2.content.split(":** ", 1)[0] + ":** "
1527-
new_content = f"{prefix}{message}"
1528-
tasks += [m2.edit(content=new_content)]
1529-
else:
1530-
tasks += [m2.edit(content=message)]
1531-
else:
1532-
embed2 = m2.embeds[0]
1533-
embed2.description = message
1534-
tasks += [m2.edit(embed=embed2)]
1483+
embed2 = m2.embeds[0]
1484+
embed2.description = message
1485+
tasks += [m2.edit(embed=embed2)]
15351486

15361487
await asyncio.gather(*tasks)
15371488

0 commit comments

Comments
 (0)