blob: 35de994723737d176f5dc883fa02f67162e2f14b [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";
pineafan435a8782023-06-24 12:45:58 +01006import { imageDataEasterEgg } from "../utils/defaults.js";
pineafan0f5cc782022-08-12 21:55:42 +01007
pineafan63fc5e22022-08-04 22:04:10 +01008export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00009
PineaFan752af462022-12-31 21:59:38 +000010export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
PineaFan538d3752023-01-12 21:48:23 +000011 if (newMessage.author.id === client.user!.id) return;
TheCodedProf4f79da12023-01-31 16:50:37 -050012 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010013 if (!newMessage.guild) return;
pineafan435a8782023-06-24 12:45:58 +010014 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } =
15 client.logger;
pineafan0f5cc782022-08-12 21:55:42 +010016 const replyTo: MessageReference | null = newMessage.reference;
TheCodedProf32302c92023-06-06 17:01:08 -040017 const newContent = newMessage.cleanContent.replaceAll("`", "‘");
18 const oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
pineafan63fc5e22022-08-04 22:04:10 +010019 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010020 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
21 newMessage.channel.id + newMessage.id
22 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010023 if (config) {
24 attachmentJump = ` [[View attachments]](${config})`;
25 }
TheCodedProf267563a2023-01-21 17:00:57 -050026 if (newMessage.crosspostable !== oldMessage.crosspostable) {
Skyler Greyda16adf2023-03-05 10:22:12 +000027 if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return;
PineaFan538d3752023-01-12 21:48:23 +000028 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010029 const data = {
30 meta: {
31 type: "messageAnnounce",
32 displayName: "Message Published",
33 calculateType: "messageAnnounce",
TheCodedProfe00405f2023-03-05 14:44:23 -050034 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010035 emoji: "MESSAGE.CREATE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050036 timestamp: newMessage.editedTimestamp ?? Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010037 },
38 separate: {
39 end: `[[Jump to message]](${newMessage.url})`
40 },
41 list: {
42 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010043 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
Skyler Greyda16adf2023-03-05 10:22:12 +000044 sentIn: entry(
45 newMessage.channel.id,
46 renderChannel(newMessage.channel as Discord.GuildBasedChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010047 ),
Skyler Greyda16adf2023-03-05 10:22:12 +000048 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
PineappleFanc1908c02023-03-08 21:20:30 +000049 published: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey11236ba2022-08-08 21:13:33 +010050 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010051 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010052 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
53 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010054 )
55 },
56 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010057 guild: newMessage.guild.id
pineafanda6e5342022-07-03 10:03:16 +010058 }
pineafan63fc5e22022-08-04 22:04:10 +010059 };
60 return log(data);
pineafan32767212022-03-14 21:27:39 +000061 }
pineafan63fc5e22022-08-04 22:04:10 +010062 }
Skyler Greyda16adf2023-03-05 10:22:12 +000063 if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010064 if (!newMessage.editedTimestamp) {
65 return;
66 }
TheCodedProf32302c92023-06-06 17:01:08 -040067 const differences = diff.diffChars(oldContent, newContent);
pineafan435a8782023-06-24 12:45:58 +010068 const charsAdded = differences
69 .filter((d) => d.added)
70 .map((d) => d.count)
71 .reduce((a, b) => a! + b!, 0)!;
72 const charsRemoved = differences
73 .filter((d) => d.removed)
74 .map((d) => d.count)
75 .reduce((a, b) => a! + b!, 0)!;
76 const imageData = JSON.stringify({ data: differences, extra: imageDataEasterEgg }, null, 2);
TheCodedProf4a7c25d2023-06-07 17:09:45 -040077 const data = {
78 meta: {
79 type: "messageUpdate",
80 displayName: "Message Edited",
81 calculateType: "messageUpdate",
82 color: NucleusColors.yellow,
83 emoji: "MESSAGE.EDIT",
84 timestamp: newMessage.editedTimestamp,
pineafan435a8782023-06-24 12:45:58 +010085 buttons: [{ buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:message.edit` }],
pineafan67c9f1f2023-06-23 22:50:26 +010086 imageData: imageData
TheCodedProf4a7c25d2023-06-07 17:09:45 -040087 },
88 separate: {
pineafan67c9f1f2023-06-23 22:50:26 +010089 start: `${addPlural(charsAdded, "character")} added, ${addPlural(charsRemoved, "character")} removed`,
TheCodedProf4a7c25d2023-06-07 17:09:45 -040090 end: `[[Jump to message]](${newMessage.url})`
91 },
92 list: {
93 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
94 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
95 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
96 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
97 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
98 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
99 attachments: entry(
100 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
101 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
102 ),
103 repliedTo: entry(
104 replyTo ? replyTo.messageId! : null,
105 replyTo
106 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
107 : "None"
108 )
109 },
110 hidden: {
111 guild: newMessage.guild.id
112 }
113 };
114 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100115}