blob: f2301e0c0a65488666d4121ae2cf47e93a9f7e7a [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001// @ts-expect-error
2import type { HaikuClient } from "jshaiku";
3import type { GuildAuditLogsEntry, Message } from "discord.js";
4
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "messageDelete";
pineafan32767212022-03-14 21:27:39 +00006
pineafan0f5cc782022-08-12 21:55:42 +01007export async function callback(client: HaikuClient, message: Message) {
pineafane625d782022-05-09 18:04:32 +01008 try {
pineafane23c4ec2022-07-27 21:56:27 +01009 if (message.author.id === client.user.id) return;
pineafan0f5cc782022-08-12 21:55:42 +010010 if (client.noLog.includes(`${message.id}/${message.channel.id}/${message.id}`)) return;
11 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010012 const auditLog = await getAuditLog(message.guild, "MEMBER_BAN_ADD");
pineafan0f5cc782022-08-12 21:55:42 +010013 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === message.author.id).first();
pineafane625d782022-05-09 18:04:32 +010014 if (audit) {
15 if (audit.createdAt - 100 < new Date().getTime()) return;
pineafan32767212022-03-14 21:27:39 +000016 }
pineafan0f5cc782022-08-12 21:55:42 +010017 const replyTo = message.reference;
pineafan63fc5e22022-08-04 22:04:10 +010018 let content = message.cleanContent;
19 content.replace("`", "\\`");
20 if (content.length > 256) content = content.substring(0, 253) + "...";
Skyler Grey75ea9172022-08-06 10:22:23 +010021 const attachments =
22 message.attachments.size +
23 (
24 message.content.match(
25 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
26 ) ?? []
27 ).length;
pineafan63fc5e22022-08-04 22:04:10 +010028 let attachmentJump = "";
pineafan0f5cc782022-08-12 21:55:42 +010029 const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[
Skyler Grey11236ba2022-08-08 21:13:33 +010030 message.channel.id + message.id
31 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010032 if (config) {
33 attachmentJump = ` [[View attachments]](${config})`;
34 }
pineafan63fc5e22022-08-04 22:04:10 +010035 const data = {
pineafane625d782022-05-09 18:04:32 +010036 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010037 type: "messageDelete",
38 displayName: "Message Deleted",
39 calculateType: "messageDelete",
pineafane625d782022-05-09 18:04:32 +010040 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010041 emoji: "MESSAGE.DELETE",
pineafane625d782022-05-09 18:04:32 +010042 timestamp: new Date().getTime()
43 },
44 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +010045 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane625d782022-05-09 18:04:32 +010046 },
47 list: {
pineafanda6e5342022-07-03 10:03:16 +010048 messageId: entry(message.id, `\`${message.id}\``),
pineafane625d782022-05-09 18:04:32 +010049 sentBy: entry(message.author.id, renderUser(message.author)),
Skyler Grey11236ba2022-08-08 21:13:33 +010050 sentIn: entry(message.channel.id, renderChannel(message.channel)),
51 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafane625d782022-05-09 18:04:32 +010052 mentions: message.mentions.users.size,
pineafan02ba0232022-07-24 22:16:15 +010053 attachments: entry(attachments, attachments + attachmentJump),
pineafane625d782022-05-09 18:04:32 +010054 repliedTo: entry(
pineafan0f5cc782022-08-12 21:55:42 +010055 replyTo,
56 replyTo
57 ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010058 : "None"
pineafane625d782022-05-09 18:04:32 +010059 )
60 },
61 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010062 guild: message.guild!.id
pineafane625d782022-05-09 18:04:32 +010063 }
pineafan63fc5e22022-08-04 22:04:10 +010064 };
pineafan4edb7762022-06-26 19:21:04 +010065 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010066 } catch (e) {
67 console.log(e);
68 }
pineafan32767212022-03-14 21:27:39 +000069}