PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 1 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 2 | import type { Message, MessageReference } from "discord.js"; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 3 | import type Discord from "discord.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 4 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | export const event = "messageUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 6 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 7 | export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 8 | if (newMessage.author.id === client.user!.id) return; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 9 | if (newMessage.author.bot) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 10 | if (!newMessage.guild) return; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 11 | const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger; |
| 12 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 13 | const replyTo: MessageReference | null = newMessage.reference; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | let newContent = newMessage.cleanContent.replaceAll("`", "‘"); |
| 15 | let oldContent = oldMessage.cleanContent.replaceAll("`", "‘"); |
| 16 | let attachmentJump = ""; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 17 | const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[ |
| 18 | newMessage.channel.id + newMessage.id |
| 19 | ]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 20 | if (config) { |
| 21 | attachmentJump = ` [[View attachments]](${config})`; |
| 22 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 23 | if (newMessage.crosspostable !== oldMessage.crosspostable) { |
| 24 | if(!await isLogging(newMessage.guild.id, "messageAnnounce")) return; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 25 | if (!replyTo) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 26 | const data = { |
| 27 | meta: { |
| 28 | type: "messageAnnounce", |
| 29 | displayName: "Message Published", |
| 30 | calculateType: "messageAnnounce", |
| 31 | color: NucleusColors.yellow, |
| 32 | emoji: "MESSAGE.CREATE", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 33 | timestamp: newMessage.editedTimestamp ?? Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | }, |
| 35 | separate: { |
| 36 | end: `[[Jump to message]](${newMessage.url})` |
| 37 | }, |
| 38 | list: { |
| 39 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 40 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 41 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | sent: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 43 | newMessage.createdTimestamp, |
| 44 | renderDelta(newMessage.createdTimestamp) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | ), |
| 46 | published: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 47 | newMessage.editedTimestamp!, |
| 48 | renderDelta(newMessage.editedTimestamp!) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 50 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 52 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 53 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | ) |
| 55 | }, |
| 56 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 57 | guild: newMessage.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | }; |
| 60 | return log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 61 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 63 | if (!await isLogging(newMessage.guild.id, "messageUpdate")) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | if (!newMessage.editedTimestamp) { |
| 65 | return; |
| 66 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 67 | if (newContent.length > 256) newContent = newContent.substring(0, 253) + "..."; |
| 68 | if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "..."; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | start: |
| 80 | (oldContent |
| 81 | ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n` |
| 82 | : "**Before:** *Message had no content*\n") + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 83 | (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 84 | 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)), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 89 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 92 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 94 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 95 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 96 | ), |
| 97 | repliedTo: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 98 | replyTo ? replyTo.messageId! : null, |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 99 | replyTo |
| 100 | ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | : "None" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | ) |
| 103 | }, |
| 104 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 105 | guild: newMessage.guild.id |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | } |
| 107 | }; |
| 108 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | } |