PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 1 | import type { NucleusClient } from "../utils/client.js"; |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 2 | import { Message, MessageReference, ButtonStyle } from "discord.js"; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 3 | import type Discord from "discord.js"; |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 4 | import * as diff from "diff"; |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 5 | import addPlural from "../utils/plurals.js"; |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame^] | 6 | import { imageDataEasterEgg } from "../utils/defaults.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 7 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | export const event = "messageUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 10 | export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 11 | if (newMessage.author.id === client.user!.id) return; |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 12 | if (newMessage.author.bot) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 13 | if (!newMessage.guild) return; |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame^] | 14 | const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = |
| 15 | client.logger; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 16 | const replyTo: MessageReference | null = newMessage.reference; |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 17 | const newContent = newMessage.cleanContent.replaceAll("`", "‘"); |
| 18 | const oldContent = oldMessage.cleanContent.replaceAll("`", "‘"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | let attachmentJump = ""; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 20 | const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[ |
| 21 | newMessage.channel.id + newMessage.id |
| 22 | ]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | if (config) { |
| 24 | attachmentJump = ` [[View attachments]](${config})`; |
| 25 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 26 | if (newMessage.crosspostable !== oldMessage.crosspostable) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 27 | if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 28 | if (!replyTo) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | const data = { |
| 30 | meta: { |
| 31 | type: "messageAnnounce", |
| 32 | displayName: "Message Published", |
| 33 | calculateType: "messageAnnounce", |
TheCodedProf | e00405f | 2023-03-05 14:44:23 -0500 | [diff] [blame] | 34 | color: NucleusColors.green, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | emoji: "MESSAGE.CREATE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 36 | timestamp: newMessage.editedTimestamp ?? Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | }, |
| 38 | separate: { |
| 39 | end: `[[Jump to message]](${newMessage.url})` |
| 40 | }, |
| 41 | list: { |
| 42 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 43 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 44 | sentIn: entry( |
| 45 | newMessage.channel.id, |
| 46 | renderChannel(newMessage.channel as Discord.GuildBasedChannel) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 47 | ), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
PineappleFan | c1908c0 | 2023-03-08 21:20:30 +0000 | [diff] [blame] | 49 | published: entry(Date.now(), renderDelta(Date.now())), |
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 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [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 | } |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 67 | const differences = diff.diffChars(oldContent, newContent); |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame^] | 68 | const charsAdded = differences |
| 69 | .filter((d) => d.added) |
| 70 | .map((d) => d.count) |
| 71 | .reduce((a, b) => a! + b!, 0)!; |
| 72 | const charsRemoved = differences |
| 73 | .filter((d) => d.removed) |
| 74 | .map((d) => d.count) |
| 75 | .reduce((a, b) => a! + b!, 0)!; |
| 76 | const imageData = JSON.stringify({ data: differences, extra: imageDataEasterEgg }, null, 2); |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 77 | const data = { |
| 78 | meta: { |
| 79 | type: "messageUpdate", |
| 80 | displayName: "Message Edited", |
| 81 | calculateType: "messageUpdate", |
| 82 | color: NucleusColors.yellow, |
| 83 | emoji: "MESSAGE.EDIT", |
| 84 | timestamp: newMessage.editedTimestamp, |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame^] | 85 | buttons: [{ buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:message.edit` }], |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame] | 86 | imageData: imageData |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 87 | }, |
| 88 | separate: { |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame] | 89 | start: `${addPlural(charsAdded, "character")} added, ${addPlural(charsRemoved, "character")} removed`, |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 90 | end: `[[Jump to message]](${newMessage.url})` |
| 91 | }, |
| 92 | list: { |
| 93 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
| 94 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
| 95 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
| 96 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
| 97 | edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)), |
| 98 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
| 99 | attachments: entry( |
| 100 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 101 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
| 102 | ), |
| 103 | repliedTo: entry( |
| 104 | replyTo ? replyTo.messageId! : null, |
| 105 | replyTo |
| 106 | ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})` |
| 107 | : "None" |
| 108 | ) |
| 109 | }, |
| 110 | hidden: { |
| 111 | guild: newMessage.guild.id |
| 112 | } |
| 113 | }; |
| 114 | await log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 115 | } |