blob: d92c994164d4ee18797c82732b386bef1b10e506 [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";
PineaFan64486c42022-12-28 09:21:04 +00009import type { HaikuClient } from "../utils/haiku/index.js";
pineafan377794f2022-04-18 19:01:01 +010010import getEmojiByName from "../utils/getEmojiByName.js";
11
pineafan63fc5e22022-08-04 22:04:10 +010012export const event = "channelDelete";
pineafan4e425942022-08-08 22:01:47 +010013
14export async function callback(client: HaikuClient, channel: GuildBasedChannel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010015 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
pineafan32767212022-03-14 21:27:39 +000016
pineafan63fc5e22022-08-04 22:04:10 +010017 const auditLog = await getAuditLog(channel.guild, "CHANNEL_DELETE");
pineafan4e425942022-08-08 22:01:47 +010018 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === channel.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010019 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000020
pineafan63fc5e22022-08-04 22:04:10 +010021 let emoji;
22 let readableType;
23 let displayName;
24 switch (channel.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010025 case "GUILD_TEXT": {
26 emoji = "CHANNEL.TEXT.DELETE";
27 readableType = "Text";
28 displayName = "Text Channel";
29 break;
30 }
31 case "GUILD_VOICE": {
32 emoji = "CHANNEL.VOICE.DELETE";
33 readableType = "Voice";
34 displayName = "Voice Channel";
35 break;
36 }
37 case "GUILD_CATEGORY": {
38 emoji = "CHANNEL.CATEGORY.DELETE";
39 readableType = "Category";
40 displayName = "Category";
41 break;
42 }
43 default: {
44 emoji = "CHANNEL.TEXT.DELETE";
45 readableType = "Channel";
46 displayName = "Channel";
47 }
pineafan63fc5e22022-08-04 22:04:10 +010048 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010049 const list: {
pineafan3a02ea32022-08-11 21:35:04 +010050 channelId: ReturnType<typeof entry>;
51 name: ReturnType<typeof entry>;
52 topic?: ReturnType<typeof entry> | null;
53 type: ReturnType<typeof entry>;
54 category: ReturnType<typeof entry>;
55 nsfw?: ReturnType<typeof entry> | null;
56 created: ReturnType<typeof entry>;
57 deleted: ReturnType<typeof entry>;
58 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +010059 } = {
pineafan63fc5e22022-08-04 22:04:10 +010060 channelId: entry(channel.id, `\`${channel.id}\``),
61 name: entry(channel.id, `${channel.name}`),
62 topic: null,
63 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010064 category: entry(
65 channel.parent ? channel.parent.id : null,
66 channel.parent ? channel.parent.name : "Uncategorised"
67 ),
pineafan63fc5e22022-08-04 22:04:10 +010068 nsfw: null,
Skyler Grey11236ba2022-08-08 21:13:33 +010069 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010070 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
71 deletedBy: entry(audit.executor.id, renderUser(audit.executor))
72 };
Skyler Grey11236ba2022-08-08 21:13:33 +010073 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
74 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010075 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010076 if (
77 channel instanceof BaseGuildTextChannel ||
78 channel instanceof VoiceChannel ||
79 channel instanceof ThreadChannel
80 ) {
81 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +010082 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010083 nsfw,
84 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +010085 );
Skyler Grey11236ba2022-08-08 21:13:33 +010086 } else {
87 delete list.nsfw;
88 }
pineafan32767212022-03-14 21:27:39 +000089
pineafan63fc5e22022-08-04 22:04:10 +010090 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010092 type: "channelDelete",
93 displayName: displayName + " Deleted",
94 calculateType: "channelUpdate",
95 color: NucleusColors.red,
96 emoji: emoji,
97 timestamp: audit.createdTimestamp
98 },
99 list: list,
100 hidden: {
101 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100102 }
pineafan63fc5e22022-08-04 22:04:10 +0100103 };
104 log(data);
pineafan32767212022-03-14 21:27:39 +0000105}