Skip to content

Commit ff9888b

Browse files
committed
Create and delete persistent notes
1 parent 96effc2 commit ff9888b

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

cogs/modmail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ async def note_persistent(self, ctx, *, msg: str = ""):
901901
async with ctx.typing():
902902
msg = await ctx.thread.note(ctx.message, persistent=True)
903903
await msg.pin()
904-
await self.bot.api.create_note(recipient=ctx.thread.recipient, message=ctx.message)
904+
await self.bot.api.create_note(recipient=ctx.thread.recipient, message=ctx.message, message_id=msg.id)
905905

906906
@commands.command()
907907
@checks.has_permissions(PermissionLevel.SUPPORTER)

core/clients.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ async def search_closed_by(self, user_id: Union[int, str]):
142142
async def search_by_text(self, text: str, limit: Optional[int]):
143143
return NotImplemented
144144

145+
async def create_note(self, recipient: Member, message: Message, message_id: Union[int, str]):
146+
return NotImplemented
147+
148+
async def find_notes(self, recipient: Member):
149+
return NotImplemented
150+
151+
async def update_note_ids(self, ids: dict):
152+
return NotImplemented
153+
154+
async def delete_note(self, message_id: Union[int, str]):
155+
return NotImplemented
156+
157+
async def edit_note(self, message_id: Union[int, str]):
158+
return NotImplemented
159+
145160
def get_plugin_partition(self, cog):
146161
return NotImplemented
147162

@@ -401,25 +416,34 @@ async def search_by_text(self, text: str, limit: Optional[int]):
401416
{"messages": {"$slice": 5}},
402417
).to_list(limit)
403418

404-
async def create_note(self, recipient: Member, message: Message):
419+
async def create_note(self, recipient: Member, message: Message, message_id: Union[int, str]):
405420
await self.db.notes.insert_one(
406421
{
407422
"recipient": str(recipient.id),
408423
"author": {
409424
"id": str(message.author.id),
410425
"name": message.author.name,
411426
"discriminator": message.author.discriminator,
412-
"avatar_url": str(message.author.avatar_url),
427+
"avatar_url": str(message.author.avatar_url)
413428
},
429+
"message": message.content,
430+
"message_id": str(message_id),
414431
}
415432
)
416433

417-
async def delete_note(self):
418-
pass
419-
420434
async def find_notes(self, recipient: Member):
421435
return await self.db.notes.find({"recipient": str(recipient.id)}).to_list(None)
422436

437+
async def update_note_ids(self, ids: dict):
438+
for object_id, message_id in ids.items():
439+
await self.db.notes.update_one({"_id": object_id}, {"$set": {"message_id": message_id}})
440+
441+
async def delete_note(self, message_id: Union[int, str]):
442+
await self.db.notes.delete_one({"message_id": str(message_id)})
443+
444+
async def edit_note(self, message_id: Union[int, str]):
445+
return NotImplemented
446+
423447
def get_plugin_partition(self, cog):
424448
cls_name = cog.__class__.__name__
425449
return self.db.plugins[cls_name]

core/thread.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import typing
55
from datetime import datetime, timedelta
6+
import time
67
from types import SimpleNamespace
78

89
import isodate
@@ -191,13 +192,42 @@ async def send_recipient_genesis_message():
191192
await self.bot.add_reaction(msg, close_emoji)
192193

193194
async def send_persistent_notes():
194-
notes = await self.bot.api.find_notes()
195+
notes = await self.bot.api.find_notes(self.recipient)
196+
ids = {}
197+
198+
class State:
199+
def store_user(self, user):
200+
return user
201+
195202
for note in notes:
196-
message = discord.Message()
197-
await self.note(note.message)
198-
pass
203+
author = note["author"]
204+
205+
class Author:
206+
name = author["name"]
207+
id = author["id"]
208+
discriminator = author["discriminator"]
209+
avatar_url = author["avatar_url"]
210+
211+
data = {
212+
"id": round(time.time() * 1000 - discord.utils.DISCORD_EPOCH) << 22,
213+
"attachments": {},
214+
"embeds": {},
215+
"edited_timestamp": None,
216+
"type": None,
217+
"pinned": None,
218+
"mention_everyone": None,
219+
"tts": None,
220+
"content": note["message"],
221+
"author": Author(),
222+
}
223+
message = discord.Message(state=State(), channel=None, data=data)
224+
ids[note["_id"]] = str((await self.note(message, persistent=True, thread_creation=True)).id)
225+
226+
await self.bot.api.update_note_ids(ids)
199227

200-
await asyncio.gather(send_genesis_message(), send_recipient_genesis_message())
228+
await asyncio.gather(
229+
send_genesis_message(), send_recipient_genesis_message(), send_persistent_notes()
230+
)
201231
self.bot.dispatch("thread_ready", self)
202232

203233
def _format_info_embed(self, user, log_url, log_count, color):
@@ -517,11 +547,14 @@ async def find_linked_messages(
517547
):
518548
raise ValueError("Thread message not found.")
519549

520-
if message1.embeds[0].color.value == self.bot.main_color and message1.embeds[
521-
0
522-
].author.name.startswith("Note"):
550+
if message1.embeds[0].color.value == self.bot.main_color and (
551+
message1.embeds[0].author.name.startswith("Note")
552+
or message1.embeds[0].author.name.startswith("Persistent Note")
553+
):
523554
if not note:
524555
raise ValueError("Thread message not found.")
556+
elif message1.embeds[0].author.name.startswith("Persistent Note"):
557+
await self.bot.api.delete_note(message_id)
525558
return message1, None
526559

527560
if message1.embeds[0].color.value != self.bot.mod_color and not (
@@ -636,11 +669,11 @@ async def edit_dm_message(self, message: discord.Message, content: str) -> None:
636669
self.bot.api.edit_message(message.id, content), linked_message.edit(embed=embed)
637670
)
638671

639-
async def note(self, message: discord.Message, persistent=False) -> None:
672+
async def note(self, message: discord.Message, persistent=False, thread_creation=False) -> None:
640673
if not message.content and not message.attachments:
641674
raise MissingRequiredArgument(SimpleNamespace(name="msg"))
642675

643-
msg = await self.send(message, self.channel, note=True, persistent_note=persistent)
676+
msg = await self.send(message, self.channel, note=True, persistent_note=persistent, thread_creation=thread_creation)
644677

645678
self.bot.loop.create_task(
646679
self.bot.api.append_log(
@@ -727,6 +760,7 @@ async def send(
727760
anonymous: bool = False,
728761
plain: bool = False,
729762
persistent_note: bool = False,
763+
thread_creation: bool = False,
730764
) -> None:
731765

732766
self.bot.loop.create_task(
@@ -876,7 +910,7 @@ async def send(
876910
embed.set_footer(text=f"Message ID: {message.id}")
877911
embed.colour = self.bot.recipient_color
878912

879-
if from_mod or note:
913+
if (from_mod or note) and not thread_creation:
880914
delete_message = not bool(message.attachments)
881915
if delete_message and destination == self.channel:
882916
try:

0 commit comments

Comments
 (0)