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; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 9 | if (!newMessage.guild) return; |
| 10 | const { log, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = client.logger; |
| 11 | const replyTo: MessageReference | null = newMessage.reference; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | let newContent = newMessage.cleanContent.replaceAll("`", "‘"); |
| 13 | let oldContent = oldMessage.cleanContent.replaceAll("`", "‘"); |
| 14 | let attachmentJump = ""; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[ |
| 16 | newMessage.channel.id + newMessage.id |
| 17 | ]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | if (config) { |
| 19 | attachmentJump = ` [[View attachments]](${config})`; |
| 20 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 21 | if (newMessage.crosspostable !== oldMessage.crosspostable) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 22 | if (!replyTo) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const data = { |
| 24 | meta: { |
| 25 | type: "messageAnnounce", |
| 26 | displayName: "Message Published", |
| 27 | calculateType: "messageAnnounce", |
| 28 | color: NucleusColors.yellow, |
| 29 | emoji: "MESSAGE.CREATE", |
| 30 | timestamp: newMessage.editedTimestamp |
| 31 | }, |
| 32 | separate: { |
| 33 | end: `[[Jump to message]](${newMessage.url})` |
| 34 | }, |
| 35 | list: { |
| 36 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 37 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 38 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | sent: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 40 | newMessage.createdTimestamp, |
| 41 | renderDelta(newMessage.createdTimestamp) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | ), |
| 43 | published: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 44 | newMessage.editedTimestamp!, |
| 45 | renderDelta(newMessage.editedTimestamp!) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 49 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 50 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | ) |
| 52 | }, |
| 53 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 54 | guild: newMessage.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 55 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | }; |
| 57 | return log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | if (!newMessage.editedTimestamp) { |
| 61 | return; |
| 62 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | if (newContent.length > 256) newContent = newContent.substring(0, 253) + "..."; |
| 64 | if (oldContent.length > 256) oldContent = oldContent.substring(0, 253) + "..."; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | const data = { |
| 66 | meta: { |
| 67 | type: "messageUpdate", |
| 68 | displayName: "Message Edited", |
| 69 | calculateType: "messageUpdate", |
| 70 | color: NucleusColors.yellow, |
| 71 | emoji: "MESSAGE.EDIT", |
| 72 | timestamp: newMessage.editedTimestamp |
| 73 | }, |
| 74 | separate: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | start: |
| 76 | (oldContent |
| 77 | ? `**Before:**\n\`\`\`\n${oldContent}\n\`\`\`\n` |
| 78 | : "**Before:** *Message had no content*\n") + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 79 | (newContent ? `**After:**\n\`\`\`\n${newContent}\n\`\`\`` : "**After:** *Message had no content*"), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | end: `[[Jump to message]](${newMessage.url})` |
| 81 | }, |
| 82 | list: { |
| 83 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
| 84 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 85 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
| 86 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
| 87 | edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 88 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 90 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 91 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | ), |
| 93 | repliedTo: entry( |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 94 | replyTo ? replyTo.messageId! : null, |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 95 | replyTo |
| 96 | ? `[[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] | 97 | : "None" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 98 | ) |
| 99 | }, |
| 100 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 101 | guild: newMessage.guild.id |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | } |
| 103 | }; |
| 104 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | } |