blob: b74314727ed5a9cfc6d6ead1e880e65bcdb43dc0 [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";
PineaFan538d3752023-01-12 21:48:23 +00003import type Discord from "discord.js";
pineafan0f5cc782022-08-12 21:55:42 +01004
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00006
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
PineaFan538d3752023-01-12 21:48:23 +00008 if (newMessage.author.id === client.user!.id) return;
TheCodedProf4f79da12023-01-31 16:50:37 -05009 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010010 if (!newMessage.guild) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000011 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } =
12 client.logger;
13
pineafan0f5cc782022-08-12 21:55:42 +010014 const replyTo: MessageReference | null = newMessage.reference;
pineafan63fc5e22022-08-04 22:04:10 +010015 let newContent = newMessage.cleanContent.replaceAll("`", "‘");
16 let oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
17 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010018 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
19 newMessage.channel.id + newMessage.id
20 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010021 if (config) {
22 attachmentJump = ` [[View attachments]](${config})`;
23 }
TheCodedProf267563a2023-01-21 17:00:57 -050024 if (newMessage.crosspostable !== oldMessage.crosspostable) {
Skyler Greyda16adf2023-03-05 10:22:12 +000025 if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return;
PineaFan538d3752023-01-12 21:48:23 +000026 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010027 const data = {
28 meta: {
29 type: "messageAnnounce",
30 displayName: "Message Published",
31 calculateType: "messageAnnounce",
TheCodedProfe00405f2023-03-05 14:44:23 -050032 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010033 emoji: "MESSAGE.CREATE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050034 timestamp: newMessage.editedTimestamp ?? Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010035 },
36 separate: {
37 end: `[[Jump to message]](${newMessage.url})`
38 },
39 list: {
40 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010041 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Greyda16adf2023-03-05 10:22:12 +000042 sentIn: entry(
43 newMessage.channel.id,
44 renderChannel(newMessage.channel as Discord.GuildBasedChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010045 ),
Skyler Greyda16adf2023-03-05 10:22:12 +000046 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
PineappleFanc1908c02023-03-08 21:20:30 +000047 published: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey11236ba2022-08-08 21:13:33 +010048 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010049 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010050 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
51 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010052 )
53 },
54 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010055 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010056 }
pineafan63fc5e22022-08-04 22:04:10 +010057 };
58 return log(data);
pineafan32767212022-03-14 21:27:39 +000059 }
pineafan63fc5e22022-08-04 22:04:10 +010060 }
Skyler Greyda16adf2023-03-05 10:22:12 +000061 if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010062 if (!newMessage.editedTimestamp) {
63 return;
64 }
Skyler Grey11236ba2022-08-08 21:13:33 +010065 if (newContent.length > 256) newContent = newContent.substring(0, 253) + "...";
66 if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "...";
pineafan63fc5e22022-08-04 22:04:10 +010067 const data = {
68 meta: {
69 type: "messageUpdate",
70 displayName: "Message Edited",
71 calculateType: "messageUpdate",
72 color: NucleusColors.yellow,
73 emoji: "MESSAGE.EDIT",
74 timestamp: newMessage.editedTimestamp
75 },
76 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010077 start:
78 (oldContent
79 ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n`
80 : "**Before:** *Message had no content*\n") +
Skyler Grey11236ba2022-08-08 21:13:33 +010081 (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"),
pineafan63fc5e22022-08-04 22:04:10 +010082 end: `[[Jump to message]](${newMessage.url})`
83 },
84 list: {
85 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
86 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
PineaFan538d3752023-01-12 21:48:23 +000087 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
88 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
89 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
Skyler Grey11236ba2022-08-08 21:13:33 +010090 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010091 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010092 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
93 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010094 ),
95 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000096 replyTo ? replyTo.messageId! : null,
pineafan0f5cc782022-08-12 21:55:42 +010097 replyTo
98 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010099 : "None"
pineafan63fc5e22022-08-04 22:04:10 +0100100 )
101 },
102 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +0100103 guild: newMessage.guild.id
pineafan63fc5e22022-08-04 22:04:10 +0100104 }
105 };
106 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100107}