blob: a93e6c73f40c0953390895d371f2f28259aee3fe [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) {
Skyler Grey11236ba2022-08-08 21:13:33 +010017 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser } = client.logger;
PineaFan9b2ac4d2023-01-18 14:41:07 +000018 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete))
19 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id)[0];
20 if (!auditLog) return;
21 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000022
pineafan63fc5e22022-08-04 22:04:10 +010023 let emoji;
24 let readableType;
25 let displayName;
26 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000027 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010028 emoji = "CHANNEL.TEXT.DELETE";
29 readableType = "Text";
30 displayName = "Text Channel";
31 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000032 } case ChannelType.GuildAnnouncement: {
33 emoji = "CHANNEL.TEXT.DELETE";
34 readableType = "Announcement";
35 displayName = "Announcement Channel";
36 break;
37 } case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010038 emoji = "CHANNEL.VOICE.DELETE";
39 readableType = "Voice";
40 displayName = "Voice Channel";
41 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000042 } case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010043 emoji = "CHANNEL.CATEGORY.DELETE";
44 readableType = "Category";
45 displayName = "Category";
46 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000047 } case ChannelType.GuildStageVoice: {
48 emoji = "CHANNEL.VOICE.DELETE";
49 readableType = "Stage";
50 displayName = "Stage Channel";
51 break;
52 } case ChannelType.GuildForum: {
53 emoji = "CHANNEL.TEXT.DELETE";
54 readableType = "Forum";
55 displayName = "Forum Channel";
56 break;
57 } default: {
Skyler Grey75ea9172022-08-06 10:22:23 +010058 emoji = "CHANNEL.TEXT.DELETE";
59 readableType = "Channel";
60 displayName = "Channel";
61 }
pineafan63fc5e22022-08-04 22:04:10 +010062 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010063 const list: {
pineafan3a02ea32022-08-11 21:35:04 +010064 channelId: ReturnType<typeof entry>;
65 name: ReturnType<typeof entry>;
66 topic?: ReturnType<typeof entry> | null;
67 type: ReturnType<typeof entry>;
68 category: ReturnType<typeof entry>;
69 nsfw?: ReturnType<typeof entry> | null;
70 created: ReturnType<typeof entry>;
71 deleted: ReturnType<typeof entry>;
72 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +010073 } = {
pineafan63fc5e22022-08-04 22:04:10 +010074 channelId: entry(channel.id, `\`${channel.id}\``),
75 name: entry(channel.id, `${channel.name}`),
76 topic: null,
77 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010078 category: entry(
79 channel.parent ? channel.parent.id : null,
80 channel.parent ? channel.parent.name : "Uncategorised"
81 ),
pineafan63fc5e22022-08-04 22:04:10 +010082 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +000083 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
pineafan63fc5e22022-08-04 22:04:10 +010084 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
PineaFan9b2ac4d2023-01-18 14:41:07 +000085 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +010086 };
Skyler Grey11236ba2022-08-08 21:13:33 +010087 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
88 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010089 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010090 if (
91 channel instanceof BaseGuildTextChannel ||
92 channel instanceof VoiceChannel ||
93 channel instanceof ThreadChannel
94 ) {
95 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +010096 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010097 nsfw,
98 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +010099 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100100 } else {
101 delete list.nsfw;
102 }
pineafan32767212022-03-14 21:27:39 +0000103
pineafan63fc5e22022-08-04 22:04:10 +0100104 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100106 type: "channelDelete",
107 displayName: displayName + " Deleted",
108 calculateType: "channelUpdate",
109 color: NucleusColors.red,
110 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000111 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100112 },
113 list: list,
114 hidden: {
115 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100116 }
pineafan63fc5e22022-08-04 22:04:10 +0100117 };
118 log(data);
pineafan32767212022-03-14 21:27:39 +0000119}