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