blob: 5964df98937f4c81f11aed727f4665cdbc115c6c [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
TheCodedProfc3195b52023-06-23 15:53:00 -04002import { Message, MessageReference, ButtonStyle } from "discord.js";
PineaFan538d3752023-01-12 21:48:23 +00003import type Discord from "discord.js";
TheCodedProf32302c92023-06-06 17:01:08 -04004import * as diff from "diff";
TheCodedProfc3195b52023-06-23 15:53:00 -04005import addPlural from "../utils/plurals.js";
pineafan0f5cc782022-08-12 21:55:42 +01006
pineafan63fc5e22022-08-04 22:04:10 +01007export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00008
PineaFan752af462022-12-31 21:59:38 +00009export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
PineaFan538d3752023-01-12 21:48:23 +000010 if (newMessage.author.id === client.user!.id) return;
TheCodedProf4f79da12023-01-31 16:50:37 -050011 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010012 if (!newMessage.guild) return;
TheCodedProfc3195b52023-06-23 15:53:00 -040013 const {
14 log,
15 isLogging,
16 NucleusColors,
17 entry,
18 renderUser,
19 renderDelta,
20 renderNumberDelta,
21 renderChannel,
22 preLog
23 } = client.logger;
pineafan0f5cc782022-08-12 21:55:42 +010024 const replyTo: MessageReference | null = newMessage.reference;
TheCodedProf32302c92023-06-06 17:01:08 -040025 const newContent = newMessage.cleanContent.replaceAll("`", "‘");
26 const oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
pineafan63fc5e22022-08-04 22:04:10 +010027 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010028 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
29 newMessage.channel.id + newMessage.id
30 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010031 if (config) {
32 attachmentJump = ` [[View attachments]](${config})`;
33 }
TheCodedProf267563a2023-01-21 17:00:57 -050034 if (newMessage.crosspostable !== oldMessage.crosspostable) {
Skyler Greyda16adf2023-03-05 10:22:12 +000035 if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return;
PineaFan538d3752023-01-12 21:48:23 +000036 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010037 const data = {
38 meta: {
39 type: "messageAnnounce",
40 displayName: "Message Published",
41 calculateType: "messageAnnounce",
TheCodedProfe00405f2023-03-05 14:44:23 -050042 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010043 emoji: "MESSAGE.CREATE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050044 timestamp: newMessage.editedTimestamp ?? Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010045 },
46 separate: {
47 end: `[[Jump to message]](${newMessage.url})`
48 },
49 list: {
50 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010051 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Greyda16adf2023-03-05 10:22:12 +000052 sentIn: entry(
53 newMessage.channel.id,
54 renderChannel(newMessage.channel as Discord.GuildBasedChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010055 ),
Skyler Greyda16adf2023-03-05 10:22:12 +000056 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
PineappleFanc1908c02023-03-08 21:20:30 +000057 published: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey11236ba2022-08-08 21:13:33 +010058 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010059 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010060 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
61 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010062 )
63 },
64 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010065 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010066 }
pineafan63fc5e22022-08-04 22:04:10 +010067 };
68 return log(data);
pineafan32767212022-03-14 21:27:39 +000069 }
pineafan63fc5e22022-08-04 22:04:10 +010070 }
Skyler Greyda16adf2023-03-05 10:22:12 +000071 if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010072 if (!newMessage.editedTimestamp) {
73 return;
74 }
TheCodedProf32302c92023-06-06 17:01:08 -040075 const differences = diff.diffChars(oldContent, newContent);
TheCodedProfc3195b52023-06-23 15:53:00 -040076 const charsAdded = differences.filter((x) => x.added).length;
77 const charsRemoved = differences.filter((x) => x.removed).length;
78 const preLogMessage = await preLog(newMessage.guild.id, JSON.stringify(differences, null, 2));
79 if (!preLogMessage) return;
TheCodedProf4a7c25d2023-06-07 17:09:45 -040080 const data = {
81 meta: {
82 type: "messageUpdate",
83 displayName: "Message Edited",
84 calculateType: "messageUpdate",
85 color: NucleusColors.yellow,
86 emoji: "MESSAGE.EDIT",
87 timestamp: newMessage.editedTimestamp,
TheCodedProfc3195b52023-06-23 15:53:00 -040088 changes: { messageId: `${preLogMessage.id}`, buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit:${preLogMessage.id}` }
TheCodedProf4a7c25d2023-06-07 17:09:45 -040089 },
90 separate: {
TheCodedProfc3195b52023-06-23 15:53:00 -040091 start: `${charsAdded} ${addPlural(charsAdded, "character")} added, ${charsRemoved} ${addPlural(charsRemoved, "character")} removed`,
TheCodedProf4a7c25d2023-06-07 17:09:45 -040092 end: `[[Jump to message]](${newMessage.url})`
93 },
94 list: {
95 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
96 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
97 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
98 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
99 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
100 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
101 attachments: entry(
102 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
103 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
104 ),
105 repliedTo: entry(
106 replyTo ? replyTo.messageId! : null,
107 replyTo
108 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
109 : "None"
110 )
111 },
112 hidden: {
113 guild: newMessage.guild.id
114 }
115 };
116 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100117}