blob: 37178a74305dffdb3e475220e5e3c6c1a1c083f9 [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01002import type { Message, MessageReference } from "discord.js";
3
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00005
PineaFan752af462022-12-31 21:59:38 +00006export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
pineafan63fc5e22022-08-04 22:04:10 +01007 if (newMessage.author.id === client.user.id) return;
pineafan0f5cc782022-08-12 21:55:42 +01008 if (!newMessage.guild) return;
9 const { log, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger;
10 const replyTo: MessageReference | null = newMessage.reference;
pineafan63fc5e22022-08-04 22:04:10 +010011 let newContent = newMessage.cleanContent.replaceAll("`", "‘");
12 let oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
13 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010014 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
15 newMessage.channel.id + newMessage.id
16 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010017 if (config) {
18 attachmentJump = ` [[View attachments]](${config})`;
19 }
pineafan63fc5e22022-08-04 22:04:10 +010020 if (newContent === oldContent) {
Skyler Grey11236ba2022-08-08 21:13:33 +010021 if (!oldMessage.flags.has("CROSSPOSTED") && newMessage.flags.has("CROSSPOSTED")) {
pineafan63fc5e22022-08-04 22:04:10 +010022 const data = {
23 meta: {
24 type: "messageAnnounce",
25 displayName: "Message Published",
26 calculateType: "messageAnnounce",
27 color: NucleusColors.yellow,
28 emoji: "MESSAGE.CREATE",
29 timestamp: newMessage.editedTimestamp
30 },
31 separate: {
32 end: `[[Jump to message]](${newMessage.url})`
33 },
34 list: {
35 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010036 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
37 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
Skyler Grey75ea9172022-08-06 10:22:23 +010038 sent: entry(
39 new Date(newMessage.createdTimestamp),
40 renderDelta(new Date(newMessage.createdTimestamp))
41 ),
42 published: entry(
pineafan0f5cc782022-08-12 21:55:42 +010043 new Date(newMessage.editedTimestamp!),
44 renderDelta(new Date(newMessage.editedTimestamp!))
Skyler Grey75ea9172022-08-06 10:22:23 +010045 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010046 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010047 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010048 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
49 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010050 )
51 },
52 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010053 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010054 }
pineafan63fc5e22022-08-04 22:04:10 +010055 };
56 return log(data);
pineafan32767212022-03-14 21:27:39 +000057 }
pineafan63fc5e22022-08-04 22:04:10 +010058 }
Skyler Grey75ea9172022-08-06 10:22:23 +010059 if (!newMessage.editedTimestamp) {
60 return;
61 }
Skyler Grey11236ba2022-08-08 21:13:33 +010062 if (newContent.length > 256) newContent = newContent.substring(0, 253) + "...";
63 if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "...";
pineafan63fc5e22022-08-04 22:04:10 +010064 const data = {
65 meta: {
66 type: "messageUpdate",
67 displayName: "Message Edited",
68 calculateType: "messageUpdate",
69 color: NucleusColors.yellow,
70 emoji: "MESSAGE.EDIT",
71 timestamp: newMessage.editedTimestamp
72 },
73 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 start:
75 (oldContent
76 ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n`
77 : "**Before:** *Message had no content*\n") +
Skyler Grey11236ba2022-08-08 21:13:33 +010078 (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"),
pineafan63fc5e22022-08-04 22:04:10 +010079 end: `[[Jump to message]](${newMessage.url})`
80 },
81 list: {
82 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
83 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Grey11236ba2022-08-08 21:13:33 +010084 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel)),
85 sent: entry(new Date(newMessage.createdTimestamp), renderDelta(new Date(newMessage.createdTimestamp))),
86 edited: entry(new Date(newMessage.editedTimestamp), renderDelta(new Date(newMessage.editedTimestamp))),
87 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010088 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010089 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
90 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010091 ),
92 repliedTo: entry(
pineafan0f5cc782022-08-12 21:55:42 +010093 replyTo,
94 replyTo
95 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010096 : "None"
pineafan63fc5e22022-08-04 22:04:10 +010097 )
98 },
99 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +0100100 guild: newMessage.guild.id
pineafan63fc5e22022-08-04 22:04:10 +0100101 }
102 };
103 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100104}