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 | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 6 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | export const event = "messageUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 9 | export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 10 | if (newMessage.author.id === client.user!.id) return; |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 11 | if (newMessage.author.bot) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 12 | if (!newMessage.guild) return; |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 13 | const { |
| 14 | log, |
| 15 | isLogging, |
| 16 | NucleusColors, |
| 17 | entry, |
| 18 | renderUser, |
| 19 | renderDelta, |
| 20 | renderNumberDelta, |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame^] | 21 | renderChannel |
TheCodedProf | c3195b5 | 2023-06-23 15:53:00 -0400 | [diff] [blame] | 22 | } = client.logger; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 23 | const replyTo: MessageReference | null = newMessage.reference; |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 24 | const newContent = newMessage.cleanContent.replaceAll("`", "‘"); |
| 25 | const oldContent = oldMessage.cleanContent.replaceAll("`", "‘"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 26 | let attachmentJump = ""; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 27 | const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[ |
| 28 | newMessage.channel.id + newMessage.id |
| 29 | ]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | if (config) { |
| 31 | attachmentJump = ` [[View attachments]](${config})`; |
| 32 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 33 | if (newMessage.crosspostable !== oldMessage.crosspostable) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 34 | if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 35 | if (!replyTo) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | const data = { |
| 37 | meta: { |
| 38 | type: "messageAnnounce", |
| 39 | displayName: "Message Published", |
| 40 | calculateType: "messageAnnounce", |
TheCodedProf | e00405f | 2023-03-05 14:44:23 -0500 | [diff] [blame] | 41 | color: NucleusColors.green, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | emoji: "MESSAGE.CREATE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 43 | timestamp: newMessage.editedTimestamp ?? Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | }, |
| 45 | separate: { |
| 46 | end: `[[Jump to message]](${newMessage.url})` |
| 47 | }, |
| 48 | list: { |
| 49 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 50 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 51 | sentIn: entry( |
| 52 | newMessage.channel.id, |
| 53 | renderChannel(newMessage.channel as Discord.GuildBasedChannel) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | ), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 55 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
PineappleFan | c1908c0 | 2023-03-08 21:20:30 +0000 | [diff] [blame] | 56 | published: entry(Date.now(), renderDelta(Date.now())), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 57 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 59 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 60 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | ) |
| 62 | }, |
| 63 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 64 | guild: newMessage.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 65 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | }; |
| 67 | return log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 68 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 70 | if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | if (!newMessage.editedTimestamp) { |
| 72 | return; |
| 73 | } |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 74 | const differences = diff.diffChars(oldContent, newContent); |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame^] | 75 | const charsAdded = (differences.filter((d) => d.added).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!; |
| 76 | const charsRemoved = (differences.filter((d) => d.removed).map((d) => d.count)).reduce((a, b) => a! + b!, 0)!; |
| 77 | const imageData = JSON.stringify({data: differences, extra: "The image in this embed contains data about the below log.\n" + |
| 78 | "It isn't designed to be read by humans, but you can decode " + |
| 79 | "it with any base64 decoder, and then read it as JSON.\n" + |
| 80 | "We use base 64 to get around people using virus tests and the file being blocked, and an image to have the embed hidden (files can't be suppressed)\n" + |
| 81 | "If you've got to this point and are reading this hidden message, you should come and work with us " + |
| 82 | "at https://discord.gg/w35pXdrxKW (Internal development server) and let us know how you got here."}, null, 2) |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 83 | const data = { |
| 84 | meta: { |
| 85 | type: "messageUpdate", |
| 86 | displayName: "Message Edited", |
| 87 | calculateType: "messageUpdate", |
| 88 | color: NucleusColors.yellow, |
| 89 | emoji: "MESSAGE.EDIT", |
| 90 | timestamp: newMessage.editedTimestamp, |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame^] | 91 | buttons: [{ buttonText: "View Changes", buttonStyle: ButtonStyle.Secondary, buttonId: `log:edit` }], |
| 92 | imageData: imageData |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 93 | }, |
| 94 | separate: { |
pineafan | 67c9f1f | 2023-06-23 22:50:26 +0100 | [diff] [blame^] | 95 | start: `${addPlural(charsAdded, "character")} added, ${addPlural(charsRemoved, "character")} removed`, |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 96 | end: `[[Jump to message]](${newMessage.url})` |
| 97 | }, |
| 98 | list: { |
| 99 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
| 100 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
| 101 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
| 102 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
| 103 | edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)), |
| 104 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
| 105 | attachments: entry( |
| 106 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 107 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
| 108 | ), |
| 109 | repliedTo: entry( |
| 110 | replyTo ? replyTo.messageId! : null, |
| 111 | replyTo |
| 112 | ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})` |
| 113 | : "None" |
| 114 | ) |
| 115 | }, |
| 116 | hidden: { |
| 117 | guild: newMessage.guild.id |
| 118 | } |
| 119 | }; |
| 120 | await log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | } |