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) {
TheCodedProf4f79da12023-01-31 16:50:37 -05007 if (message.author.id === client.user!.id) return;
8 if (message.author.bot) return;
TheCodedProf5b53a8c2023-02-03 15:40:26 -05009 if (client.noLog.includes(`${message.guild!.id}/${message.channel.id}/${message.id}`)) return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050010 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
11 if (!await isLogging(message.guild!.id, "messageDelete")) return;
TheCodedProf4f79da12023-01-31 16:50:37 -050012 const auditLog = (await getAuditLog(message.guild!, AuditLogEvent.MemberBanAdd))
13 .filter((entry: GuildAuditLogsEntry) => (entry.target! as User).id === message.author.id)[0];
14 if (auditLog) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050015 if (auditLog.createdTimestamp - 1000 < Date.now()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010016 }
TheCodedProf4f79da12023-01-31 16:50:37 -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",
TheCodedProf6ec331b2023-02-20 12:13:06 -050039 timestamp: Date.now()
TheCodedProf4f79da12023-01-31 16:50:37 -050040 },
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)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050048 deleted: entry(Date.now(), renderDelta(Date.now())),
TheCodedProf4f79da12023-01-31 16:50:37 -050049 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}