blob: 2eb2ec79fa1f71f96684ee50003a0ab1c7951375 [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";
Skyler Greyecab41b2023-04-23 20:55:59 +000011import _client, { NucleusClient } from "../utils/client.js";
pineafan377794f2022-04-18 19:01:01 +010012import getEmojiByName from "../utils/getEmojiByName.js";
13
TheCodedProf2ffc1542023-04-23 16:42:18 -040014
pineafan63fc5e22022-08-04 22:04:10 +010015export const event = "channelDelete";
pineafan4e425942022-08-08 22:01:47 +010016
TheCodedProf2ffc1542023-04-23 16:42:18 -040017// function getPropFromObject(splitProp: string[], object: Record<string, unknown>) {
18// if (splitProp.length === 0) return null
19// if (splitProp.length === 1) {
20// return object[splitProp[0]!]
21// }
22// const property: string = splitProp[0]!
23// if (! Object.keys(object).includes(property)) return null;
24// splitProp = splitProp.splice(1)
25// return getPropFromObject(splitProp, object[property] as Record<string, unknown>)
26// }
27
28// async function deleteFromGuildConfig(channel: GuildBasedChannel) {
29// const guildConfig = await client.database.guilds.read(channel.guild.id);
30// const lists = [
31// "filters.wordFilter.allowed.channels",
32// "filters.invite.allowed.channels",
33// "filters.pings.allowed.channels",
34// "filters.clean.allowed.channels",
35// "filters.autoPublish.allowed.channels"
36// ]
37// const single = [
38// "welcome.channel",
39// "logging.logs.channel",
40// "logging.staff.channel",
41// "logging.attachments.channel",
42// "tickets.category"
43// ]
44// console.log(guildConfig, lists, single)
45// // for (const list of lists) {
46// // const index = guildConfig[list].indexOf(channel.id);
47// // if (index !== -1) guildConfig[list].splice(index, 1);
48// // }
49// };
50
Skyler Greyecab41b2023-04-23 20:55:59 +000051export async function callback(client: NucleusClient, channel: GuildBasedChannel) {
52 // In future, please avoid using client from the outer scope. If you import client separately this
53 // parameter should shadow it.
54
TheCodedProf2ffc1542023-04-23 16:42:18 -040055 // await deleteFromGuildConfig(channel)
TheCodedProf6ec331b2023-02-20 12:13:06 -050056 const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000057 if (!(await isLogging(channel.guild.id, "channelUpdate"))) return;
58 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter(
59 (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id
60 )[0];
PineaFan9b2ac4d2023-01-18 14:41:07 +000061 if (!auditLog) return;
62 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000063
pineafan63fc5e22022-08-04 22:04:10 +010064 let emoji;
65 let readableType;
66 let displayName;
67 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000068 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 emoji = "CHANNEL.TEXT.DELETE";
70 readableType = "Text";
71 displayName = "Text Channel";
72 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000073 }
74 case ChannelType.GuildAnnouncement: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000075 emoji = "CHANNEL.TEXT.DELETE";
76 readableType = "Announcement";
77 displayName = "Announcement Channel";
78 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000079 }
80 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 emoji = "CHANNEL.VOICE.DELETE";
82 readableType = "Voice";
83 displayName = "Voice Channel";
84 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000085 }
86 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010087 emoji = "CHANNEL.CATEGORY.DELETE";
88 readableType = "Category";
89 displayName = "Category";
90 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000091 }
92 case ChannelType.GuildStageVoice: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000093 emoji = "CHANNEL.VOICE.DELETE";
94 readableType = "Stage";
95 displayName = "Stage Channel";
96 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000097 }
98 case ChannelType.GuildForum: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000099 emoji = "CHANNEL.TEXT.DELETE";
100 readableType = "Forum";
101 displayName = "Forum Channel";
102 break;
Skyler Greyda16adf2023-03-05 10:22:12 +0000103 }
104 default: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 emoji = "CHANNEL.TEXT.DELETE";
106 readableType = "Channel";
107 displayName = "Channel";
108 }
pineafan63fc5e22022-08-04 22:04:10 +0100109 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100110 const list: {
pineafan3a02ea32022-08-11 21:35:04 +0100111 channelId: ReturnType<typeof entry>;
112 name: ReturnType<typeof entry>;
113 topic?: ReturnType<typeof entry> | null;
114 type: ReturnType<typeof entry>;
115 category: ReturnType<typeof entry>;
116 nsfw?: ReturnType<typeof entry> | null;
117 created: ReturnType<typeof entry>;
118 deleted: ReturnType<typeof entry>;
119 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +0100120 } = {
pineafan63fc5e22022-08-04 22:04:10 +0100121 channelId: entry(channel.id, `\`${channel.id}\``),
122 name: entry(channel.id, `${channel.name}`),
123 topic: null,
124 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 category: entry(
126 channel.parent ? channel.parent.id : null,
127 channel.parent ? channel.parent.name : "Uncategorised"
128 ),
pineafan63fc5e22022-08-04 22:04:10 +0100129 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000130 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500131 deleted: entry(Date.now(), renderDelta(Date.now())),
PineaFan9b2ac4d2023-01-18 14:41:07 +0000132 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +0100133 };
Skyler Grey11236ba2022-08-08 21:13:33 +0100134 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
135 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +0100136 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +0100137 if (
138 channel instanceof BaseGuildTextChannel ||
139 channel instanceof VoiceChannel ||
140 channel instanceof ThreadChannel
141 ) {
142 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100144 nsfw,
145 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100147 } else {
148 delete list.nsfw;
149 }
pineafan32767212022-03-14 21:27:39 +0000150
pineafan63fc5e22022-08-04 22:04:10 +0100151 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100153 type: "channelDelete",
154 displayName: displayName + " Deleted",
155 calculateType: "channelUpdate",
156 color: NucleusColors.red,
157 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000158 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100159 },
160 list: list,
161 hidden: {
162 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100163 }
pineafan63fc5e22022-08-04 22:04:10 +0100164 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000165 await log(data);
pineafan32767212022-03-14 21:27:39 +0000166}