blob: b79c2da9827c22e032acc9656ba7b158c9978e20 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import {
PineaFan9b2ac4d2023-01-18 14:41:07 +00002 AuditLogEvent,
Skyler Grey11236ba2022-08-08 21:13:33 +01003 BaseGuildTextChannel,
PineaFan9b2ac4d2023-01-18 14:41:07 +00004 ChannelType,
pineafan4e425942022-08-08 22:01:47 +01005 GuildAuditLogsEntry,
6 GuildBasedChannel,
Skyler Grey11236ba2022-08-08 21:13:33 +01007 StageChannel,
8 ThreadChannel,
9 VoiceChannel
10} from "discord.js";
PineaFan752af462022-12-31 21:59:38 +000011import type { NucleusClient } from "../utils/client.js";
pineafan377794f2022-04-18 19:01:01 +010012import getEmojiByName from "../utils/getEmojiByName.js";
13
pineafan63fc5e22022-08-04 22:04:10 +010014export const event = "channelDelete";
pineafan4e425942022-08-08 22:01:47 +010015
PineaFan752af462022-12-31 21:59:38 +000016export async function callback(client: NucleusClient, channel: GuildBasedChannel) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050017 const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000018 if (!(await isLogging(channel.guild.id, "channelUpdate"))) return;
19 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter(
20 (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id
21 )[0];
PineaFan9b2ac4d2023-01-18 14:41:07 +000022 if (!auditLog) return;
23 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000024
pineafan63fc5e22022-08-04 22:04:10 +010025 let emoji;
26 let readableType;
27 let displayName;
28 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000029 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010030 emoji = "CHANNEL.TEXT.DELETE";
31 readableType = "Text";
32 displayName = "Text Channel";
33 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000034 }
35 case ChannelType.GuildAnnouncement: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000036 emoji = "CHANNEL.TEXT.DELETE";
37 readableType = "Announcement";
38 displayName = "Announcement Channel";
39 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000040 }
41 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 emoji = "CHANNEL.VOICE.DELETE";
43 readableType = "Voice";
44 displayName = "Voice Channel";
45 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000046 }
47 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 emoji = "CHANNEL.CATEGORY.DELETE";
49 readableType = "Category";
50 displayName = "Category";
51 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000052 }
53 case ChannelType.GuildStageVoice: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000054 emoji = "CHANNEL.VOICE.DELETE";
55 readableType = "Stage";
56 displayName = "Stage Channel";
57 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000058 }
59 case ChannelType.GuildForum: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000060 emoji = "CHANNEL.TEXT.DELETE";
61 readableType = "Forum";
62 displayName = "Forum Channel";
63 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000064 }
65 default: {
Skyler Grey75ea9172022-08-06 10:22:23 +010066 emoji = "CHANNEL.TEXT.DELETE";
67 readableType = "Channel";
68 displayName = "Channel";
69 }
pineafan63fc5e22022-08-04 22:04:10 +010070 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010071 const list: {
pineafan3a02ea32022-08-11 21:35:04 +010072 channelId: ReturnType<typeof entry>;
73 name: ReturnType<typeof entry>;
74 topic?: ReturnType<typeof entry> | null;
75 type: ReturnType<typeof entry>;
76 category: ReturnType<typeof entry>;
77 nsfw?: ReturnType<typeof entry> | null;
78 created: ReturnType<typeof entry>;
79 deleted: ReturnType<typeof entry>;
80 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +010081 } = {
pineafan63fc5e22022-08-04 22:04:10 +010082 channelId: entry(channel.id, `\`${channel.id}\``),
83 name: entry(channel.id, `${channel.name}`),
84 topic: null,
85 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010086 category: entry(
87 channel.parent ? channel.parent.id : null,
88 channel.parent ? channel.parent.name : "Uncategorised"
89 ),
pineafan63fc5e22022-08-04 22:04:10 +010090 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +000091 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050092 deleted: entry(Date.now(), renderDelta(Date.now())),
PineaFan9b2ac4d2023-01-18 14:41:07 +000093 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +010094 };
Skyler Grey11236ba2022-08-08 21:13:33 +010095 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
96 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010097 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010098 if (
99 channel instanceof BaseGuildTextChannel ||
100 channel instanceof VoiceChannel ||
101 channel instanceof ThreadChannel
102 ) {
103 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100105 nsfw,
106 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100108 } else {
109 delete list.nsfw;
110 }
pineafan32767212022-03-14 21:27:39 +0000111
pineafan63fc5e22022-08-04 22:04:10 +0100112 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100114 type: "channelDelete",
115 displayName: displayName + " Deleted",
116 calculateType: "channelUpdate",
117 color: NucleusColors.red,
118 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000119 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100120 },
121 list: list,
122 hidden: {
123 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100124 }
pineafan63fc5e22022-08-04 22:04:10 +0100125 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000126 await log(data);
pineafan32767212022-03-14 21:27:39 +0000127}