blob: d4916416d6031b9087fb8f42f0097fb001b9dfc9 [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";
pineafan0f5cc782022-08-12 21:55:42 +01004
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "messageUpdate";
pineafan32767212022-03-14 21:27:39 +00006
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) {
PineaFan538d3752023-01-12 21:48:23 +00008 if (newMessage.author.id === client.user!.id) return;
TheCodedProf4f79da12023-01-31 16:50:37 -05009 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010010 if (!newMessage.guild) return;
11 const { log, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger;
12 const replyTo: MessageReference | null = newMessage.reference;
pineafan63fc5e22022-08-04 22:04:10 +010013 let newContent = newMessage.cleanContent.replaceAll("`", "‘");
14 let oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
15 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010016 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
17 newMessage.channel.id + newMessage.id
18 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010019 if (config) {
20 attachmentJump = ` [[View attachments]](${config})`;
21 }
TheCodedProf267563a2023-01-21 17:00:57 -050022 if (newMessage.crosspostable !== oldMessage.crosspostable) {
PineaFan538d3752023-01-12 21:48:23 +000023 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010024 const data = {
25 meta: {
26 type: "messageAnnounce",
27 displayName: "Message Published",
28 calculateType: "messageAnnounce",
29 color: NucleusColors.yellow,
30 emoji: "MESSAGE.CREATE",
31 timestamp: newMessage.editedTimestamp
32 },
33 separate: {
34 end: `[[Jump to message]](${newMessage.url})`
35 },
36 list: {
37 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010038 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
PineaFan538d3752023-01-12 21:48:23 +000039 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
Skyler Grey75ea9172022-08-06 10:22:23 +010040 sent: entry(
PineaFan538d3752023-01-12 21:48:23 +000041 newMessage.createdTimestamp,
42 renderDelta(newMessage.createdTimestamp)
Skyler Grey75ea9172022-08-06 10:22:23 +010043 ),
44 published: entry(
PineaFan538d3752023-01-12 21:48:23 +000045 newMessage.editedTimestamp!,
46 renderDelta(newMessage.editedTimestamp!)
Skyler Grey75ea9172022-08-06 10:22:23 +010047 ),
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 Grey75ea9172022-08-06 10:22:23 +010061 if (!newMessage.editedTimestamp) {
62 return;
63 }
Skyler Grey11236ba2022-08-08 21:13:33 +010064 if (newContent.length > 256) newContent = newContent.substring(0, 253) + "...";
65 if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "...";
pineafan63fc5e22022-08-04 22:04:10 +010066 const data = {
67 meta: {
68 type: "messageUpdate",
69 displayName: "Message Edited",
70 calculateType: "messageUpdate",
71 color: NucleusColors.yellow,
72 emoji: "MESSAGE.EDIT",
73 timestamp: newMessage.editedTimestamp
74 },
75 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 start:
77 (oldContent
78 ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n`
79 : "**Before:** *Message had no content*\n") +
Skyler Grey11236ba2022-08-08 21:13:33 +010080 (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"),
pineafan63fc5e22022-08-04 22:04:10 +010081 end: `[[Jump to message]](${newMessage.url})`
82 },
83 list: {
84 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
85 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
PineaFan538d3752023-01-12 21:48:23 +000086 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
87 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
88 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
Skyler Grey11236ba2022-08-08 21:13:33 +010089 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010090 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010091 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
92 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010093 ),
94 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000095 replyTo ? replyTo.messageId! : null,
pineafan0f5cc782022-08-12 21:55:42 +010096 replyTo
97 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010098 : "None"
pineafan63fc5e22022-08-04 22:04:10 +010099 )
100 },
101 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +0100102 guild: newMessage.guild.id
pineafan63fc5e22022-08-04 22:04:10 +0100103 }
104 };
105 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100106}