blob: 052acc1b0f31a3c9139952c1d67879beb1d22844 [file] [log] [blame]
TheCodedProf309d6182023-01-18 18:10:29 -05001import { GuildChannel, AuditLogEvent, ChannelType, TextChannel, VoiceChannel, StageChannel } from 'discord.js';
2import type { GuildAuditLogsEntry } from 'discord.js';
3//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import humanizeDuration from "humanize-duration";
PineaFane6ba7882023-01-18 20:41:16 +00005import type { NucleusClient } from "../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import getEmojiByName from "../utils/getEmojiByName.js";
TheCodedProfb493b8a2023-01-18 21:11:00 -05007import c from "../utils/client.js";
PineaFan19dc9b82023-01-19 12:25:54 +00008import { capitalize } from "../utils/generateKeyValueList.js";
9
TheCodedProfb493b8a2023-01-18 21:11:00 -050010let entry = c.logger.entry;
pineafan32767212022-03-14 21:27:39 +000011
TheCodedProf309d6182023-01-18 18:10:29 -050012const channelTypeEmoji: Record<number, string> = {
13 0: "Text", // Text channel
14 2: "Voice", // Voice channel
15 5: "Announcement", // Announcement channel
16 13: "Stage", // Stage channel
17 15: "Forum", // Forum channel
18 99: "Rules" // Rules channel
19};
20
21interface channelChanges {
TheCodedProfb493b8a2023-01-18 21:11:00 -050022 channelId: ReturnType<typeof entry>;
23 channel: ReturnType<typeof entry>;
24 edited: ReturnType<typeof entry>;
25 editedBy: ReturnType<typeof entry>;
26 type?: ReturnType<typeof entry>;
27 name?: ReturnType<typeof entry>;
28 position?: ReturnType<typeof entry>;
29 description?: ReturnType<typeof entry>;
30 nsfw?: ReturnType<typeof entry>;
31 slowmode?: ReturnType<typeof entry>;
32 topic?: ReturnType<typeof entry>;
33 bitrate?: ReturnType<typeof entry>;
34 userLimit?: ReturnType<typeof entry>;
TheCodedProfb493b8a2023-01-18 21:11:00 -050035 parent?: ReturnType<typeof entry>;
36 permissionOverwrites?: ReturnType<typeof entry>;
37 region?: ReturnType<typeof entry>;
38 maxUsers?: ReturnType<typeof entry>;
TheCodedProf6ec331b2023-02-20 12:13:06 -050039 autoArchiveDuration?: ReturnType<typeof entry>;
TheCodedProf309d6182023-01-18 18:10:29 -050040}
41
42
43
pineafan63fc5e22022-08-04 22:04:10 +010044export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +000045
PineaFane6ba7882023-01-18 20:41:16 +000046export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050047 const { getAuditLog, log, isLogging, NucleusColors, renderDelta, renderUser, renderChannel } = client.logger;
48 if (!await isLogging(newChannel.guild.id, "channelUpdate")) return;
PineaFane6ba7882023-01-18 20:41:16 +000049 const config = await client.memory.readGuildInfo(newChannel.guild.id);
TheCodedProfb493b8a2023-01-18 21:11:00 -050050 entry = client.logger.entry;
PineaFane6ba7882023-01-18 20:41:16 +000051 if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000052
PineaFanc4d6c3f2023-01-19 12:17:25 +000053 const auditLog: null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate))
54 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id)[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> | null;
55 if (!auditLog) return;
TheCodedProf309d6182023-01-18 18:10:29 -050056 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000057
Skyler Grey75ea9172022-08-06 10:22:23 +010058 let emoji: string;
59 let readableType: string;
60 let displayName: string;
TheCodedProf309d6182023-01-18 18:10:29 -050061 const changes: channelChanges = {
PineaFane6ba7882023-01-18 20:41:16 +000062 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
63 channel: entry(newChannel.id, renderChannel(newChannel)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050064 edited: entry(Date.now(), renderDelta(Date.now())),
TheCodedProf309d6182023-01-18 18:10:29 -050065 editedBy: entry(auditLog.executor!.id, renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)),
pineafan63fc5e22022-08-04 22:04:10 +010066 };
PineaFane6ba7882023-01-18 20:41:16 +000067 if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
68 if (oldChannel.position !== newChannel.position)
TheCodedProf60a1f492023-01-18 16:59:20 -050069 changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`);
pineafan63fc5e22022-08-04 22:04:10 +010070
PineaFane6ba7882023-01-18 20:41:16 +000071 switch (newChannel.type) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050072 case ChannelType.PrivateThread:
73 case ChannelType.PublicThread: {
74 return;
75 }
TheCodedProf309d6182023-01-18 18:10:29 -050076 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010077 emoji = "CHANNEL.TEXT.EDIT";
78 readableType = "Text";
79 displayName = "Text Channel";
TheCodedProf6ec331b2023-02-20 12:13:06 -050080 let oldTopic = (oldChannel as TextChannel).topic ?? "*None*",
81 newTopic = (oldChannel as TextChannel).topic ?? "*None*";
Skyler Grey75ea9172022-08-06 10:22:23 +010082 if (oldTopic) {
83 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010084 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010085 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
86 } else {
87 oldTopic = "None";
88 }
89 if (newTopic) {
90 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010091 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010092 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
93 } else {
94 newTopic = "None";
95 }
96 const nsfw = ["", ""];
TheCodedProf309d6182023-01-18 18:10:29 -050097 nsfw[0] = (oldChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
98 nsfw[1] = (newChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
TheCodedProf6ec331b2023-02-20 12:13:06 -050099 if (oldTopic !== newTopic)
TheCodedProf309d6182023-01-18 18:10:29 -0500100 changes.description = entry([(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
101 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) changes.nsfw = entry([(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"], `${nsfw[0]} -> ${nsfw[1]}`);
TheCodedProf6ec331b2023-02-20 12:13:06 -0500102 if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser)
103 changes.slowmode = entry(
PineaFan638eb132023-01-19 10:41:22 +0000104 [((oldChannel as TextChannel).rateLimitPerUser).toString(), ((newChannel as TextChannel).rateLimitPerUser).toString()],
TheCodedProf309d6182023-01-18 18:10:29 -0500105 `${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration((newChannel as TextChannel).rateLimitPerUser * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 );
TheCodedProf6ec331b2023-02-20 12:13:06 -0500107 if((oldChannel as TextChannel).defaultAutoArchiveDuration !== (newChannel as TextChannel).defaultAutoArchiveDuration) {
108 changes.autoArchiveDuration = entry(
109 [((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(), ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()],
110 `${humanizeDuration(((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)} -> ${humanizeDuration(((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)}`
111 );
112 }
pineafan63fc5e22022-08-04 22:04:10 +0100113
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 break;
115 }
TheCodedProf309d6182023-01-18 18:10:29 -0500116 case ChannelType.GuildAnnouncement: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 emoji = "CHANNEL.TEXT.EDIT";
TheCodedProf309d6182023-01-18 18:10:29 -0500118 readableType = "Announcement";
PineaFan638eb132023-01-19 10:41:22 +0000119 displayName = "Announcement Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500120 let oldTopic = (oldChannel as TextChannel).topic,
121 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 if (oldTopic) {
123 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100124 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
126 } else {
127 oldTopic = "None";
128 }
129 if (newTopic) {
130 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100131 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
133 } else {
134 newTopic = "None";
135 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500136 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) {
TheCodedProf309d6182023-01-18 18:10:29 -0500137 changes.nsfw = entry([(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"], `${(oldChannel as TextChannel).nsfw ? "On" : "Off"} -> ${(newChannel as TextChannel).nsfw ? "On" : "Off"}`);
TheCodedProf6ec331b2023-02-20 12:13:06 -0500138 }
139 if((oldChannel as TextChannel).defaultAutoArchiveDuration !== (newChannel as TextChannel).defaultAutoArchiveDuration) {
140 changes.autoArchiveDuration = entry(
141 [((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(), ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()],
142 `${humanizeDuration(((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)} -> ${humanizeDuration(((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)}`
143 );
144 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 break;
146 }
TheCodedProf309d6182023-01-18 18:10:29 -0500147 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 emoji = "CHANNEL.VOICE.EDIT";
149 readableType = "Voice";
150 displayName = "Voice Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500151 if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
152 changes.bitrate = entry([(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()], `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`);
153 if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100154 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500155 [(oldChannel as VoiceChannel).userLimit.toString(), (newChannel as VoiceChannel).userLimit.toString()],
156 `${(oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"} -> ${(newChannel as VoiceChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 );
TheCodedProf309d6182023-01-18 18:10:29 -0500158 if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 changes.region = entry(
PineaFan19dc9b82023-01-19 12:25:54 +0000160 [(oldChannel as VoiceChannel).rtcRegion ?? "automatic", (newChannel as VoiceChannel).rtcRegion ?? "automatic"],
161 `${capitalize((oldChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")} -> ${capitalize((newChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100162 );
163 break;
164 }
TheCodedProf309d6182023-01-18 18:10:29 -0500165 case ChannelType.GuildStageVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 emoji = "CHANNEL.VOICE.EDIT";
167 readableType = "Stage";
168 displayName = "Stage Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500169 let oldTopic = (oldChannel as StageChannel).topic,
170 newTopic = (newChannel as StageChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100171 if (oldTopic) {
172 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100173 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
175 } else {
176 oldTopic = "None";
177 }
178 if (newTopic) {
179 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100180 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
182 } else {
183 newTopic = "None";
184 }
TheCodedProf309d6182023-01-18 18:10:29 -0500185 if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
186 changes.bitrate = entry([(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()], `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`);
187 if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100188 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500189 [(oldChannel as StageChannel).userLimit.toString(), (newChannel as StageChannel).userLimit.toString()],
190 `${(oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"} -> ${(newChannel as StageChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 );
TheCodedProf309d6182023-01-18 18:10:29 -0500192 if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 changes.region = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500194 [(oldChannel as StageChannel).rtcRegion ?? "Automatic", (newChannel as StageChannel).rtcRegion ?? "Automatic"],
TheCodedProf6ec331b2023-02-20 12:13:06 -0500195 `${capitalize((oldChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")} -> ${capitalize((newChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 );
197 break;
198 }
TheCodedProf309d6182023-01-18 18:10:29 -0500199 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100200 emoji = "CHANNEL.CATEGORY.EDIT";
201 readableType = "Category";
202 displayName = "Category";
203 break;
204 }
205 default: {
206 emoji = "CHANNEL.TEXT.EDIT";
207 readableType = "Channel";
208 displayName = "Channel";
209 }
pineafan63fc5e22022-08-04 22:04:10 +0100210 }
PineaFan638eb132023-01-19 10:41:22 +0000211 const ocType = channelTypeEmoji[oldChannel.type],
TheCodedProf309d6182023-01-18 18:10:29 -0500212 ncType = channelTypeEmoji[newChannel.type];
PineaFane6ba7882023-01-18 20:41:16 +0000213 if (oldChannel.type !== newChannel.type)
TheCodedProf309d6182023-01-18 18:10:29 -0500214 changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100215 if (!(Object.values(changes).length - 4)) return;
216 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100217 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100218 type: "channelUpdate",
219 displayName: displayName + " Edited",
220 calculateType: "channelUpdate",
221 color: NucleusColors.yellow,
222 emoji: emoji,
TheCodedProf309d6182023-01-18 18:10:29 -0500223 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100224 },
225 list: changes,
226 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000227 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100228 }
pineafan63fc5e22022-08-04 22:04:10 +0100229 };
230 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100231}