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