blob: 650e47ccbaa00f6ef11cde9ba17f0c99bc503a46 [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";
TheCodedProf2ffc1542023-04-23 16:42:18 -040011import 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
51export async function callback(_client: NucleusClient, channel: GuildBasedChannel) {
52 // await deleteFromGuildConfig(channel)
TheCodedProf6ec331b2023-02-20 12:13:06 -050053 const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000054 if (!(await isLogging(channel.guild.id, "channelUpdate"))) return;
55 const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter(
56 (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id
57 )[0];
PineaFan9b2ac4d2023-01-18 14:41:07 +000058 if (!auditLog) return;
59 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000060
pineafan63fc5e22022-08-04 22:04:10 +010061 let emoji;
62 let readableType;
63 let displayName;
64 switch (channel.type) {
PineaFan9b2ac4d2023-01-18 14:41:07 +000065 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010066 emoji = "CHANNEL.TEXT.DELETE";
67 readableType = "Text";
68 displayName = "Text Channel";
69 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000070 }
71 case ChannelType.GuildAnnouncement: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000072 emoji = "CHANNEL.TEXT.DELETE";
73 readableType = "Announcement";
74 displayName = "Announcement Channel";
75 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000076 }
77 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 emoji = "CHANNEL.VOICE.DELETE";
79 readableType = "Voice";
80 displayName = "Voice Channel";
81 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000082 }
83 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +010084 emoji = "CHANNEL.CATEGORY.DELETE";
85 readableType = "Category";
86 displayName = "Category";
87 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000088 }
89 case ChannelType.GuildStageVoice: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000090 emoji = "CHANNEL.VOICE.DELETE";
91 readableType = "Stage";
92 displayName = "Stage Channel";
93 break;
Skyler Greyda16adf2023-03-05 10:22:12 +000094 }
95 case ChannelType.GuildForum: {
PineaFan9b2ac4d2023-01-18 14:41:07 +000096 emoji = "CHANNEL.TEXT.DELETE";
97 readableType = "Forum";
98 displayName = "Forum Channel";
99 break;
Skyler Greyda16adf2023-03-05 10:22:12 +0000100 }
101 default: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 emoji = "CHANNEL.TEXT.DELETE";
103 readableType = "Channel";
104 displayName = "Channel";
105 }
pineafan63fc5e22022-08-04 22:04:10 +0100106 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100107 const list: {
pineafan3a02ea32022-08-11 21:35:04 +0100108 channelId: ReturnType<typeof entry>;
109 name: ReturnType<typeof entry>;
110 topic?: ReturnType<typeof entry> | null;
111 type: ReturnType<typeof entry>;
112 category: ReturnType<typeof entry>;
113 nsfw?: ReturnType<typeof entry> | null;
114 created: ReturnType<typeof entry>;
115 deleted: ReturnType<typeof entry>;
116 deletedBy: ReturnType<typeof entry>;
Skyler Greyc634e2b2022-08-06 17:50:48 +0100117 } = {
pineafan63fc5e22022-08-04 22:04:10 +0100118 channelId: entry(channel.id, `\`${channel.id}\``),
119 name: entry(channel.id, `${channel.name}`),
120 topic: null,
121 type: entry(channel.type, readableType),
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 category: entry(
123 channel.parent ? channel.parent.id : null,
124 channel.parent ? channel.parent.name : "Uncategorised"
125 ),
pineafan63fc5e22022-08-04 22:04:10 +0100126 nsfw: null,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000127 created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500128 deleted: entry(Date.now(), renderDelta(Date.now())),
PineaFan9b2ac4d2023-01-18 14:41:07 +0000129 deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
pineafan63fc5e22022-08-04 22:04:10 +0100130 };
Skyler Grey11236ba2022-08-08 21:13:33 +0100131 if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null)
132 list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``);
pineafan63fc5e22022-08-04 22:04:10 +0100133 else delete list.topic;
Skyler Grey11236ba2022-08-08 21:13:33 +0100134 if (
135 channel instanceof BaseGuildTextChannel ||
136 channel instanceof VoiceChannel ||
137 channel instanceof ThreadChannel
138 ) {
139 const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw;
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 list.nsfw = entry(
Skyler Grey11236ba2022-08-08 21:13:33 +0100141 nsfw,
142 nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 );
Skyler Grey11236ba2022-08-08 21:13:33 +0100144 } else {
145 delete list.nsfw;
146 }
pineafan32767212022-03-14 21:27:39 +0000147
pineafan63fc5e22022-08-04 22:04:10 +0100148 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100150 type: "channelDelete",
151 displayName: displayName + " Deleted",
152 calculateType: "channelUpdate",
153 color: NucleusColors.red,
154 emoji: emoji,
PineaFan9b2ac4d2023-01-18 14:41:07 +0000155 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100156 },
157 list: list,
158 hidden: {
159 guild: channel.guild.id
pineafane625d782022-05-09 18:04:32 +0100160 }
pineafan63fc5e22022-08-04 22:04:10 +0100161 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000162 await log(data);
pineafan32767212022-03-14 21:27:39 +0000163}