blob: d42a12abc870a215dfdaf6de103f1539928b10e3 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import {
2 BaseGuildTextChannel,
pineafan4e425942022-08-08 22:01:47 +01003 GuildAuditLogsEntry,
4 GuildBasedChannel,
Skyler Grey11236ba2022-08-08 21:13:33 +01005 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";
pineafan4e425942022-08-08 22:01:47 +010014
15export async function callback(client: HaikuClient, channel: GuildBasedChannel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010016 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan32767212022-03-14 21:27:39 +000017
pineafan63fc5e22022-08-04 22:04:10 +010018 const auditLog = await getAuditLog(channel.guild, "CHANNEL_DELETE");
pineafan4e425942022-08-08 22:01:47 +010019 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === channel.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010020 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000021
pineafan63fc5e22022-08-04 22:04:10 +010022 let emoji;
23 let readableType;
24 let displayName;
25 switch (channel.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 case "GUILD_TEXT": {
27 emoji = "CHANNEL.TEXT.DELETE";
28 readableType = "Text";
29 displayName = "Text Channel";
30 break;
31 }
32 case "GUILD_VOICE": {
33 emoji = "CHANNEL.VOICE.DELETE";
34 readableType = "Voice";
35 displayName = "Voice Channel";
36 break;
37 }
38 case "GUILD_CATEGORY": {
39 emoji = "CHANNEL.CATEGORY.DELETE";
40 readableType = "Category";
41 displayName = "Category";
42 break;
43 }
44 default: {
45 emoji = "CHANNEL.TEXT.DELETE";
46 readableType = "Channel";
47 displayName = "Channel";
48 }
pineafan63fc5e22022-08-04 22:04:10 +010049 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010050 const list: {
pineafan4e425942022-08-08 22:01:47 +010051 channelId: { value: string; displayValue: string };
52 name: { value: string; displayValue: string };
53 topic?: { value: string; displayValue: string } | null;
54 type: { value: string; displayValue: string };
55 category: { value: string; displayValue: string };
56 nsfw?: { value: string; displayValue: string } | null;
57 created: { value: string; displayValue: string };
58 deleted: { value: string; displayValue: string };
59 deletedBy: { value: string; displayValue: string };
Skyler Greyc634e2b2022-08-06 17:50:48 +010060 } = {
pineafan63fc5e22022-08-04 22:04:10 +010061 channelId: entry(channel.id, `\`${channel.id}\``),
62 name: entry(channel.id, `${channel.name}`),
63 topic: null,
64 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010065 category: entry(
66 channel.parent ? channel.parent.id : null,
67 channel.parent ? channel.parent.name : "Uncategorised"
68 ),
pineafan63fc5e22022-08-04 22:04:10 +010069 nsfw: null,
Skyler Grey11236ba2022-08-08 21:13:33 +010070 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010071 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
72 deletedBy: entry(audit.executor.id, renderUser(audit.executor))
73 };
Skyler Grey11236ba2022-08-08 21:13:33 +010074 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
75 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010076 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010077 if (
78 channel instanceof BaseGuildTextChannel ||
79 channel instanceof VoiceChannel ||
80 channel instanceof ThreadChannel
81 ) {
82 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +010083 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010084 nsfw,
85 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +010086 );
Skyler Grey11236ba2022-08-08 21:13:33 +010087 } else {
88 delete list.nsfw;
89 }
pineafan32767212022-03-14 21:27:39 +000090
pineafan63fc5e22022-08-04 22:04:10 +010091 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010092 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010093 type: "channelDelete",
94 displayName: displayName + " Deleted",
95 calculateType: "channelUpdate",
96 color: NucleusColors.red,
97 emoji: emoji,
98 timestamp: audit.createdTimestamp
99 },
100 list: list,
101 hidden: {
102 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100103 }
pineafan63fc5e22022-08-04 22:04:10 +0100104 };
105 log(data);
pineafan32767212022-03-14 21:27:39 +0000106}