blob: e396c038e8d9d97a20d86e8652aaa494ee8ae0cb [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
pineafan63fc5e22022-08-04 22:04:10 +010014export const event = "channelDelete";
pineafan4e425942022-08-08 22:01:47 +010015
TheCodedProf2ffc1542023-04-23 16:42:18 -040016// function getPropFromObject(splitProp: string[], object: Record<string, unknown>) {
17// if (splitProp.length === 0) return null
18// if (splitProp.length === 1) {
19// return object[splitProp[0]!]
20// }
21// const property: string = splitProp[0]!
22// if (! Object.keys(object).includes(property)) return null;
23// splitProp = splitProp.splice(1)
24// return getPropFromObject(splitProp, object[property] as Record<string, unknown>)
25// }
26
27// async function deleteFromGuildConfig(channel: GuildBasedChannel) {
28// const guildConfig = await client.database.guilds.read(channel.guild.id);
29// const lists = [
30// "filters.wordFilter.allowed.channels",
31// "filters.invite.allowed.channels",
32// "filters.pings.allowed.channels",
33// "filters.clean.allowed.channels",
34// "filters.autoPublish.allowed.channels"
35// ]
36// const single = [
37// "welcome.channel",
38// "logging.logs.channel",
39// "logging.staff.channel",
40// "logging.attachments.channel",
41// "tickets.category"
42// ]
43// console.log(guildConfig, lists, single)
44// // for (const list of lists) {
45// // const index = guildConfig[list].indexOf(channel.id);
46// // if (index !== -1) guildConfig[list].splice(index, 1);
47// // }
48// };
49
Skyler Greyecab41b2023-04-23 20:55:59 +000050export async function callback(client: NucleusClient, channel: GuildBasedChannel) {
51 // In future, please avoid using client from the outer scope. If you import client separately this
52 // parameter should shadow it.
53
TheCodedProf2ffc1542023-04-23 16:42:18 -040054 // await deleteFromGuildConfig(channel)
TheCodedProf6ec331b2023-02-20 12:13:06 -050055 const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000056 if (!(await isLogging(channel.guild.id, "channelUpdate"))) return;
57 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter(
58 (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id
59 )[0];
PineaFan9b2ac4d2023-01-18 14:41:07 +000060 if (!auditLog) return;
61 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000062
pineafan63fc5e22022-08-04 22:04:10 +010063 let emoji;
64 let readableType;
65 let displayName;
66 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000067 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010068 emoji = "CHANNEL.TEXT.DELETE";
69 readableType = "Text";
70 displayName = "Text Channel";
71 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000072 }
73 case ChannelType.GuildAnnouncement: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000074 emoji = "CHANNEL.TEXT.DELETE";
75 readableType = "Announcement";
76 displayName = "Announcement Channel";
77 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000078 }
79 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 emoji = "CHANNEL.VOICE.DELETE";
81 readableType = "Voice";
82 displayName = "Voice Channel";
83 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000084 }
85 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010086 emoji = "CHANNEL.CATEGORY.DELETE";
87 readableType = "Category";
88 displayName = "Category";
89 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000090 }
91 case ChannelType.GuildStageVoice: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000092 emoji = "CHANNEL.VOICE.DELETE";
93 readableType = "Stage";
94 displayName = "Stage Channel";
95 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000096 }
97 case ChannelType.GuildForum: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000098 emoji = "CHANNEL.TEXT.DELETE";
99 readableType = "Forum";
100 displayName = "Forum Channel";
101 break;
Skyler Greyda16adf2023-03-05 10:22:12 +0000102 }
103 default: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 emoji = "CHANNEL.TEXT.DELETE";
105 readableType = "Channel";
106 displayName = "Channel";
107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100109 const list: {
pineafan3a02ea32022-08-11 21:35:04 +0100110 channelId: ReturnType<typeof entry>;
111 name: ReturnType<typeof entry>;
112 topic?: ReturnType<typeof entry> | null;
113 type: ReturnType<typeof entry>;
114 category: ReturnType<typeof entry>;
115 nsfw?: ReturnType<typeof entry> | null;
116 created: ReturnType<typeof entry>;
117 deleted: ReturnType<typeof entry>;
118 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +0100119 } = {
pineafan63fc5e22022-08-04 22:04:10 +0100120 channelId: entry(channel.id, `\`${channel.id}\``),
121 name: entry(channel.id, `${channel.name}`),
122 topic: null,
123 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 category: entry(
125 channel.parent ? channel.parent.id : null,
126 channel.parent ? channel.parent.name : "Uncategorised"
127 ),
pineafan63fc5e22022-08-04 22:04:10 +0100128 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000129 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500130 deleted: entry(Date.now(), renderDelta(Date.now())),
PineaFan9b2ac4d2023-01-18 14:41:07 +0000131 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +0100132 };
Skyler Grey11236ba2022-08-08 21:13:33 +0100133 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
134 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +0100135 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +0100136 if (
137 channel instanceof BaseGuildTextChannel ||
138 channel instanceof VoiceChannel ||
139 channel instanceof ThreadChannel
140 ) {
141 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100143 nsfw,
144 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100146 } else {
147 delete list.nsfw;
148 }
pineafan32767212022-03-14 21:27:39 +0000149
pineafan63fc5e22022-08-04 22:04:10 +0100150 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100152 type: "channelDelete",
153 displayName: displayName + " Deleted",
154 calculateType: "channelUpdate",
155 color: NucleusColors.red,
156 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000157 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100158 },
159 list: list,
160 hidden: {
161 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100162 }
pineafan63fc5e22022-08-04 22:04:10 +0100163 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000164 await log(data);
pineafan32767212022-03-14 21:27:39 +0000165}