blob: f41901cc3cc392dc8b7579581a3339612d1f7bad [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
TheCodedProf4a7c25d2023-06-07 17:09:45 -04002import { AttachmentBuilder, 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);
TheCodedProf4a7c25d2023-06-07 17:09:45 -040066 const green = "\x1B[36m";
67 const red = "\x1B[41m";
68 const skipped = "\x1B[40;33m";
69 const reset = "\x1B[0m";
70 const bold = "\x1B[1m";
71 // console.log(differences);
72 // let contentAdd = "";
73 // let contentRemove = "";
74 // if (differences.map((d) => (d.added || d.removed ? 1 : 0)).filter((f) => f === 1).length > 0) {
75 // const cutoff = 20;
76 // differences.forEach((part) => {
77 // if (!part.added && !part.removed && part.value.length > cutoff) {
78 // contentAdd +=
79 // reset +
80 // part.value.slice(0, cutoff / 2) +
81 // skipped +
82 // `(${part.value.length - cutoff} more)` +
83 // reset +
84 // part.value.slice(-(cutoff / 2));
85 // contentRemove +=
86 // reset +
87 // part.value.slice(0, cutoff / 2) +
88 // skipped +
89 // `(${part.value.length - cutoff} more)` +
90 // reset +
91 // part.value.slice(-(cutoff / 2));
92 // } else {
93 // if (part.added || part.removed) {
94 // part.value = part.value.replaceAll(" ", "▁");
95 // }
96 // if (part.added) {
97 // contentAdd += green + part.value + reset;
98 // } else if (part.removed) {
99 // contentRemove += red + part.value + reset;
100 // } else {
101 // contentAdd += part.value;
102 // contentRemove += part.value;
103 // }
104 // }
105 // });
106 const key = `\n\n${bold}Key:${reset} ${green}Added${reset} | ${red}Removed${reset} | ${skipped}Skipped${reset}`;
107 const data = {
108 meta: {
109 type: "messageUpdate",
110 displayName: "Message Edited",
111 calculateType: "messageUpdate",
112 color: NucleusColors.yellow,
113 emoji: "MESSAGE.EDIT",
114 timestamp: newMessage.editedTimestamp,
115 files: [
TheCodedProf7b985d82023-06-08 16:40:41 -0400116 new AttachmentBuilder(Buffer.from(JSON.stringify(differences), "base64"), {
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400117 name: "diff.json",
118 description: "A JSON file containing the differences between the two messages."
119 })
120 ],
121 showDetails: true
122 },
123 separate: {
124 start: `To read the full log press the button below.\n\`\`\`ansi\n${key}\`\`\``,
125 end: `[[Jump to message]](${newMessage.url})`
126 },
127 list: {
128 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
129 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
130 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
131 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
132 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
133 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
134 attachments: entry(
135 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
136 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
137 ),
138 repliedTo: entry(
139 replyTo ? replyTo.messageId! : null,
140 replyTo
141 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
142 : "None"
143 )
144 },
145 hidden: {
146 guild: newMessage.guild.id
147 }
148 };
149 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100150}