Skip to content

Commit ed6047c

Browse files
committed
Added a delete command
1 parent 92c706e commit ed6047c

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

cogs/modmail.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,7 @@ async def note(self, ctx, *, msg=''):
566566
async with ctx.typing():
567567
await ctx.thread.note(ctx.message)
568568

569-
@commands.command()
570-
@checks.has_permissions(PermissionLevel.SUPPORTER)
571-
@checks.thread_only()
572-
async def edit(self, ctx, message_id: Optional[int] = None,
573-
*, new_message):
574-
"""Edit a message that was sent using the reply command.
575-
576-
If no `message_id` is provided, the
577-
last message sent by a mod will be edited.
578-
579-
`[message_id]` the id of the message that you want to edit.
580-
`new_message` is the new message that will be edited in.
581-
"""
582-
thread = ctx.thread
583-
569+
async def find_linked_message(self, ctx, message_id):
584570
linked_message_id = None
585571

586572
async for msg in ctx.channel.history():
@@ -600,8 +586,31 @@ async def edit(self, ctx, message_id: Optional[int] = None,
600586
linked_message_id = str(url).split('/')[-1]
601587
break
602588

603-
if not linked_message_id:
604-
raise commands.UserInputError
589+
return linked_message_id
590+
591+
@commands.command()
592+
@checks.has_permissions(PermissionLevel.SUPPORTER)
593+
@checks.thread_only()
594+
async def edit(self, ctx, message_id: Optional[int] = None,
595+
*, new_message):
596+
"""Edit a message that was sent using the reply command.
597+
598+
If no `message_id` is provided, the
599+
last message sent by a mod will be edited.
600+
601+
`[message_id]` the id of the message that you want to edit.
602+
`new_message` is the new message that will be edited in.
603+
"""
604+
thread = ctx.thread
605+
606+
linked_message_id = await self.find_linked_message(ctx, message_id)
607+
608+
if linked_message_id is None:
609+
return await ctx.send(embed=discord.Embed(
610+
title='Failed',
611+
description='Cannot find a message to edit.',
612+
color=discord.Color.red()
613+
))
605614

606615
await asyncio.gather(
607616
thread.edit_message(linked_message_id, new_message),
@@ -801,6 +810,27 @@ async def unblock(self, ctx, *, user: User = None):
801810

802811
return await ctx.send(embed=embed)
803812

813+
@commands.command()
814+
@checks.has_permissions(PermissionLevel.SUPPORTER)
815+
async def delete(self, ctx, message_id: int = None):
816+
"""
817+
Deletes the previous message, unless a message ID is provided, which in that case,
818+
delete the message with that message ID.
819+
"""
820+
thread = ctx.thread
821+
822+
linked_message_id = await self.find_linked_message(ctx, message_id)
823+
824+
if linked_message_id is None:
825+
return await ctx.send(embed=discord.Embed(
826+
title='Failed',
827+
description='Cannot find a message to delete.',
828+
color=discord.Color.red()
829+
))
830+
831+
await thread.delete_message(linked_message_id)
832+
await ctx.message.add_reaction('✅')
833+
804834

805835
def setup(bot):
806836
bot.add_cog(Modmail(bot))

core/thread.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,38 @@ async def cancel_closure(self):
313313
await self.bot.config.update()
314314

315315
@staticmethod
316-
async def _edit_thread_message(channel, message_id, message):
316+
async def _find_thread_message(channel, message_id):
317317
async for msg in channel.history():
318318
if not msg.embeds:
319319
continue
320320
embed = msg.embeds[0]
321321
if embed and embed.author and embed.author.url:
322322
if str(message_id) == str(embed.author.url).split('/')[-1]:
323-
embed.description = message
324-
await msg.edit(embed=embed)
325-
break
323+
return msg
326324

327325
async def edit_message(self, message_id, message):
326+
msg_recipient, msg_channel = await asyncio.gather(
327+
self._find_thread_message(self.recipient, message_id),
328+
self._find_thread_message(self.channel, message_id)
329+
)
330+
331+
embed_recipient = msg_recipient.embeds[0]
332+
embed_channel = msg_recipient.embeds[0]
333+
embed_recipient.description = message
334+
embed_channel.description = message
335+
await asyncio.gather(
336+
msg_recipient.edit(embed=embed_recipient),
337+
msg_channel.edit(embed=embed_channel)
338+
)
339+
340+
async def delete_message(self, message_id):
341+
msg_recipient, msg_channel = await asyncio.gather(
342+
self._find_thread_message(self.recipient, message_id),
343+
self._find_thread_message(self.channel, message_id)
344+
)
328345
await asyncio.gather(
329-
self._edit_thread_message(self.recipient, message_id, message),
330-
self._edit_thread_message(self.channel, message_id, message)
346+
msg_recipient.delete(),
347+
msg_channel.delete()
331348
)
332349

333350
async def note(self, message):

0 commit comments

Comments
 (0)