blob: bca652e3bccace7b83ed5765cb777dff3332f0aa [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";
TheCodedProf32302c92023-06-06 17:01:08 -04004import * as diff from "diff";
pineafan0f5cc782022-08-12 21:55:42 +01005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00007
PineaFan752af462022-12-31 21:59:38 +00008export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
PineaFan538d3752023-01-12 21:48:23 +00009 if (newMessage.author.id === client.user!.id) return;
TheCodedProf4f79da12023-01-31 16:50:37 -050010 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010011 if (!newMessage.guild) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000012 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } =
13 client.logger;
pineafan0f5cc782022-08-12 21:55:42 +010014 const replyTo: MessageReference | null = newMessage.reference;
TheCodedProf32302c92023-06-06 17:01:08 -040015 const newContent = newMessage.cleanContent.replaceAll("`", "‘");
16 const oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
pineafan63fc5e22022-08-04 22:04:10 +010017 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 }
TheCodedProf32302c92023-06-06 17:01:08 -040065 const differences = diff.diffChars(oldContent, newContent);
66 console.log(differences);
67 let contentEdit = "";
68 if (differences.map((d) => (d.added || d.removed ? 1 : 0)).filter((f) => f === 1).length > 0) {
69 const green = "\x1B[36m";
70 const red = "\x1B[41m";
71 const skipped = "\x1B[40;33m";
72 const reset = "\x1B[0m";
73 const bold = "\x1B[1m";
74 const cutoff = 20;
75 differences.forEach((part) => {
76 if (!part.added && !part.removed && part.value.length > cutoff) {
77 contentEdit +=
78 reset +
79 part.value.slice(0, cutoff / 2) +
80 skipped +
81 `(${part.value.length - cutoff} more)` +
82 reset +
83 part.value.slice(-(cutoff / 2));
84 } else {
85 if (part.added || part.removed) {
86 part.value = part.value.replaceAll(" ", "▁");
87 }
TheCodedProf9d9ce0e2023-06-06 17:16:02 -040088 contentEdit += (part.added ? green : part.removed ? red : "") + part.value + reset;
TheCodedProf32302c92023-06-06 17:01:08 -040089 }
90 });
91 contentEdit = contentEdit.slice(0, 2000);
92 contentEdit += `\n\n${bold}Key:${reset} ${green}Added${reset} | ${red}Removed${reset} | ${skipped}Skipped${reset}`;
93 const data = {
94 meta: {
95 type: "messageUpdate",
96 displayName: "Message Edited",
97 calculateType: "messageUpdate",
98 color: NucleusColors.yellow,
99 emoji: "MESSAGE.EDIT",
100 timestamp: newMessage.editedTimestamp
101 },
102 separate: {
103 start: `\`\`\`ansi\n${contentEdit}\`\`\``,
104 end: `[[Jump to message]](${newMessage.url})`
105 },
106 list: {
107 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
108 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
109 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
110 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
111 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
112 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
113 attachments: entry(
114 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
115 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
116 ),
117 repliedTo: entry(
118 replyTo ? replyTo.messageId! : null,
119 replyTo
120 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
121 : "None"
122 )
123 },
124 hidden: {
125 guild: newMessage.guild.id
126 }
127 };
128 await log(data);
129 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100130}