blob: f39e7ed9113b0efa1894d7a0cb928a6bb417f08c [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import {
2 BaseGuildTextChannel,
3 BaseGuildVoiceChannel,
4 GuildChannel,
5 StageChannel,
6 ThreadChannel,
7 VoiceChannel
8} from "discord.js";
9// @ts-expect-error
10import type { HaikuClient } from "jshaiku";
pineafan377794f2022-04-18 19:01:01 +010011import getEmojiByName from "../utils/getEmojiByName.js";
12
pineafan63fc5e22022-08-04 22:04:10 +010013export const event = "channelDelete";
Skyler Grey11236ba2022-08-08 21:13:33 +010014 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan32767212022-03-14 21:27:39 +000015
pineafan63fc5e22022-08-04 22:04:10 +010016 const auditLog = await getAuditLog(channel.guild, "CHANNEL_DELETE");
Skyler Grey11236ba2022-08-08 21:13:33 +010017 const audit = auditLog.entries.filter((entry) => entry.target.id === channel.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010018 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000019
pineafan63fc5e22022-08-04 22:04:10 +010020 let emoji;
21 let readableType;
22 let displayName;
23 switch (channel.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010024 case "GUILD_TEXT": {
25 emoji = "CHANNEL.TEXT.DELETE";
26 readableType = "Text";
27 displayName = "Text Channel";
28 break;
29 }
30 case "GUILD_VOICE": {
31 emoji = "CHANNEL.VOICE.DELETE";
32 readableType = "Voice";
33 displayName = "Voice Channel";
34 break;
35 }
36 case "GUILD_CATEGORY": {
37 emoji = "CHANNEL.CATEGORY.DELETE";
38 readableType = "Category";
39 displayName = "Category";
40 break;
41 }
42 default: {
43 emoji = "CHANNEL.TEXT.DELETE";
44 readableType = "Channel";
45 displayName = "Channel";
46 }
pineafan63fc5e22022-08-04 22:04:10 +010047 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010048 const list: {
49 channelId: string;
50 name: string;
51 topic?: string | null;
52 type: any;
53 category: any;
54 nsfw?: boolean | null;
55 created: any;
56 deleted: any;
57 deletedBy: any;
58 } = {
pineafan63fc5e22022-08-04 22:04:10 +010059 channelId: entry(channel.id, `\`${channel.id}\``),
60 name: entry(channel.id, `${channel.name}`),
61 topic: null,
62 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010063 category: entry(
64 channel.parent ? channel.parent.id : null,
65 channel.parent ? channel.parent.name : "Uncategorised"
66 ),
pineafan63fc5e22022-08-04 22:04:10 +010067 nsfw: null,
Skyler Grey11236ba2022-08-08 21:13:33 +010068 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010069 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
70 deletedBy: entry(audit.executor.id, renderUser(audit.executor))
71 };
Skyler Grey11236ba2022-08-08 21:13:33 +010072 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
73 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010074 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010075 if (
76 channel instanceof BaseGuildTextChannel ||
77 channel instanceof VoiceChannel ||
78 channel instanceof ThreadChannel
79 ) {
80 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +010081 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010082 nsfw,
83 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +010084 );
Skyler Grey11236ba2022-08-08 21:13:33 +010085 } else {
86 delete list.nsfw;
87 }
pineafan32767212022-03-14 21:27:39 +000088
pineafan63fc5e22022-08-04 22:04:10 +010089 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010091 type: "channelDelete",
92 displayName: displayName + " Deleted",
93 calculateType: "channelUpdate",
94 color: NucleusColors.red,
95 emoji: emoji,
96 timestamp: audit.createdTimestamp
97 },
98 list: list,
99 hidden: {
100 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100101 }
pineafan63fc5e22022-08-04 22:04:10 +0100102 };
103 log(data);
pineafan32767212022-03-14 21:27:39 +0000104}