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