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