blob: c29bfd09ca43417fda69330a93ff52d7306e51f5 [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;
Skyler Greyda16adf2023-03-05 10:22:12 +000011 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];
TheCodedProf4f79da12023-01-31 16:50:37 -050015 if (auditLog) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050016 if (auditLog.createdTimestamp - 1000 < Date.now()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010017 }
TheCodedProf4f79da12023-01-31 16:50:37 -050018 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 Greyda16adf2023-03-05 10:22:12 +000023 message.attachments.size +
24 (
TheCodedProf4f79da12023-01-31 16:50:37 -050025 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 Greyda16adf2023-03-05 10:22:12 +000033 if (config) {
34 attachmentJump = ` [[View attachments]](${config})`;
35 }
TheCodedProf4f79da12023-01-31 16:50:37 -050036 const data = {
37 meta: {
38 type: "messageDelete",
39 displayName: "Message Deleted",
40 calculateType: "messageDelete",
41 color: NucleusColors.red,
42 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050043 timestamp: Date.now()
TheCodedProf4f79da12023-01-31 16:50:37 -050044 },
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 Greyda16adf2023-03-05 10:22:12 +000051 sentIn: entry(
52 message.channel.id,
53 renderChannel(message.channel as Discord.GuildChannel | Discord.ThreadChannel)
54 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -050055 deleted: entry(Date.now(), renderDelta(Date.now())),
TheCodedProf4f79da12023-01-31 16:50:37 -050056 mentions: message.mentions.users.size,
57 attachments: entry(attachments, attachments + attachmentJump),
58 repliedTo: entry(
59 replyTo ? replyTo.messageId! : null,
Skyler Greyda16adf2023-03-05 10:22:12 +000060 replyTo
61 ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${
62 replyTo.messageId
63 })`
64 : "None"
TheCodedProf4f79da12023-01-31 16:50:37 -050065 )
66 },
67 hidden: {
68 guild: message.guild!.id
69 }
70 };
71 log(data);
pineafan32767212022-03-14 21:27:39 +000072}