Skip to content

Commit 44adfaa

Browse files
committed
Merge from "master" & fix some errors from previous commit
2 parents 5e083be + ccc571c commit 44adfaa

File tree

8 files changed

+124
-68
lines changed

8 files changed

+124
-68
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# v2.11.0
8+
9+
### Added
10+
- `loglink` command, returns the log link for the current thread.
11+
12+
# v2.10.2
13+
14+
### Changed
15+
- Your logs now track and show edited messages.
16+
17+
# v2.10.1
18+
19+
### Changed
20+
- Use reply author's top role for the mod tag by default.
721

822
# v2.10.0
923

bot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
"""
2424

25-
__version__ = '2.10.0'
25+
__version__ = '2.11.0'
2626

2727
import discord
2828
from discord.enums import ActivityType
@@ -477,6 +477,8 @@ async def on_message_edit(self, before, after):
477477
if matches and matches[-1] == str(before.id):
478478
embed.description = after.content
479479
await msg.edit(embed=embed)
480+
await self.api.edit_message(str(after.id),
481+
after.content)
480482
break
481483

482484
async def on_command_error(self, ctx, error):

cogs/modmail.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ async def nsfw(self, ctx):
338338
return
339339
await ctx.channel.edit(nsfw=True)
340340
await ctx.message.add_reaction('✅')
341+
342+
@commands.command()
343+
async def loglink(self, ctx):
344+
thread = await self.bot.threads.find(channel=ctx.channel)
345+
if thread:
346+
log_link = await self.bot.api.get_log_link(ctx.channel.id)
347+
await ctx.send(embed=discord.Embed(
348+
color=discord.Color.blurple(),
349+
description=log_link)
350+
)
341351

342352
@commands.command(aliases=['threads'])
343353
@commands.has_permissions(manage_messages=True)
@@ -480,7 +490,9 @@ async def edit(self, ctx, message_id: Optional[int] = None,
480490
if not linked_message_id:
481491
raise commands.UserInputError
482492

483-
await thread.edit_message(linked_message_id, new_message)
493+
await thread.edit_message(linked_message_id, new_message),
494+
await self.bot.api.edit_message(linked_message_id, new_message)
495+
484496
await ctx.message.add_reaction('✅')
485497

486498
@commands.command()

core/clients.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ async def update_repository(self, sha: str = None) -> Optional[dict]:
8383

8484
async def fork_repository(self) -> None:
8585
await self.request(self.FORK_URL, method='POST')
86-
86+
8787
async def has_starred(self) -> bool:
8888
resp = await self.request(self.STAR_URL, return_response=True)
8989
return resp.status == 204
9090

9191
async def star_repository(self) -> None:
9292
await self.request(self.STAR_URL, method='PUT',
93-
headers={'Content-Length': '0'})
93+
headers={'Content-Length': '0'})
9494

9595
# TODO: Broken.
9696
async def get_latest_commits(self, limit: int = 3) -> None:
@@ -153,14 +153,18 @@ async def get_user_logs(self, user_id):
153153
async def get_log(self, channel_id):
154154
return await self.request(self.LOGS + '/' + str(channel_id))
155155

156+
async def get_log_link(self, channel_id):
157+
doc = await self.get_log(channel_id)
158+
return f'https://logs.modmail.tk/{doc["key"]}'
159+
156160
async def get_config(self):
157161
return await self.request(self.CONFIG)
158162

159163
async def update_config(self, data):
160164
data = self.filter_valid(data)
161165
return await self.request(self.CONFIG, method='PATCH', payload=data)
162166

163-
async def get_log_url(self, recipient, channel, creator):
167+
async def create_log_entry(self, recipient, channel, creator):
164168
return await self.request(self.LOGS + '/key', payload={
165169
'channel_id': str(channel.id),
166170
'guild_id': str(self.bot.guild_id),
@@ -180,8 +184,14 @@ async def get_log_url(self, recipient, channel, creator):
180184
}
181185
})
182186

183-
async def append_log(self, message, channel_id='', type_='thread_message'):
187+
async def edit_message(self, message_id, new_content):
188+
await self.request(self.LOGS + '/edit', method='PATCH',
189+
payload={
190+
'message_id': str(message_id),
191+
'new_content': new_content
192+
})
184193

194+
async def append_log(self, message, channel_id='', type_='thread_message'):
185195
channel_id = str(channel_id) or str(message.channel.id)
186196
data = {
187197
'payload': {
@@ -199,12 +209,12 @@ async def append_log(self, message, channel_id='', type_='thread_message'):
199209
'content': message.content,
200210
'type': type_,
201211
'attachments': [
202-
{
212+
{
203213
'id': a.id,
204214
'filename': a.filename,
205215
'is_image': a.width is not None,
206216
'size': a.size,
207-
'url': a.url
217+
'url': a.url
208218
} for a in message.attachments]
209219
}
210220
}
@@ -261,7 +271,11 @@ async def get_user_logs(self, user_id):
261271
async def get_log(self, channel_id):
262272
return await self.logs.find_one({'channel_id': str(channel_id)})
263273

264-
async def get_log_url(self, recipient, channel, creator):
274+
async def get_log_link(self, channel_id):
275+
doc = await self.get_log(channel_id)
276+
return f"{self.bot.config.log_url.strip('/')}/logs/{doc['key']}"
277+
278+
async def create_log_entry(self, recipient, channel, creator):
265279
key = secrets.token_hex(6)
266280

267281
await self.logs.insert_one({
@@ -287,7 +301,7 @@ async def get_log_url(self, recipient, channel, creator):
287301
},
288302
'closer': None,
289303
'messages': []
290-
})
304+
})
291305

292306
return f"{self.bot.config.log_url.strip('/')}/logs/{key}"
293307

@@ -303,31 +317,41 @@ async def update_config(self, data):
303317
return await self.db.config.update_one({'bot_id': self.bot.user.id},
304318
{'$set': data})
305319

306-
async def append_log(self, message, channel_id='', type_='thread_message'):
320+
async def edit_message(self, message_id, new_content):
321+
await self.logs.update_one({
322+
'messages.message_id': str(message_id)
323+
}, {
324+
'$set': {
325+
'messages.$.content': new_content,
326+
'messages.$.edited': True
327+
}
328+
})
307329

330+
async def append_log(self, message, channel_id='', type_='thread_message'):
308331
channel_id = str(channel_id) or str(message.channel.id)
309332
data = {
310-
'timestamp': str(message.created_at),
311-
'message_id': str(message.id),
312-
'author': {
313-
'id': str(message.author.id),
314-
'name': message.author.name,
315-
'discriminator': message.author.discriminator,
316-
'avatar_url': message.author.avatar_url,
317-
'mod': not isinstance(message.channel, DMChannel),
318-
},
319-
'content': message.content,
320-
'type': type_,
321-
'attachments': [
322-
{
323-
'id': a.id,
324-
'filename': a.filename,
325-
'is_image': a.width is not None,
326-
'size': a.size,
327-
'url': a.url
328-
} for a in message.attachments]
329-
}
330-
333+
'timestamp': str(message.created_at),
334+
'message_id': str(message.id),
335+
'author': {
336+
'id': str(message.author.id),
337+
'name': message.author.name,
338+
'discriminator': message.author.discriminator,
339+
'avatar_url': message.author.avatar_url,
340+
'mod': not isinstance(message.channel, DMChannel),
341+
},
342+
'content': message.content,
343+
'type': type_,
344+
'attachments': [
345+
{
346+
'id': a.id,
347+
'filename': a.filename,
348+
'is_image': a.width is not None,
349+
'size': a.size,
350+
'url': a.url
351+
} for a in message.attachments
352+
]
353+
}
354+
331355
return await self.logs.find_one_and_update(
332356
{'channel_id': channel_id},
333357
{'$push': {f'messages': data}},

core/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ConfigManager(ConfigManagerABC):
1010

1111
allowed_to_change_in_command = {
1212
# activity
13-
'activity_message', 'activity_type', 'twitch_url',
13+
'twitch_url',
1414
# bot settings
1515
'main_category_id', 'disable_autoupdates', 'prefix', 'mention',
1616
# logging
@@ -26,7 +26,7 @@ class ConfigManager(ConfigManagerABC):
2626
internal_keys = {
2727
'snippets', 'aliases', 'blocked',
2828
'notification_squad', 'subscriptions',
29-
'closures'
29+
'closures', 'activity_message', 'activity_type'
3030
}
3131

3232
protected_keys = {

core/objects.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ async def validate_token(self) -> bool:
171171
async def get_log(self, channel_id: Union[str, int]) -> dict:
172172
raise NotImplementedError
173173

174+
@abc.abstractmethod
175+
async def get_log_link(self, channel_id: Union[str, int]) -> str:
176+
raise NotImplementedError
177+
174178
@abc.abstractmethod
175179
async def get_config(self) -> dict:
176180
raise NotImplementedError
@@ -180,10 +184,10 @@ async def update_config(self, data: dict):
180184
raise NotImplementedError
181185

182186
@abc.abstractmethod
183-
async def get_log_url(self,
184-
recipient: Member,
185-
channel: TextChannel,
186-
creator: Member) -> str:
187+
async def create_log_entry(self,
188+
recipient: Member,
189+
channel: TextChannel,
190+
creator: Member) -> str:
187191
raise NotImplementedError
188192

189193
@abc.abstractmethod
@@ -207,6 +211,11 @@ async def get_metadata(self) -> dict:
207211
async def post_metadata(self, data: dict) -> dict:
208212
raise NotImplementedError
209213

214+
@abc.abstractmethod
215+
async def edit_message(self, message_id: Union[int, str],
216+
new_content: str) -> None:
217+
raise NotImplementedError
218+
210219

211220
class ConfigManagerABC(abc.ABC):
212221
@property
@@ -314,7 +323,8 @@ async def cancel_closure(self) -> None:
314323
raise NotImplementedError
315324

316325
@abc.abstractmethod
317-
def edit_message(self, message_id: Union[int, str], message: str) -> None:
326+
async def edit_message(self, message_id: Union[int, str],
327+
message: str) -> None:
318328
raise NotImplementedError
319329

320330
@abc.abstractmethod

core/paginator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ async def run(self) -> typing.Optional[Message]:
107107
except (HTTPException, InvalidArgument):
108108
pass
109109

110-
def previous_page(self) -> None:
110+
async def previous_page(self) -> None:
111111
"""Go to the previous page."""
112-
self.show_page(self.current - 1)
112+
await self.show_page(self.current - 1)
113113

114-
def next_page(self) -> None:
114+
async def next_page(self) -> None:
115115
"""Go to the next page"""
116-
self.show_page(self.current + 1)
116+
await self.show_page(self.current + 1)
117117

118118
async def close(self, delete: bool = True) -> typing.Optional[Message]:
119119
"""Delete this embed."""
@@ -132,10 +132,10 @@ async def close(self, delete: bool = True) -> typing.Optional[Message]:
132132
except HTTPException:
133133
pass
134134

135-
def first_page(self) -> None:
135+
async def first_page(self) -> None:
136136
"""Go to immediately to the first page"""
137-
self.show_page(0)
137+
await self.show_page(0)
138138

139-
def last_page(self) -> None:
139+
async def last_page(self) -> None:
140140
"""Go to immediately to the last page"""
141-
self.show_page(len(self.embeds) - 1)
141+
await self.show_page(len(self.embeds) - 1)

core/thread.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ async def _edit_thread_message(channel, message_id, message):
202202
await msg.edit(embed=embed)
203203
break
204204

205-
def edit_message(self, message_id, message):
206-
asyncio.gather(
205+
async def edit_message(self, message_id, message):
206+
await asyncio.gather(
207207
self._edit_thread_message(self.recipient, message_id, message),
208208
self._edit_thread_message(self.channel, message_id, message)
209209
)
@@ -298,14 +298,11 @@ async def send(self, message, destination=None,
298298
if anonymous and from_mod and \
299299
not isinstance(destination, discord.TextChannel):
300300
# Anonymously sending to the user.
301-
name = self.bot.config.get(
302-
'anon_username',
303-
self.bot.config.get('mod_tag', 'Moderator')
304-
)
305-
avatar_url = self.bot.config.get(
306-
'anon_avatar_url',
307-
self.bot.guild.icon_url
308-
)
301+
tag = self.bot.config.get('mod_tag',
302+
str(message.author.top_role))
303+
name = self.bot.config.get('anon_username', tag)
304+
avatar_url = self.bot.config.get('anon_avatar_url',
305+
self.bot.guild.icon_url)
309306
else:
310307
# Normal message
311308
name = str(author)
@@ -372,10 +369,9 @@ async def send(self, message, destination=None,
372369
embed.set_footer(text='Anonymous Reply')
373370
# Normal messages
374371
elif not anonymous:
375-
embed.set_footer(
376-
text=self.bot.config.get('mod_tag', 'Moderator')
377-
)
378-
# Anonymous reply sent to user
372+
tag = self.bot.config.get('mod_tag',
373+
str(message.author.top_role))
374+
embed.set_footer(text=tag) # Normal messages
379375
else:
380376
embed.set_footer(
381377
text=self.bot.config.get('anon_tag', 'Response')
@@ -539,13 +535,11 @@ async def create(self, recipient, *, creator=None, category=None):
539535
thread = Thread(self, recipient, channel)
540536
self.cache[recipient.id] = thread
541537

542-
log_url, log_data = await asyncio.gather(
543-
self.bot.api.get_log_url(recipient,
544-
channel,
545-
creator or recipient),
546-
self.bot.api.get_user_logs(recipient.id),
547-
# self.get_dominant_color(recipient.avatar_url),
548-
)
538+
log_url = await self.bot.api.create_log_entry(recipient, channel,
539+
creator or recipient),
540+
541+
log_data = await self.bot.api.get_user_logs(recipient.id)
542+
# await self.get_dominant_color(recipient.avatar_url)
549543

550544
log_count = sum(1 for log in log_data if not log['open'])
551545
info_embed = self._format_info_embed(recipient, creator,

0 commit comments

Comments
 (0)