blob: f5a28a41ee72be4b4d4744b72468fe8054804174 [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;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05009 if (newMessage.author.bot) return;
pineafan0f5cc782022-08-12 21:55:42 +010010 if (!newMessage.guild) return;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050011 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger;
12
pineafan0f5cc782022-08-12 21:55:42 +010013 const replyTo: MessageReference | null = newMessage.reference;
pineafan63fc5e22022-08-04 22:04:10 +010014 let newContent = newMessage.cleanContent.replaceAll("`", "‘");
15 let oldContent = oldMessage.cleanContent.replaceAll("`", "‘");
16 let attachmentJump = "";
Skyler Grey11236ba2022-08-08 21:13:33 +010017 const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[
18 newMessage.channel.id + newMessage.id
19 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010020 if (config) {
21 attachmentJump = ` [[View attachments]](${config})`;
22 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050023 if (newMessage.crosspostable !== oldMessage.crosspostable) {
24 if(!await isLogging(newMessage.guild.id, "messageAnnounce")) return;
PineaFan538d3752023-01-12 21:48:23 +000025 if (!replyTo) {
pineafan63fc5e22022-08-04 22:04:10 +010026 const data = {
27 meta: {
28 type: "messageAnnounce",
29 displayName: "Message Published",
30 calculateType: "messageAnnounce",
31 color: NucleusColors.yellow,
32 emoji: "MESSAGE.CREATE",
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050033 timestamp: newMessage.editedTimestamp ?? Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010034 },
35 separate: {
36 end: `[[Jump to message]](${newMessage.url})`
37 },
38 list: {
39 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
Skyler Grey11236ba2022-08-08 21:13:33 +010040 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
PineaFan538d3752023-01-12 21:48:23 +000041 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
Skyler Grey75ea9172022-08-06 10:22:23 +010042 sent: entry(
PineaFan538d3752023-01-12 21:48:23 +000043 newMessage.createdTimestamp,
44 renderDelta(newMessage.createdTimestamp)
Skyler Grey75ea9172022-08-06 10:22:23 +010045 ),
46 published: entry(
PineaFan538d3752023-01-12 21:48:23 +000047 newMessage.editedTimestamp!,
48 renderDelta(newMessage.editedTimestamp!)
Skyler Grey75ea9172022-08-06 10:22:23 +010049 ),
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 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050063 if (!await isLogging(newMessage.guild.id, "messageUpdate")) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010064 if (!newMessage.editedTimestamp) {
65 return;
66 }
Skyler Grey11236ba2022-08-08 21:13:33 +010067 if (newContent.length > 256) newContent = newContent.substring(0, 253) + "...";
68 if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "...";
pineafan63fc5e22022-08-04 22:04:10 +010069 const data = {
70 meta: {
71 type: "messageUpdate",
72 displayName: "Message Edited",
73 calculateType: "messageUpdate",
74 color: NucleusColors.yellow,
75 emoji: "MESSAGE.EDIT",
76 timestamp: newMessage.editedTimestamp
77 },
78 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 start:
80 (oldContent
81 ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n`
82 : "**Before:** *Message had no content*\n") +
Skyler Grey11236ba2022-08-08 21:13:33 +010083 (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"),
pineafan63fc5e22022-08-04 22:04:10 +010084 end: `[[Jump to message]](${newMessage.url})`
85 },
86 list: {
87 messageId: entry(newMessage.id, `\`${newMessage.id}\``),
88 sentBy: entry(newMessage.author.id, renderUser(newMessage.author)),
PineaFan538d3752023-01-12 21:48:23 +000089 sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)),
90 sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)),
91 edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)),
Skyler Grey11236ba2022-08-08 21:13:33 +010092 mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size),
pineafan63fc5e22022-08-04 22:04:10 +010093 attachments: entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010094 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size),
95 renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump
pineafan63fc5e22022-08-04 22:04:10 +010096 ),
97 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000098 replyTo ? replyTo.messageId! : null,
pineafan0f5cc782022-08-12 21:55:42 +010099 replyTo
100 ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 : "None"
pineafan63fc5e22022-08-04 22:04:10 +0100102 )
103 },
104 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +0100105 guild: newMessage.guild.id
pineafan63fc5e22022-08-04 22:04:10 +0100106 }
107 };
108 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100109}