blob: aac83f44597c3b43862c72babc95605d524f8d75 [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
PineaFan538d3752023-01-12 21:48:23 +00002import Discord, { AuditLogEvent, GuildAuditLogsEntry, Message, User } from "discord.js";
pineafan0f5cc782022-08-12 21:55:42 +01003
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "messageDelete";
pineafan32767212022-03-14 21:27:39 +00005
PineaFan752af462022-12-31 21:59:38 +00006export async function callback(client: NucleusClient, message: Message) {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05007 if (message.author.id === client.user!.id) return;
8 if (message.author.bot) return;
9 if (client.noLog.includes(`${message.guild!.id}/${message.channel.id}/${message.id}`)) return;
10 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
11 if (!await isLogging(message.guild!.id, "messageDelete")) return;
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) {
15 if (auditLog.createdTimestamp - 1000 < Date.now()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010016 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050017 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",
39 timestamp: Date.now()
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)),
48 deleted: entry(Date.now(), renderDelta(Date.now())),
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);
pineafan32767212022-03-14 21:27:39 +000062}