blob: 89e69dece1cb006c456426ef0f802366e7bb6620 [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001// @ts-expect-error
2import type { HaikuClient } from "jshaiku";
3import type { Message, MessageReference } from "discord.js";
4
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00006
pineafan0f5cc782022-08-12 21:55:42 +01007export async function callback(client: HaikuClient, oldMessage: Message, newMessage: Message) {
pineafan63fc5e22022-08-04 22:04:10 +01008 if (newMessage.author.id === client.user.id) return;
pineafan0f5cc782022-08-12 21:55:42 +01009 if (!newMessage.guild) return;
10 const { log, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger;
11 const replyTo: MessageReference | null = newMessage.reference;
pineafan63fc5e22022-08-04 22:04:10 +010012 let newContent = newMessage.cleanContent.replaceAll("`", "‘");
13 let oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
14 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010015 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
16 newMessage.channel.id + newMessage.id
17 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010018 if (config) {
19 attachmentJump = ` [[View attachments]](${config})`;
20 }
pineafan63fc5e22022-08-04 22:04:10 +010021 if (newContent === oldContent) {
Skyler Grey11236ba2022-08-08 21:13:33 +010022 if (!oldMessage.flags.has("CROSSPOSTED") && newMessage.flags.has("CROSSPOSTED")) {
pineafan63fc5e22022-08-04 22:04:10 +010023 const data = {
24 meta: {
25 type: "messageAnnounce",
26 displayName: "Message Published",
27 calculateType: "messageAnnounce",
28 color: NucleusColors.yellow,
29 emoji: "MESSAGE.CREATE",
30 timestamp: newMessage.editedTimestamp
31 },
32 separate: {
33 end: `[[Jump to message]](${newMessage.url})`
34 },
35 list: {
36 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010037 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
38 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
Skyler Grey75ea9172022-08-06 10:22:23 +010039 sent: entry(
40 new Date(newMessage.createdTimestamp),
41 renderDelta(new Date(newMessage.createdTimestamp))
42 ),
43 published: entry(
pineafan0f5cc782022-08-12 21:55:42 +010044 new Date(newMessage.editedTimestamp!),
45 renderDelta(new Date(newMessage.editedTimestamp!))
Skyler Grey75ea9172022-08-06 10:22:23 +010046 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010047 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010048 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010049 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
50 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010051 )
52 },
53 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010054 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 };
57 return log(data);
pineafan32767212022-03-14 21:27:39 +000058 }
pineafan63fc5e22022-08-04 22:04:10 +010059 }
Skyler Grey75ea9172022-08-06 10:22:23 +010060 if (!newMessage.editedTimestamp) {
61 return;
62 }
Skyler Grey11236ba2022-08-08 21:13:33 +010063 if (newContent.length > 256) newContent = newContent.substring(0, 253) + "...";
64 if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "...";
pineafan63fc5e22022-08-04 22:04:10 +010065 const data = {
66 meta: {
67 type: "messageUpdate",
68 displayName: "Message Edited",
69 calculateType: "messageUpdate",
70 color: NucleusColors.yellow,
71 emoji: "MESSAGE.EDIT",
72 timestamp: newMessage.editedTimestamp
73 },
74 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 start:
76 (oldContent
77 ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n`
78 : "**Before:** *Message had no content*\n") +
Skyler Grey11236ba2022-08-08 21:13:33 +010079 (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"),
pineafan63fc5e22022-08-04 22:04:10 +010080 end: `[[Jump to message]](${newMessage.url})`
81 },
82 list: {
83 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
84 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Grey11236ba2022-08-08 21:13:33 +010085 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
86 sent: entry(new Date(newMessage.createdTimestamp), renderDelta(new Date(newMessage.createdTimestamp))),
87 edited: entry(new Date(newMessage.editedTimestamp), renderDelta(new Date(newMessage.editedTimestamp))),
88 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010089 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010090 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
91 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010092 ),
93 repliedTo: entry(
pineafan0f5cc782022-08-12 21:55:42 +010094 replyTo,
95 replyTo
96 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010097 : "None"
pineafan63fc5e22022-08-04 22:04:10 +010098 )
99 },
100 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +0100101 guild: newMessage.guild.id
pineafan63fc5e22022-08-04 22:04:10 +0100102 }
103 };
104 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100105}