PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 1 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 2 | import Discord, { AuditLogEvent, ButtonStyle, GuildAuditLogsEntry, Message, User } from "discord.js"; |
| 3 | import { imageDataEasterEgg } from "../utils/defaults.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 4 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | export const event = "messageDelete"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 6 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 7 | export async function callback(client: NucleusClient, message: Message) { |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 8 | if (message.author.id === client.user!.id) return; |
| 9 | if (message.author.bot) return; |
TheCodedProf | 5b53a8c | 2023-02-03 15:40:26 -0500 | [diff] [blame] | 10 | if (client.noLog.includes(`${message.guild!.id}/${message.channel.id}/${message.id}`)) return; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 11 | const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 12 | if (!(await isLogging(message.guild!.id, "messageDelete"))) return; |
| 13 | const auditLog = (await getAuditLog(message.guild!, AuditLogEvent.MemberBanAdd)).filter( |
| 14 | (entry: GuildAuditLogsEntry) => (entry.target! as User).id === message.author.id |
| 15 | )[0]; |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 16 | if (auditLog) { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 17 | if (auditLog.createdTimestamp - 1000 < Date.now()) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | } |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 19 | const replyTo = message.reference; |
| 20 | let content = message.cleanContent; |
| 21 | content.replace("`", "\\`"); |
| 22 | if (content.length > 256) content = content.substring(0, 253) + "..."; |
| 23 | const attachments = |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 24 | message.attachments.size + |
| 25 | ( |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 26 | message.content.match( |
| 27 | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi |
| 28 | ) ?? [] |
| 29 | ).length; |
| 30 | let attachmentJump = ""; |
| 31 | const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[ |
| 32 | message.channel.id + message.id |
| 33 | ]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 34 | if (config) { |
| 35 | attachmentJump = ` [[View attachments]](${config})`; |
| 36 | } |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 37 | const imageData = JSON.stringify({ data: message.content, extra: imageDataEasterEgg }, null, 2); |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 38 | const data = { |
| 39 | meta: { |
| 40 | type: "messageDelete", |
| 41 | displayName: "Message Deleted", |
| 42 | calculateType: "messageDelete", |
| 43 | color: NucleusColors.red, |
| 44 | emoji: "MESSAGE.DELETE", |
pineafan | 435a878 | 2023-06-24 12:45:58 +0100 | [diff] [blame] | 45 | timestamp: Date.now(), |
| 46 | imageData: imageData, |
| 47 | buttons: [{ buttonText: "View text", buttonId: "log:message.delete", buttonStyle: ButtonStyle.Secondary }] |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 48 | }, |
| 49 | separate: { |
| 50 | start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*" |
| 51 | }, |
| 52 | list: { |
| 53 | messageId: entry(message.id, `\`${message.id}\``), |
| 54 | sentBy: entry(message.author.id, renderUser(message.author)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 55 | sentIn: entry( |
| 56 | message.channel.id, |
| 57 | renderChannel(message.channel as Discord.GuildChannel | Discord.ThreadChannel) |
| 58 | ), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 59 | deleted: entry(Date.now(), renderDelta(Date.now())), |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 60 | mentions: message.mentions.users.size, |
| 61 | attachments: entry(attachments, attachments + attachmentJump), |
| 62 | repliedTo: entry( |
| 63 | replyTo ? replyTo.messageId! : null, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 64 | replyTo |
| 65 | ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${ |
| 66 | replyTo.messageId |
| 67 | })` |
| 68 | : "None" |
TheCodedProf | 4f79da1 | 2023-01-31 16:50:37 -0500 | [diff] [blame] | 69 | ) |
| 70 | }, |
| 71 | hidden: { |
| 72 | guild: message.guild!.id |
| 73 | } |
| 74 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 75 | await log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 76 | } |