Skip to content

Commit

Permalink
v3.4.1, mask errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Taaku18 committed Dec 18, 2019
1 parent 9c4b98a commit 957a711
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 35 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.
however, insignificant breaking changes does not guarantee a major version bump, see the reasoning [here](https://github.com/kyb3r/modmail/issues/319).


# v3.4.0
# v3.4.1

### Fixed

- Masked a bunch of noise errors when deleting messages.
- Added more checks for deleting messages.

### Breaking

- `thread_initiate` is now dispatched at the beginning of the setup process.
- `thread_create` is dispatched when the thread is registered as a thread by Modmail (ie. when channel topic is edited).
- `thread_ready` is dispatched when a thread finishes its setup steps.


# v3.4.0

### Added

Expand Down
32 changes: 24 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.4.0"
__version__ = "3.4.1"


import asyncio
Expand Down Expand Up @@ -1069,19 +1069,32 @@ async def on_member_join(self, member):
async def on_message_delete(self, message):
"""Support for deleting linked messages"""
# TODO: use audit log to check if modmail deleted the message
if message.embeds and not isinstance(message.channel, discord.DMChannel):
thread = await self.threads.find(channel=message.channel)
if isinstance(message.channel, discord.DMChannel):
thread = await self.threads.find(recipient=message.author)
if not thread:
return
try:
await thread.delete_message(message)
message = await thread.find_linked_message_from_dm(message)
except ValueError as e:
if str(e) not in {"DM message not found.", " Malformed thread message."}:
if str(e) != "Thread channel message not found.":
logger.warning("Failed to find linked message to delete: %s", e)
else:
thread = await self.threads.find(recipient=message.author)
message = await thread.find_linked_message_from_dm(message)
return
embed = message.embeds[0]
embed.set_footer(text=f"{embed.footer.text} (deleted)", icon_url=embed.footer.icon_url)
await message.edit(embed=embed)
return

thread = await self.threads.find(channel=message.channel)
if not thread:
return
try:
await thread.delete_message(message, note=False)
except ValueError as e:
if str(e) not in {"DM message not found.", "Malformed thread message."}:
logger.warning("Failed to find linked message to delete: %s", e)
return
except discord.NotFound:
return

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

if isinstance(after.channel, discord.DMChannel):
thread = await self.threads.find(recipient=before.author)
if not thread:
return

try:
await thread.edit_dm_message(after, after.content)
except ValueError:
Expand Down
5 changes: 3 additions & 2 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,9 @@ async def delete(self, ctx, message_id: int = None):
thread = ctx.thread

try:
await thread.delete_message(message_id)
except ValueError:
await thread.delete_message(message_id, note=True)
except ValueError as e:
logger.warning("Failed to delete message: %s.", e)
return await ctx.send(
embed=discord.Embed(
title="Failed",
Expand Down
54 changes: 31 additions & 23 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ def ready(self) -> bool:
def ready(self, flag: bool):
if flag:
self._ready_event.set()
self.bot.dispatch("thread_ready", self)
self.bot.dispatch("thread_create", self)
else:
self._ready_event.clear()

async def setup(self, *, creator=None, category=None):
"""Create the thread channel and other io related initialisation tasks"""

self.bot.dispatch("thread_create", self)

self.bot.dispatch("thread_initiate", self)
recipient = self.recipient

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

await asyncio.gather(send_genesis_message(), send_recipient_genesis_message())
self.bot.dispatch("thread_ready", self)

def _format_info_embed(self, user, log_url, log_count, color):
"""Get information about a member of a server
Expand Down Expand Up @@ -457,9 +456,14 @@ async def find_linked_messages(
message_id: typing.Optional[int] = None,
either_direction: bool = False,
message1: discord.Message = None,
note: bool = True,
) -> typing.Tuple[discord.Message, typing.Optional[discord.Message]]:
if message1 is not None:
if not message1.embeds or not message1.embeds[0].author.url:
if (
not message1.embeds
or not message1.embeds[0].author.url
or message1.author != self.bot.user
):
raise ValueError("Malformed thread message.")

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

if not (
message1.embeds and message1.embeds[0].author.url and message1.embeds[0].color
message1.embeds
and message1.embeds[0].author.url
and message1.embeds[0].color
and message1.author == self.bot.user
):
raise ValueError("Thread message not found.")

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

if message1.embeds[0].color.value != self.bot.mod_color and not (
Expand All @@ -495,6 +504,8 @@ async def find_linked_messages(
and message1.embeds[0].color.value == self.bot.recipient_color
)
)
and message1.embeds[0].author.url.split("#")[-1].isdigit()
and message1.author == self.bot.user
):
break
else:
Expand All @@ -516,7 +527,7 @@ async def find_linked_messages(
if int(msg.embeds[0].author.url.split("#")[-1]) == joint_id:
return message1, msg
except ValueError:
raise ValueError("DM message not found.")
continue
raise ValueError("DM message not found.")

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

await asyncio.gather(*tasks)

async def delete_message(self, message: typing.Union[int, discord.Message] = None) -> None:
try:
if isinstance(message, discord.Message):
message1, message2 = await self.find_linked_messages(message1=message)
else:
message1, message2 = await self.find_linked_messages(message)
except ValueError as e:
logger.warning("Failed to delete message: %s.", e)
raise

async def delete_message(
self, message: typing.Union[int, discord.Message] = None, note: bool = True
) -> None:
if isinstance(message, discord.Message):
message1, message2 = await self.find_linked_messages(message1=message, note=note)
else:
message1, message2 = await self.find_linked_messages(message, note=note)
tasks = []
if not isinstance(message, discord.Message):
tasks += [message1.delete()]
Expand All @@ -571,12 +579,12 @@ async def find_linked_message_from_dm(self, message, either_direction=False):
return linked_message

msg_id = url.split("#")[-1]
try:
if int(msg_id) == message.id:
return linked_message
except ValueError:
raise ValueError("Malformed dm channel message.")
raise ValueError("DM channel message not found.")
if not msg_id.isdigit():
continue
msg_id = int(msg_id)
if int(msg_id) == message.id:
return linked_message
raise ValueError("Thread channel message not found.")

async def edit_dm_message(self, message: discord.Message, content: str) -> None:
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exclude = '''

[tool.poetry]
name = 'Modmail'
version = '3.4.0'
version = '3.4.1'
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.'
license = 'AGPL-3.0-only'
authors = [
Expand Down

0 comments on commit 957a711

Please sign in to comment.