Skip to content

Commit f49efbf

Browse files
committed
Use author url to store linked_message_ids
1 parent 3faff2a commit f49efbf

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

bot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
__version__ = '2.0.0'
2626

2727
from contextlib import redirect_stdout
28-
from urllib.parse import urlparse
2928
from copy import deepcopy
3029
import functools
3130
import asyncio
@@ -243,16 +242,18 @@ async def on_message(self, message):
243242
async def on_message_delete(self, message):
244243
'''Support for deleting linked messages'''
245244
if message.embeds and not isinstance(message.channel, discord.DMChannel):
246-
matches = re.findall(r'Moderator - (\d+)', str(message.embeds[0].footer.text))
245+
matches = re.findall(r'\d+', str(message.embeds[0].author.url))
247246
if matches:
248247
thread = await self.threads.find(channel=message.channel)
249248

250249
channel = thread.recipient.dm_channel
251250
message_id = matches[0]
252251

253252
async for msg in channel.history():
254-
if msg.embeds and f'Moderator - {message_id}' in msg.embeds[0].footer.text:
255-
return await msg.delete()
253+
if msg.embeds and msg.embeds[0].author:
254+
url = msg.embeds[0].author.url
255+
if message_id == re.findall(r'\d+', url):
256+
return await msg.delete()
256257

257258
async def on_message_edit(self, before, after):
258259
if before.author.bot:

cogs/modmail.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from discord.ext import commands
44
import datetime
55
import dateutil.parser
6+
import re
7+
from typing import Optional
68
from core.decorators import trigger_typing
79
from core.paginator import PaginatorSession
810

@@ -104,6 +106,17 @@ async def __del(self, ctx, *, name: str.lower):
104106

105107
await ctx.send(embed=em)
106108

109+
@commands.command()
110+
@commands.has_permissions(manage_channels=True)
111+
async def move(self, ctx, *, category: discord.CategoryChannel):
112+
'''Moves a thread to a specified cateogry.'''
113+
thread = await self.bot.threads.find(channel=ctx.channel)
114+
if not thread:
115+
return await ctx.send('This is not a modmail thread.')
116+
117+
await thread.channel.edit(category=category)
118+
await ctx.message.add_reaction('✅')
119+
107120
@commands.command(name='close')
108121
@commands.has_permissions(manage_channels=True)
109122
async def _close(self, ctx):
@@ -126,7 +139,7 @@ async def _close(self, ctx):
126139

127140
# Logging
128141
categ = self.bot.main_category
129-
log_channel = categ.channels[1]
142+
log_channel = categ.channels[0]
130143

131144
log_data = await self.bot.modmail_api.post_log(ctx.channel.id, {
132145
'open': False, 'closed_at': str(datetime.datetime.utcnow()), 'closer': {
@@ -219,16 +232,35 @@ async def reply(self, ctx, *, msg=''):
219232
await thread.reply(ctx.message)
220233

221234
@commands.command()
222-
async def edit(self, ctx, message_id: int, *, new_message):
235+
async def edit(self, ctx, message_id: Optional[int]=None, *, new_message):
223236
'''Edit a message that was sent using the reply command.
224237
225238
`<message_id>` is the id shown in the footer of thread messages.
226239
`<new_message>` is the new message that will be edited in.
227240
'''
228-
thread = self.bot.threads.find(channel=ctx.channel)
229-
if thread and thread.category.name == 'Mod Mail':
230-
await thread.edit_message(message_id, new_message)
241+
thread = await self.bot.threads.find(channel=ctx.channel)
242+
print(message_id, new_message)
243+
if thread and thread.channel.category.name == 'Mod Mail':
244+
linked_message_id = None
245+
246+
async for msg in ctx.channel.history():
247+
if message_id is None and msg.embeds:
248+
em = msg.embeds[0]
249+
if 'Moderator' not in str(em.footer.text):
250+
continue
251+
linked_message_id = int(re.findall(r'\d+', em.author.url)[0])
252+
break
253+
elif message_id and msg.id == message_id:
254+
url = msg.embeds[0].author.url
255+
linked_message_id = int(re.findall(r'\d+', url)[0])
256+
break
231257

258+
if not linked_message_id:
259+
raise commands.UserInputError
260+
261+
await thread.edit_message(linked_message_id, new_message)
262+
await ctx.message.add_reaction('✅')
263+
232264
@commands.command()
233265
@trigger_typing
234266
@commands.has_permissions(manage_channels=True)

0 commit comments

Comments
 (0)