blob: 4780700d0a77ec575a91e6899dd61298e2eb1fd9 [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 audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === channel.id).first();
19 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete))
20 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id)[0];
21 if (!auditLog) return;
22 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000023
pineafan63fc5e22022-08-04 22:04:10 +010024 let emoji;
25 let readableType;
26 let displayName;
27 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000028 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 emoji = "CHANNEL.TEXT.DELETE";
30 readableType = "Text";
31 displayName = "Text Channel";
32 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000033 } case ChannelType.GuildAnnouncement: {
34 emoji = "CHANNEL.TEXT.DELETE";
35 readableType = "Announcement";
36 displayName = "Announcement Channel";
37 break;
38 } case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010039 emoji = "CHANNEL.VOICE.DELETE";
40 readableType = "Voice";
41 displayName = "Voice Channel";
42 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000043 } case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 emoji = "CHANNEL.CATEGORY.DELETE";
45 readableType = "Category";
46 displayName = "Category";
47 break;
PineaFan9b2ac4d2023-01-18 14:41:07 +000048 } case ChannelType.GuildStageVoice: {
49 emoji = "CHANNEL.VOICE.DELETE";
50 readableType = "Stage";
51 displayName = "Stage Channel";
52 break;
53 } case ChannelType.GuildForum: {
54 emoji = "CHANNEL.TEXT.DELETE";
55 readableType = "Forum";
56 displayName = "Forum Channel";
57 break;
58 } default: {
Skyler Grey75ea9172022-08-06 10:22:23 +010059 emoji = "CHANNEL.TEXT.DELETE";
60 readableType = "Channel";
61 displayName = "Channel";
62 }
pineafan63fc5e22022-08-04 22:04:10 +010063 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010064 const list: {
pineafan3a02ea32022-08-11 21:35:04 +010065 channelId: ReturnType<typeof entry>;
66 name: ReturnType<typeof entry>;
67 topic?: ReturnType<typeof entry> | null;
68 type: ReturnType<typeof entry>;
69 category: ReturnType<typeof entry>;
70 nsfw?: ReturnType<typeof entry> | null;
71 created: ReturnType<typeof entry>;
72 deleted: ReturnType<typeof entry>;
73 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +010074 } = {
pineafan63fc5e22022-08-04 22:04:10 +010075 channelId: entry(channel.id, `\`${channel.id}\``),
76 name: entry(channel.id, `${channel.name}`),
77 topic: null,
78 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +010079 category: entry(
80 channel.parent ? channel.parent.id : null,
81 channel.parent ? channel.parent.name : "Uncategorised"
82 ),
pineafan63fc5e22022-08-04 22:04:10 +010083 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +000084 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
pineafan63fc5e22022-08-04 22:04:10 +010085 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
PineaFan9b2ac4d2023-01-18 14:41:07 +000086 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +010087 };
Skyler Grey11236ba2022-08-08 21:13:33 +010088 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
89 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +010090 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +010091 if (
92 channel instanceof BaseGuildTextChannel ||
93 channel instanceof VoiceChannel ||
94 channel instanceof ThreadChannel
95 ) {
96 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +010097 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +010098 nsfw,
99 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100101 } else {
102 delete list.nsfw;
103 }
pineafan32767212022-03-14 21:27:39 +0000104
pineafan63fc5e22022-08-04 22:04:10 +0100105 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100107 type: "channelDelete",
108 displayName: displayName + " Deleted",
109 calculateType: "channelUpdate",
110 color: NucleusColors.red,
111 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000112 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100113 },
114 list: list,
115 hidden: {
116 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 };
119 log(data);
pineafan32767212022-03-14 21:27:39 +0000120}