Skip to content

Commit 957a711

Browse files
committed
v3.4.1, mask errors
1 parent 9c4b98a commit 957a711

File tree

5 files changed

+73
-35
lines changed

5 files changed

+73
-35
lines changed

CHANGELOG.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@ This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.
77
however, insignificant breaking changes does not guarantee a major version bump, see the reasoning [here](https://github.com/kyb3r/modmail/issues/319).
88

99

10-
# v3.4.0
10+
# v3.4.1
11+
12+
### Fixed
1113

14+
- Masked a bunch of noise errors when deleting messages.
15+
- Added more checks for deleting messages.
16+
17+
### Breaking
18+
19+
- `thread_initiate` is now dispatched at the beginning of the setup process.
20+
- `thread_create` is dispatched when the thread is registered as a thread by Modmail (ie. when channel topic is edited).
21+
- `thread_ready` is dispatched when a thread finishes its setup steps.
22+
23+
24+
# v3.4.0
1225

1326
### Added
1427

bot.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "3.4.0"
1+
__version__ = "3.4.1"
22

33

44
import asyncio
@@ -1069,19 +1069,32 @@ async def on_member_join(self, member):
10691069
async def on_message_delete(self, message):
10701070
"""Support for deleting linked messages"""
10711071
# TODO: use audit log to check if modmail deleted the message
1072-
if message.embeds and not isinstance(message.channel, discord.DMChannel):
1073-
thread = await self.threads.find(channel=message.channel)
1072+
if isinstance(message.channel, discord.DMChannel):
1073+
thread = await self.threads.find(recipient=message.author)
1074+
if not thread:
1075+
return
10741076
try:
1075-
await thread.delete_message(message)
1077+
message = await thread.find_linked_message_from_dm(message)
10761078
except ValueError as e:
1077-
if str(e) not in {"DM message not found.", " Malformed thread message."}:
1079+
if str(e) != "Thread channel message not found.":
10781080
logger.warning("Failed to find linked message to delete: %s", e)
1079-
else:
1080-
thread = await self.threads.find(recipient=message.author)
1081-
message = await thread.find_linked_message_from_dm(message)
1081+
return
10821082
embed = message.embeds[0]
10831083
embed.set_footer(text=f"{embed.footer.text} (deleted)", icon_url=embed.footer.icon_url)
10841084
await message.edit(embed=embed)
1085+
return
1086+
1087+
thread = await self.threads.find(channel=message.channel)
1088+
if not thread:
1089+
return
1090+
try:
1091+
await thread.delete_message(message, note=False)
1092+
except ValueError as e:
1093+
if str(e) not in {"DM message not found.", "Malformed thread message."}:
1094+
logger.warning("Failed to find linked message to delete: %s", e)
1095+
return
1096+
except discord.NotFound:
1097+
return
10851098

10861099
async def on_bulk_message_delete(self, messages):
10871100
await discord.utils.async_all(self.on_message_delete(msg) for msg in messages)
@@ -1094,6 +1107,9 @@ async def on_message_edit(self, before, after):
10941107

10951108
if isinstance(after.channel, discord.DMChannel):
10961109
thread = await self.threads.find(recipient=before.author)
1110+
if not thread:
1111+
return
1112+
10971113
try:
10981114
await thread.edit_dm_message(after, after.content)
10991115
except ValueError:

cogs/modmail.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,9 @@ async def delete(self, ctx, message_id: int = None):
11501150
thread = ctx.thread
11511151

11521152
try:
1153-
await thread.delete_message(message_id)
1154-
except ValueError:
1153+
await thread.delete_message(message_id, note=True)
1154+
except ValueError as e:
1155+
logger.warning("Failed to delete message: %s.", e)
11551156
return await ctx.send(
11561157
embed=discord.Embed(
11571158
title="Failed",

core/thread.py

+31-23
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ def ready(self) -> bool:
7272
def ready(self, flag: bool):
7373
if flag:
7474
self._ready_event.set()
75-
self.bot.dispatch("thread_ready", self)
75+
self.bot.dispatch("thread_create", self)
7676
else:
7777
self._ready_event.clear()
7878

7979
async def setup(self, *, creator=None, category=None):
8080
"""Create the thread channel and other io related initialisation tasks"""
81-
82-
self.bot.dispatch("thread_create", self)
83-
81+
self.bot.dispatch("thread_initiate", self)
8482
recipient = self.recipient
8583

8684
# in case it creates a channel outside of category
@@ -175,6 +173,7 @@ async def send_recipient_genesis_message():
175173
await self.bot.add_reaction(msg, close_emoji)
176174

177175
await asyncio.gather(send_genesis_message(), send_recipient_genesis_message())
176+
self.bot.dispatch("thread_ready", self)
178177

179178
def _format_info_embed(self, user, log_url, log_count, color):
180179
"""Get information about a member of a server
@@ -457,9 +456,14 @@ async def find_linked_messages(
457456
message_id: typing.Optional[int] = None,
458457
either_direction: bool = False,
459458
message1: discord.Message = None,
459+
note: bool = True,
460460
) -> typing.Tuple[discord.Message, typing.Optional[discord.Message]]:
461461
if message1 is not None:
462-
if not message1.embeds or not message1.embeds[0].author.url:
462+
if (
463+
not message1.embeds
464+
or not message1.embeds[0].author.url
465+
or message1.author != self.bot.user
466+
):
463467
raise ValueError("Malformed thread message.")
464468

465469
elif message_id is not None:
@@ -469,13 +473,18 @@ async def find_linked_messages(
469473
raise ValueError("Thread message not found.")
470474

471475
if not (
472-
message1.embeds and message1.embeds[0].author.url and message1.embeds[0].color
476+
message1.embeds
477+
and message1.embeds[0].author.url
478+
and message1.embeds[0].color
479+
and message1.author == self.bot.user
473480
):
474481
raise ValueError("Thread message not found.")
475482

476483
if message1.embeds[0].color.value == self.bot.main_color and message1.embeds[
477484
0
478485
].author.name.startswith("Note"):
486+
if not note:
487+
raise ValueError("Thread message not found.")
479488
return message1, None
480489

481490
if message1.embeds[0].color.value != self.bot.mod_color and not (
@@ -495,6 +504,8 @@ async def find_linked_messages(
495504
and message1.embeds[0].color.value == self.bot.recipient_color
496505
)
497506
)
507+
and message1.embeds[0].author.url.split("#")[-1].isdigit()
508+
and message1.author == self.bot.user
498509
):
499510
break
500511
else:
@@ -516,7 +527,7 @@ async def find_linked_messages(
516527
if int(msg.embeds[0].author.url.split("#")[-1]) == joint_id:
517528
return message1, msg
518529
except ValueError:
519-
raise ValueError("DM message not found.")
530+
continue
520531
raise ValueError("DM message not found.")
521532

522533
async def edit_message(self, message_id: typing.Optional[int], message: str) -> None:
@@ -537,16 +548,13 @@ async def edit_message(self, message_id: typing.Optional[int], message: str) ->
537548

538549
await asyncio.gather(*tasks)
539550

540-
async def delete_message(self, message: typing.Union[int, discord.Message] = None) -> None:
541-
try:
542-
if isinstance(message, discord.Message):
543-
message1, message2 = await self.find_linked_messages(message1=message)
544-
else:
545-
message1, message2 = await self.find_linked_messages(message)
546-
except ValueError as e:
547-
logger.warning("Failed to delete message: %s.", e)
548-
raise
549-
551+
async def delete_message(
552+
self, message: typing.Union[int, discord.Message] = None, note: bool = True
553+
) -> None:
554+
if isinstance(message, discord.Message):
555+
message1, message2 = await self.find_linked_messages(message1=message, note=note)
556+
else:
557+
message1, message2 = await self.find_linked_messages(message, note=note)
550558
tasks = []
551559
if not isinstance(message, discord.Message):
552560
tasks += [message1.delete()]
@@ -571,12 +579,12 @@ async def find_linked_message_from_dm(self, message, either_direction=False):
571579
return linked_message
572580

573581
msg_id = url.split("#")[-1]
574-
try:
575-
if int(msg_id) == message.id:
576-
return linked_message
577-
except ValueError:
578-
raise ValueError("Malformed dm channel message.")
579-
raise ValueError("DM channel message not found.")
582+
if not msg_id.isdigit():
583+
continue
584+
msg_id = int(msg_id)
585+
if int(msg_id) == message.id:
586+
return linked_message
587+
raise ValueError("Thread channel message not found.")
580588

581589
async def edit_dm_message(self, message: discord.Message, content: str) -> None:
582590
try:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exclude = '''
2121

2222
[tool.poetry]
2323
name = 'Modmail'
24-
version = '3.4.0'
24+
version = '3.4.1'
2525
description = 'Modmail is similar to Reddits Modmail both in functionality and purpose. It serves as a shared inbox for server staff to communicate with their users in a seamless way.'
2626
license = 'AGPL-3.0-only'
2727
authors = [

0 commit comments

Comments
 (0)