PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 1 | import type { NucleusClient } from "../utils/client.js"; |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 2 | import { AttachmentBuilder, Message, MessageReference } 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"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 5 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 6 | export const event = "messageUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 7 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 8 | export async function callback(client: NucleusClient, oldMessage: Message, newMessage: Message) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 9 | if (newMessage.author.id === client.user!.id) return; |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 10 | if (newMessage.author.bot) return; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 11 | if (!newMessage.guild) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 12 | const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderNumberDelta, renderChannel } = |
| 13 | client.logger; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 14 | const replyTo: MessageReference | null = newMessage.reference; |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 15 | const newContent = newMessage.cleanContent.replaceAll("`", "‘"); |
| 16 | const oldContent = oldMessage.cleanContent.replaceAll("`", "‘"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | let attachmentJump = ""; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 18 | const config = (await client.database.guilds.read(newMessage.guild.id)).logging.attachments.saved[ |
| 19 | newMessage.channel.id + newMessage.id |
| 20 | ]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | if (config) { |
| 22 | attachmentJump = ` [[View attachments]](${config})`; |
| 23 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 24 | if (newMessage.crosspostable !== oldMessage.crosspostable) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 25 | if (!(await isLogging(newMessage.guild.id, "messageAnnounce"))) return; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 26 | if (!replyTo) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | const data = { |
| 28 | meta: { |
| 29 | type: "messageAnnounce", |
| 30 | displayName: "Message Published", |
| 31 | calculateType: "messageAnnounce", |
TheCodedProf | e00405f | 2023-03-05 14:44:23 -0500 | [diff] [blame] | 32 | color: NucleusColors.green, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | emoji: "MESSAGE.CREATE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 34 | timestamp: newMessage.editedTimestamp ?? Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | }, |
| 36 | separate: { |
| 37 | end: `[[Jump to message]](${newMessage.url})` |
| 38 | }, |
| 39 | list: { |
| 40 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 41 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 42 | sentIn: entry( |
| 43 | newMessage.channel.id, |
| 44 | renderChannel(newMessage.channel as Discord.GuildBasedChannel) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | ), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 46 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
PineappleFan | c1908c0 | 2023-03-08 21:20:30 +0000 | [diff] [blame] | 47 | published: entry(Date.now(), renderDelta(Date.now())), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 48 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | attachments: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 50 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 51 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | ) |
| 53 | }, |
| 54 | hidden: { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 55 | guild: newMessage.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 56 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | }; |
| 58 | return log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 59 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 61 | if (!(await isLogging(newMessage.guild.id, "messageUpdate"))) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | if (!newMessage.editedTimestamp) { |
| 63 | return; |
| 64 | } |
TheCodedProf | 32302c9 | 2023-06-06 17:01:08 -0400 | [diff] [blame] | 65 | const differences = diff.diffChars(oldContent, newContent); |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 66 | const green = "\x1B[36m"; |
| 67 | const red = "\x1B[41m"; |
| 68 | const skipped = "\x1B[40;33m"; |
| 69 | const reset = "\x1B[0m"; |
| 70 | const bold = "\x1B[1m"; |
| 71 | // console.log(differences); |
| 72 | // let contentAdd = ""; |
| 73 | // let contentRemove = ""; |
| 74 | // if (differences.map((d) => (d.added || d.removed ? 1 : 0)).filter((f) => f === 1).length > 0) { |
| 75 | // const cutoff = 20; |
| 76 | // differences.forEach((part) => { |
| 77 | // if (!part.added && !part.removed && part.value.length > cutoff) { |
| 78 | // contentAdd += |
| 79 | // reset + |
| 80 | // part.value.slice(0, cutoff / 2) + |
| 81 | // skipped + |
| 82 | // `(${part.value.length - cutoff} more)` + |
| 83 | // reset + |
| 84 | // part.value.slice(-(cutoff / 2)); |
| 85 | // contentRemove += |
| 86 | // reset + |
| 87 | // part.value.slice(0, cutoff / 2) + |
| 88 | // skipped + |
| 89 | // `(${part.value.length - cutoff} more)` + |
| 90 | // reset + |
| 91 | // part.value.slice(-(cutoff / 2)); |
| 92 | // } else { |
| 93 | // if (part.added || part.removed) { |
| 94 | // part.value = part.value.replaceAll(" ", "▁"); |
| 95 | // } |
| 96 | // if (part.added) { |
| 97 | // contentAdd += green + part.value + reset; |
| 98 | // } else if (part.removed) { |
| 99 | // contentRemove += red + part.value + reset; |
| 100 | // } else { |
| 101 | // contentAdd += part.value; |
| 102 | // contentRemove += part.value; |
| 103 | // } |
| 104 | // } |
| 105 | // }); |
| 106 | const key = `\n\n${bold}Key:${reset} ${green}Added${reset} | ${red}Removed${reset} | ${skipped}Skipped${reset}`; |
| 107 | const data = { |
| 108 | meta: { |
| 109 | type: "messageUpdate", |
| 110 | displayName: "Message Edited", |
| 111 | calculateType: "messageUpdate", |
| 112 | color: NucleusColors.yellow, |
| 113 | emoji: "MESSAGE.EDIT", |
| 114 | timestamp: newMessage.editedTimestamp, |
| 115 | files: [ |
TheCodedProf | 7b985d8 | 2023-06-08 16:40:41 -0400 | [diff] [blame] | 116 | new AttachmentBuilder(Buffer.from(JSON.stringify(differences), "base64"), { |
TheCodedProf | 4a7c25d | 2023-06-07 17:09:45 -0400 | [diff] [blame] | 117 | name: "diff.json", |
| 118 | description: "A JSON file containing the differences between the two messages." |
| 119 | }) |
| 120 | ], |
| 121 | showDetails: true |
| 122 | }, |
| 123 | separate: { |
| 124 | start: `To read the full log press the button below.\n\`\`\`ansi\n${key}\`\`\``, |
| 125 | end: `[[Jump to message]](${newMessage.url})` |
| 126 | }, |
| 127 | list: { |
| 128 | messageId: entry(newMessage.id, `\`${newMessage.id}\``), |
| 129 | sentBy: entry(newMessage.author.id, renderUser(newMessage.author)), |
| 130 | sentIn: entry(newMessage.channel.id, renderChannel(newMessage.channel as Discord.GuildBasedChannel)), |
| 131 | sent: entry(newMessage.createdTimestamp, renderDelta(newMessage.createdTimestamp)), |
| 132 | edited: entry(newMessage.editedTimestamp, renderDelta(newMessage.editedTimestamp)), |
| 133 | mentions: renderNumberDelta(oldMessage.mentions.users.size, newMessage.mentions.users.size), |
| 134 | attachments: entry( |
| 135 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size), |
| 136 | renderNumberDelta(oldMessage.attachments.size, newMessage.attachments.size) + attachmentJump |
| 137 | ), |
| 138 | repliedTo: entry( |
| 139 | replyTo ? replyTo.messageId! : null, |
| 140 | replyTo |
| 141 | ? `[[Jump to message]](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${replyTo.messageId})` |
| 142 | : "None" |
| 143 | ) |
| 144 | }, |
| 145 | hidden: { |
| 146 | guild: newMessage.guild.id |
| 147 | } |
| 148 | }; |
| 149 | await log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | } |