blob: f61a8739c80257547d1df19deb5d3e6fe003aef1 [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,
pineafan67c9f1f2023-06-23 22:50:26 +010021 renderChannel
TheCodedProfc3195b52023-06-23 15:53:00 -040022 } = client.logger;
pineafan0f5cc782022-08-12 21:55:42 +010023 const replyTo: MessageReference | null = newMessage.reference;
TheCodedProf32302c92023-06-06 17:01:08 -040024 const newContent = newMessage.cleanContent.replaceAll("`", "‘");
25 const oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
pineafan63fc5e22022-08-04 22:04:10 +010026 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010027 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
28 newMessage.channel.id + newMessage.id
29 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (config) {
31 attachmentJump = ` [[View attachments]](${config})`;
32 }
TheCodedProf267563a2023-01-21 17:00:57 -050033 if (newMessage.crosspostable !== oldMessage.crosspostable) {
Skyler Greyda16adf2023-03-05 10:22:12 +000034 if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return;
PineaFan538d3752023-01-12 21:48:23 +000035 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010036 const data = {
37 meta: {
38 type: "messageAnnounce",
39 displayName: "Message Published",
40 calculateType: "messageAnnounce",
TheCodedProfe00405f2023-03-05 14:44:23 -050041 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010042 emoji: "MESSAGE.CREATE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050043 timestamp: newMessage.editedTimestamp ?? Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010044 },
45 separate: {
46 end: `[[Jump to message]](${newMessage.url})`
47 },
48 list: {
49 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010050 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Greyda16adf2023-03-05 10:22:12 +000051 sentIn: entry(
52 newMessage.channel.id,
53 renderChannel(newMessage.channel as Discord.GuildBasedChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010054 ),
Skyler Greyda16adf2023-03-05 10:22:12 +000055 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
PineappleFanc1908c02023-03-08 21:20:30 +000056 published: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey11236ba2022-08-08 21:13:33 +010057 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010058 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010059 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
60 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010061 )
62 },
63 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010064 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010065 }
pineafan63fc5e22022-08-04 22:04:10 +010066 };
67 return log(data);
pineafan32767212022-03-14 21:27:39 +000068 }
pineafan63fc5e22022-08-04 22:04:10 +010069 }
Skyler Greyda16adf2023-03-05 10:22:12 +000070 if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010071 if (!newMessage.editedTimestamp) {
72 return;
73 }
TheCodedProf32302c92023-06-06 17:01:08 -040074 const differences = diff.diffChars(oldContent, newContent);
pineafan67c9f1f2023-06-23 22:50:26 +010075 const charsAdded = (differences.filter((d) => d.added).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!;
76 const charsRemoved = (differences.filter((d) => d.removed).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!;
77 const imageData = JSON.stringify({data: differences, extra: "The image in this embed contains data about the below log.\n" +
78 "It isn't designed to be read by humans, but you can decode " +
79 "it with any base64 decoder, and then read it as JSON.\n" +
80 "We use base 64 to get around people using virus tests and the file being blocked, and an image to have the embed hidden (files can't be suppressed)\n" +
81 "If you've got to this point and are reading this hidden message, you should come and work with us " +
82 "at https://discord.gg/w35pXdrxKW (Internal development server) and let us know how you got here."}, null, 2)
TheCodedProf4a7c25d2023-06-07 17:09:45 -040083 const data = {
84 meta: {
85 type: "messageUpdate",
86 displayName: "Message Edited",
87 calculateType: "messageUpdate",
88 color: NucleusColors.yellow,
89 emoji: "MESSAGE.EDIT",
90 timestamp: newMessage.editedTimestamp,
pineafan67c9f1f2023-06-23 22:50:26 +010091 buttons: [{ buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit` }],
92 imageData: imageData
TheCodedProf4a7c25d2023-06-07 17:09:45 -040093 },
94 separate: {
pineafan67c9f1f2023-06-23 22:50:26 +010095 start: `${addPlural(charsAdded, "character")} added, ${addPlural(charsRemoved, "character")} removed`,
TheCodedProf4a7c25d2023-06-07 17:09:45 -040096 end: `[[Jump to message]](${newMessage.url})`
97 },
98 list: {
99 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
100 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
101 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
102 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
103 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
104 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
105 attachments: entry(
106 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
107 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
108 ),
109 repliedTo: entry(
110 replyTo ? replyTo.messageId! : null,
111 replyTo
112 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
113 : "None"
114 )
115 },
116 hidden: {
117 guild: newMessage.guild.id
118 }
119 };
120 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100121}