Skip to content

Commit c7a87e1

Browse files
committed
use jump_url
1 parent 35c6b44 commit c7a87e1

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

bot.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
from discord.ext.commands.view import StringView
3737
from colorama import init, Fore, Style
3838

39-
init()
40-
4139
from core.api import Github, ModmailApiClient
4240
from core.thread import ThreadManager
4341
from core.config import ConfigManager
4442

4543

44+
init()
4645

4746
line = Fore.BLACK + Style.BRIGHT + '-------------------------' + Style.RESET_ALL
4847

@@ -60,7 +59,7 @@ def __init__(self):
6059
self.data_task = self.loop.create_task(self.data_loop())
6160
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
6261
self._add_commands()
63-
62+
6463
def _add_commands(self):
6564
"""Adds commands automatically"""
6665
self.remove_command('help')
@@ -151,7 +150,6 @@ async def on_connect(self):
151150
status = self.config.get('status')
152151
if status:
153152
await self.change_presence(activity=discord.Game(status))
154-
155153

156154
async def on_ready(self):
157155
"""Bot startup, sets uptime."""
@@ -227,7 +225,7 @@ async def get_context(self, message, *, cls=commands.Context):
227225
return ctx
228226

229227
async def on_message(self, message):
230-
if message.type == discord.MessageType.pins_add and message.author == self.user:
228+
if message.type == discord.MessageType.pins_add and message.author == self.user:
231229
await message.delete()
232230
if message.author.bot:
233231
return
@@ -246,7 +244,7 @@ async def on_message(self, message):
246244
async def on_message_delete(self, message):
247245
"""Support for deleting linked messages"""
248246
if message.embeds and not isinstance(message.channel, discord.DMChannel):
249-
matches = re.findall(r'\d+', str(message.embeds[0].author.url))
247+
matches = int(str(message.embeds[0].author.url).split('/')[-1])
250248
if matches:
251249
thread = await self.threads.find(channel=message.channel)
252250

@@ -256,7 +254,7 @@ async def on_message_delete(self, message):
256254
async for msg in channel.history():
257255
if msg.embeds and msg.embeds[0].author:
258256
url = msg.embeds[0].author.url
259-
if message_id == re.findall(r'\d+', url)[0]:
257+
if message_id == url.split('/')[-1]:
260258
return await msg.delete()
261259

262260
async def on_message_edit(self, before, after):
@@ -267,8 +265,8 @@ async def on_message_edit(self, before, after):
267265
async for msg in thread.channel.history():
268266
if msg.embeds:
269267
embed = msg.embeds[0]
270-
matches = re.findall(r'\d+', str(embed.author.url))
271-
if matches and int(matches[0]) == before.id:
268+
matches = embed.author.url.split('/')
269+
if matches and int(matches[-1]) == before.id:
272270
if ' - (Edited)' not in embed.footer.text:
273271
embed.set_footer(text=embed.footer.text + ' - (Edited)')
274272
embed.description = after.content
@@ -295,9 +293,9 @@ def overwrites(self, ctx):
295293
return overwrites
296294

297295
async def validate_api_token(self):
298-
valid = True
296+
valid = True
299297
try:
300-
token = self.config.modmail_api_token
298+
self.config.modmail_api_token
301299
except KeyError:
302300
print('MODMAIL_API_TOKEN not found.')
303301
print('Set a config variable called MODMAIL_API_TOKEN with a token from https://dashboard.modmail.tk')
@@ -311,7 +309,7 @@ async def validate_api_token(self):
311309
await self.logout()
312310
else:
313311
print(Style.RESET_ALL + Fore.CYAN + 'Validated API token.' + Style.RESET_ALL)
314-
312+
315313
async def data_loop(self):
316314
await self.wait_until_ready()
317315

cogs/modmail.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from discord.ext import commands
44
import datetime
55
import dateutil.parser
6-
import re
76
from typing import Optional, Union
87
from core.decorators import trigger_typing
98
from core.paginator import PaginatorSession
@@ -255,11 +254,11 @@ async def edit(self, ctx, message_id: Optional[int]=None, *, new_message):
255254
em = msg.embeds[0]
256255
if 'Moderator' not in str(em.footer.text):
257256
continue
258-
linked_message_id = int(re.findall(r'\d+', em.author.url)[0])
257+
linked_message_id = int(em.author.url.split('/')[-1])
259258
break
260259
elif message_id and msg.id == message_id:
261260
url = msg.embeds[0].author.url
262-
linked_message_id = int(re.findall(r'\d+', url)[0])
261+
linked_message_id = int(url.split('/')[-1])
263262
break
264263

