blob: 0527a60c9b2603e85049d33b996d734ba4ae0f6c [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");
Skyler Greyf21323a2022-08-13 23:58:22 +010013 const audit = auditLog.entries
14 .filter((entry: GuildAuditLogsEntry) => entry.target!.id === message.author.id)
15 .first();
pineafane625d782022-05-09 18:04:32 +010016 if (audit) {
17 if (audit.createdAt - 100 < new Date().getTime()) return;
pineafan32767212022-03-14 21:27:39 +000018 }
pineafan0f5cc782022-08-12 21:55:42 +010019 const replyTo = message.reference;
pineafan63fc5e22022-08-04 22:04:10 +010020 let content = message.cleanContent;
21 content.replace("`", "\\`");
22 if (content.length > 256) content = content.substring(0, 253) + "...";
Skyler Grey75ea9172022-08-06 10:22:23 +010023 const attachments =
24 message.attachments.size +
25 (
26 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;
pineafan63fc5e22022-08-04 22:04:10 +010030 let attachmentJump = "";
pineafan0f5cc782022-08-12 21:55:42 +010031 const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[
Skyler Grey11236ba2022-08-08 21:13:33 +010032 message.channel.id + message.id
33 ];
Skyler Grey75ea9172022-08-06 10:22:23 +010034 if (config) {
35 attachmentJump = ` [[View attachments]](${config})`;
36 }
pineafan63fc5e22022-08-04 22:04:10 +010037 const data = {
pineafane625d782022-05-09 18:04:32 +010038 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010039 type: "messageDelete",
40 displayName: "Message Deleted",
41 calculateType: "messageDelete",
pineafane625d782022-05-09 18:04:32 +010042 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010043 emoji: "MESSAGE.DELETE",
pineafane625d782022-05-09 18:04:32 +010044 timestamp: new Date().getTime()
45 },
46 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +010047 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane625d782022-05-09 18:04:32 +010048 },
49 list: {
pineafanda6e5342022-07-03 10:03:16 +010050 messageId: entry(message.id, `\`${message.id}\``),
pineafane625d782022-05-09 18:04:32 +010051 sentBy: entry(message.author.id, renderUser(message.author)),
Skyler Grey11236ba2022-08-08 21:13:33 +010052 sentIn: entry(message.channel.id, renderChannel(message.channel)),
53 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafane625d782022-05-09 18:04:32 +010054 mentions: message.mentions.users.size,
pineafan02ba0232022-07-24 22:16:15 +010055 attachments: entry(attachments, attachments + attachmentJump),
pineafane625d782022-05-09 18:04:32 +010056 repliedTo: entry(
pineafan0f5cc782022-08-12 21:55:42 +010057 replyTo,
58 replyTo
Skyler Greyf21323a2022-08-13 23:58:22 +010059 ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${
60 message.channel.id
61 }/${replyTo.messageId})`
Skyler Grey75ea9172022-08-06 10:22:23 +010062 : "None"
pineafane625d782022-05-09 18:04:32 +010063 )
64 },
65 hidden: {
pineafan0f5cc782022-08-12 21:55:42 +010066 guild: message.guild!.id
pineafane625d782022-05-09 18:04:32 +010067 }
pineafan63fc5e22022-08-04 22:04:10 +010068 };
pineafan4edb7762022-06-26 19:21:04 +010069 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010070 } catch (e) {
71 console.log(e);
72 }
pineafan32767212022-03-14 21:27:39 +000073}