blob: 8188a6702d349cd4902ba7a66243b00ab7cc6978 [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
pineafan435a8782023-06-24 12:45:58 +01002import Discord, { AuditLogEvent, ButtonStyle, GuildAuditLogsEntry, Message, User } from "discord.js";
3import { imageDataEasterEgg } from "../utils/defaults.js";
pineafan0f5cc782022-08-12 21:55:42 +01004
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "messageDelete";
pineafan32767212022-03-14 21:27:39 +00006
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, message: Message) {
TheCodedProf4f79da12023-01-31 16:50:37 -05008 if (message.author.id === client.user!.id) return;
9 if (message.author.bot) return;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050010 if (client.noLog.includes(`${message.guild!.id}/${message.channel.id}/${message.id}`)) return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050011 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000012 if (!(await isLogging(message.guild!.id, "messageDelete"))) return;
13 const auditLog = (await getAuditLog(message.guild!, AuditLogEvent.MemberBanAdd)).filter(
14 (entry: GuildAuditLogsEntry) => (entry.target! as User).id === message.author.id
15 )[0];
TheCodedProf4f79da12023-01-31 16:50:37 -050016 if (auditLog) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050017 if (auditLog.createdTimestamp - 1000 < Date.now()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010018 }
TheCodedProf4f79da12023-01-31 16:50:37 -050019 const replyTo = message.reference;
20 let content = message.cleanContent;
21 content.replace("`", "\\`");
22 if (content.length > 256) content = content.substring(0, 253) + "...";
23 const attachments =
Skyler Greyda16adf2023-03-05 10:22:12 +000024 message.attachments.size +
25 (
TheCodedProf4f79da12023-01-31 16:50:37 -050026 message.content.match(
27 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
28 ) ?? []
29 ).length;
30 let attachmentJump = "";
31 const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[
32 message.channel.id + message.id
33 ];
Skyler Greyda16adf2023-03-05 10:22:12 +000034 if (config) {
35 attachmentJump = ` [[View attachments]](${config})`;
36 }
pineafan435a8782023-06-24 12:45:58 +010037 const imageData = JSON.stringify({ data: message.content, extra: imageDataEasterEgg }, null, 2);
TheCodedProf4f79da12023-01-31 16:50:37 -050038 const data = {
39 meta: {
40 type: "messageDelete",
41 displayName: "Message Deleted",
42 calculateType: "messageDelete",
43 color: NucleusColors.red,
44 emoji: "MESSAGE.DELETE",
pineafan435a8782023-06-24 12:45:58 +010045 timestamp: Date.now(),
46 imageData: imageData,
47 buttons: [{ buttonText: "View text", buttonId: "log:message.delete", buttonStyle: ButtonStyle.Secondary }]
TheCodedProf4f79da12023-01-31 16:50:37 -050048 },
49 separate: {
50 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
51 },
52 list: {
53 messageId: entry(message.id, `\`${message.id}\``),
54 sentBy: entry(message.author.id, renderUser(message.author)),
Skyler Greyda16adf2023-03-05 10:22:12 +000055 sentIn: entry(
56 message.channel.id,
57 renderChannel(message.channel as Discord.GuildChannel | Discord.ThreadChannel)
58 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -050059 deleted: entry(Date.now(), renderDelta(Date.now())),
TheCodedProf4f79da12023-01-31 16:50:37 -050060 mentions: message.mentions.users.size,
61 attachments: entry(attachments, attachments + attachmentJump),
62 repliedTo: entry(
63 replyTo ? replyTo.messageId! : null,
Skyler Greyda16adf2023-03-05 10:22:12 +000064 replyTo
65 ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${
66 replyTo.messageId
67 })`
68 : "None"
TheCodedProf4f79da12023-01-31 16:50:37 -050069 )
70 },
71 hidden: {
72 guild: message.guild!.id
73 }
74 };
Skyler Greyf4f21c42023-03-08 14:36:29 +000075 await log(data);
pineafan32767212022-03-14 21:27:39 +000076}