265264
if not linked_message_id:

core/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def __init__(self, bot):
4141
self.headers = {
4242
'Authorization': 'Bearer ' + self.token
4343
}
44-
44+
4545
async def validate_token(self):
4646
resp = await self.request(self.base + '/token/verify', return_response=True)
4747
return resp.status == 200
48-
48+
4949
def post_metadata(self, data):
5050
return self.request(self.metadata, method='POST', payload=data)
5151

core/config.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import os
32
import json
43
import box
@@ -8,15 +7,13 @@ class ConfigManager:
87
"""Class that manages a cached configuration"""
98

109
valid_keys = {
11-
'prefix', 'status', 'guild_id',
12-
'mention', 'disable_autoupdates',
13-
'modmail_guild_id', 'token', 'snippets',
10+
'prefix', 'status', 'guild_id',
11+
'mention', 'disable_autoupdates',
12+
'modmail_guild_id', 'token', 'snippets',
1413
'aliases', 'owners', 'modmail_api_token'
15-
}
16-
17-
18-
allowed_to_change_in_command = valid_keys - {'token', 'snippets', 'aliases', 'owners', 'modmail_api_token'}
14+
}
1915

16+
allowed_to_change_in_command = valid_keys - {'token', 'snippets', 'aliases', 'owners', 'modmail_api_token'}
2017

2118
def __init__(self, bot):
2219
self.bot = bot

core/decorators.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from discord.ext import commands
44
import asyncio
55

6+
67
def trigger_typing(func):
78
@functools.wraps(func)
89
async def wrapper(self, ctx, *args, **kwargs):
910
await ctx.trigger_typing()
1011
return await func(self, ctx, *args, **kwargs)
1112
return wrapper
1213

14+
1315
def auth_required(func):
1416
@functools.wraps(func)
1517
async def wrapper(self, ctx, *args, **kwargs):
@@ -19,22 +21,25 @@ async def wrapper(self, ctx, *args, **kwargs):
1921
color=discord.Color.red(),
2022
title='Unauthorized',
2123
description='You can only use this command if you have a configured `MODMAIL_API_TOKEN`. Get your token from https://dashboard.modmail.tk'
22-
)
24+
)
2325
await ctx.send(embed=em)
24-
return wrapper
26+
return wrapper
27+
2528

2629
def owner_only():
2730
async def predicate(ctx):
2831
allowed = [int(x) for x in str(ctx.bot.config.get('owners', '0')).split(',')]
2932
return ctx.author.id in allowed
3033
return commands.check(predicate)
3134

35+
3236
def asyncexecutor(loop=None, executor=None):
3337
loop = loop or asyncio.get_event_loop()
38+
3439
def decorator(func):
3540
@functools.wraps(func)
3641
def wrapper(*args, **kwargs):
3742
partial = functools.partial(func, *args, **kwargs)
3843
return loop.run_in_executor(executor, partial)
3944
return wrapper
40-
return decorator
45+
return decorator

core/thread.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def _edit_thread_message(self, channel, message_id, message):
5050
continue
5151
embed = msg.embeds[0]
5252
if embed and embed.author:
53-
if message_id == int(re.findall(r'\d+', embed.author.url)[0]):
53+
if message_id == int(embed.author.url.split('/')[-1]):
5454
if ' - (Edited)' not in embed.footer.text:
5555
embed.set_footer(text=embed.footer.text + ' - (Edited)')
5656
embed.description = message
@@ -90,7 +90,7 @@ async def send(self, message, destination=None, from_mod=False, delete_message=T
9090
timestamp=message.created_at
9191
)
9292

93-
em.set_author(name=str(author), icon_url=author.avatar_url, url=f'https://{message.id}.id') # store message id in hidden url
93+
em.set_author(name=str(author), icon_url=author.avatar_url, url=message.jump_url) # store message id in hidden url
9494

9595
image_types = ['.png', '.jpg', '.gif', '.jpeg', '.webp']
9696
is_image_url = lambda u: any(urlparse(u.lower()).path.endswith(x) for x in image_types)
@@ -176,7 +176,7 @@ async def _find_from_channel(self, channel):
176176

177177
if channel.topic and 'User ID: ' in channel.topic:
178178
user_id = int(re.findall(r'\d+', channel.topic)[0])
179-
179+
180180
# BUG: This wont work with multiple categories.
181181
# elif channel.topic is None:
182182
# async for message in channel.history(limit=50):

0 commit comments

Comments
 (0)