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