blob: f8433fce3bc03a7ddc9ecc7e61a0dba738c09145 [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) {
pineafane625d782022-05-09 18:04:32 +01007 try {
PineaFan538d3752023-01-12 21:48:23 +00008 if (message.author.id === client.user!.id) return;
pineafan0f5cc782022-08-12 21:55:42 +01009 if (client.noLog.includes(`${message.id}/${message.channel.id}/${message.id}`)) return;
10 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
PineaFan538d3752023-01-12 21:48:23 +000011 const auditLog = (await getAuditLog(message.guild!, AuditLogEvent.MemberBanAdd))
12 .filter((entry: GuildAuditLogsEntry) => (entry.target! as User).id === message.author.id)[0];
13 if (auditLog) {
14 if (auditLog.createdTimestamp - 1000 < new Date().getTime()) return;
pineafan32767212022-03-14 21:27:39 +000015 }
pineafan0f5cc782022-08-12 21:55:42 +010016 const replyTo = message.reference;
pineafan63fc5e22022-08-04 22:04:10 +010017 let content = message.cleanContent;
18 content.replace("`", "\\`");
19 if (content.length > 256) content = content.substring(0, 253) + "...";
Skyler Grey75ea9172022-08-06 10:22:23 +010020 const attachments =
PineaFan538d3752023-01-12 21:48:23 +000021 message.attachments.size + (
Skyler Grey75ea9172022-08-06 10:22:23 +010022 message.content.match(
23 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
24 ) ?? []
25 ).length;
pineafan63fc5e22022-08-04 22:04:10 +010026 let attachmentJump = "";
pineafan0f5cc782022-08-12 21:55:42 +010027 const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[
Skyler Grey11236ba2022-08-08 21:13:33 +010028 message.channel.id + message.id
29 ];
PineaFan538d3752023-01-12 21:48:23 +000030 if (config) { attachmentJump = ` [[View attachments]](${config})`; }
pineafan63fc5e22022-08-04 22:04:10 +010031 const data = {
pineafane625d782022-05-09 18:04:32 +010032 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010033 type: "messageDelete",
34 displayName: "Message Deleted",
35 calculateType: "messageDelete",
pineafane625d782022-05-09 18:04:32 +010036 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010037 emoji: "MESSAGE.DELETE",
pineafane625d782022-05-09 18:04:32 +010038 timestamp: new Date().getTime()
39 },
40 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +010041 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane625d782022-05-09 18:04:32 +010042 },
43 list: {
pineafanda6e5342022-07-03 10:03:16 +010044 messageId: entry(message.id, `\`${message.id}\``),
pineafane625d782022-05-09 18:04:32 +010045 sentBy: entry(message.author.id, renderUser(message.author)),
PineaFan538d3752023-01-12 21:48:23 +000046 sentIn: entry(message.channel.id, renderChannel(message.channel as Discord.GuildChannel | Discord.ThreadChannel)),
Skyler Grey11236ba2022-08-08 21:13:33 +010047 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafane625d782022-05-09 18:04:32 +010048 mentions: message.mentions.users.size,
pineafan02ba0232022-07-24 22:16:15 +010049 attachments: entry(attachments, attachments + attachmentJump),
pineafane625d782022-05-09 18:04:32 +010050 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000051 replyTo ? replyTo.messageId! : null,
52 replyTo ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${replyTo.messageId})`
53 : "None"
pineafane625d782022-05-09 18:04:32 +010054 )
55 },
56 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010057 guild: message.guild!.id
pineafane625d782022-05-09 18:04:32 +010058 }
pineafan63fc5e22022-08-04 22:04:10 +010059 };
pineafan4edb7762022-06-26 19:21:04 +010060 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010061 } catch (e) {
62 console.log(e);
63 }
pineafan32767212022-03-14 21:27:39 +000064}