blob: 2fc2a33ac9d559c17cdcd856a98a2d170d0620d2 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { GuildChannel, AuditLogEvent, ChannelType, TextChannel, VoiceChannel, StageChannel } from "discord.js";
2import type { GuildAuditLogsEntry } from "discord.js";
TheCodedProf309d6182023-01-18 18:10:29 -05003//@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";
TheCodedProf7b985d82023-06-08 16:40:41 -04007import client from "../utils/client.js";
PineaFan19dc9b82023-01-19 12:25:54 +00008import { capitalize } from "../utils/generateKeyValueList.js";
9
TheCodedProf7b985d82023-06-08 16:40:41 -040010let entry = client.logger.entry;
pineafan32767212022-03-14 21:27:39 +000011
TheCodedProf309d6182023-01-18 18:10:29 -050012const channelTypeEmoji: Record<number, string> = {
Skyler Greyda16adf2023-03-05 10:22:12 +000013 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
TheCodedProf309d6182023-01-18 18:10:29 -050019};
20
TheCodedProf7b985d82023-06-08 16:40:41 -040021type 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>;
TheCodedProf7b985d82023-06-08 16:40:41 -040040 [key: string]: unknown;
TheCodedProf309d6182023-01-18 18:10:29 -050041}
42
pineafan63fc5e22022-08-04 22:04:10 +010043export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +000044
TheCodedProf7b985d82023-06-08 16:40:41 -040045export async function callback(_client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050046 const { getAuditLog, log, isLogging, NucleusColors, renderDelta, renderUser, renderChannel } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000047 if (!(await isLogging(newChannel.guild.id, "channelUpdate"))) return;
PineaFane6ba7882023-01-18 20:41:16 +000048 const config = await client.memory.readGuildInfo(newChannel.guild.id);
TheCodedProfb493b8a2023-01-18 21:11:00 -050049 entry = client.logger.entry;
PineaFane6ba7882023-01-18 20:41:16 +000050 if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000051
Skyler Greyda16adf2023-03-05 10:22:12 +000052 const auditLog: null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (
53 await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate)
54 ).filter(
55 (entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id
56 )[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> | null;
PineaFanc4d6c3f2023-01-19 12:17:25 +000057 if (!auditLog) return;
TheCodedProf309d6182023-01-18 18:10:29 -050058 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000059
Skyler Grey75ea9172022-08-06 10:22:23 +010060 let emoji: string;
61 let readableType: string;
62 let displayName: string;
TheCodedProf309d6182023-01-18 18:10:29 -050063 const changes: channelChanges = {
PineaFane6ba7882023-01-18 20:41:16 +000064 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
65 channel: entry(newChannel.id, renderChannel(newChannel)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050066 edited: entry(Date.now(), renderDelta(Date.now())),
Skyler Greyda16adf2023-03-05 10:22:12 +000067 editedBy: entry(
68 auditLog.executor!.id,
69 renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)
70 )
pineafan63fc5e22022-08-04 22:04:10 +010071 };
Skyler Greyda16adf2023-03-05 10:22:12 +000072 if (oldChannel.name !== newChannel.name)
73 changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
PineaFane6ba7882023-01-18 20:41:16 +000074 if (oldChannel.position !== newChannel.position)
Skyler Greyda16adf2023-03-05 10:22:12 +000075 changes.position = entry(
76 [oldChannel.position.toString(), newChannel.position.toString()],
77 `${oldChannel.position} -> ${newChannel.position}`
78 );
pineafan63fc5e22022-08-04 22:04:10 +010079
PineaFane6ba7882023-01-18 20:41:16 +000080 switch (newChannel.type) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050081 case ChannelType.PrivateThread:
82 case ChannelType.PublicThread: {
83 return;
84 }
TheCodedProf309d6182023-01-18 18:10:29 -050085 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010086 emoji = "CHANNEL.TEXT.EDIT";
87 readableType = "Text";
88 displayName = "Text Channel";
TheCodedProf6ec331b2023-02-20 12:13:06 -050089 let oldTopic = (oldChannel as TextChannel).topic ?? "*None*",
90 newTopic = (oldChannel as TextChannel).topic ?? "*None*";
Skyler Grey75ea9172022-08-06 10:22:23 +010091 if (oldTopic) {
92 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010093 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010094 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
95 } else {
96 oldTopic = "None";
97 }
98 if (newTopic) {
99 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100100 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
102 } else {
103 newTopic = "None";
104 }
105 const nsfw = ["", ""];
Skyler Greyda16adf2023-03-05 10:22:12 +0000106 nsfw[0] = (oldChannel as TextChannel).nsfw
107 ? `${getEmojiByName("CONTROL.TICK")} Yes`
108 : `${getEmojiByName("CONTROL.CROSS")} No`;
109 nsfw[1] = (newChannel as TextChannel).nsfw
110 ? `${getEmojiByName("CONTROL.TICK")} Yes`
111 : `${getEmojiByName("CONTROL.CROSS")} No`;
TheCodedProf6ec331b2023-02-20 12:13:06 -0500112 if (oldTopic !== newTopic)
Skyler Greyda16adf2023-03-05 10:22:12 +0000113 changes.description = entry(
114 [(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""],
115 `\nBefore: ${oldTopic}\nAfter: ${newTopic}`
116 );
117 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw)
118 changes.nsfw = entry(
119 [(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"],
120 `${nsfw[0]} -> ${nsfw[1]}`
121 );
TheCodedProf6ec331b2023-02-20 12:13:06 -0500122 if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser)
123 changes.slowmode = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000124 [
125 (oldChannel as TextChannel).rateLimitPerUser.toString(),
126 (newChannel as TextChannel).rateLimitPerUser.toString()
127 ],
128 `${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration(
129 (newChannel as TextChannel).rateLimitPerUser * 1000
130 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 );
Skyler Greyda16adf2023-03-05 10:22:12 +0000132 if (
133 (oldChannel as TextChannel).defaultAutoArchiveDuration !==
134 (newChannel as TextChannel).defaultAutoArchiveDuration
135 ) {
TheCodedProf6ec331b2023-02-20 12:13:06 -0500136 changes.autoArchiveDuration = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000137 [
138 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(),
139 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()
140 ],
141 `${humanizeDuration(
142 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
143 )} -> ${humanizeDuration(
144 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
145 )}`
TheCodedProf6ec331b2023-02-20 12:13:06 -0500146 );
147 }
pineafan63fc5e22022-08-04 22:04:10 +0100148
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 break;
150 }
TheCodedProf309d6182023-01-18 18:10:29 -0500151 case ChannelType.GuildAnnouncement: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 emoji = "CHANNEL.TEXT.EDIT";
TheCodedProf309d6182023-01-18 18:10:29 -0500153 readableType = "Announcement";
PineaFan638eb132023-01-19 10:41:22 +0000154 displayName = "Announcement Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500155 let oldTopic = (oldChannel as TextChannel).topic,
156 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 if (oldTopic) {
158 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100159 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
161 } else {
162 oldTopic = "None";
163 }
164 if (newTopic) {
165 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100166 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100167 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
168 } else {
169 newTopic = "None";
170 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500171 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000172 changes.nsfw = entry(
173 [(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"],
174 `${(oldChannel as TextChannel).nsfw ? "On" : "Off"} -> ${
175 (newChannel as TextChannel).nsfw ? "On" : "Off"
176 }`
177 );
TheCodedProf6ec331b2023-02-20 12:13:06 -0500178 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000179 if (
180 (oldChannel as TextChannel).defaultAutoArchiveDuration !==
181 (newChannel as TextChannel).defaultAutoArchiveDuration
182 ) {
TheCodedProf6ec331b2023-02-20 12:13:06 -0500183 changes.autoArchiveDuration = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000184 [
185 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(),
186 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()
187 ],
188 `${humanizeDuration(
189 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
190 )} -> ${humanizeDuration(
191 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
192 )}`
TheCodedProf6ec331b2023-02-20 12:13:06 -0500193 );
194 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 break;
196 }
TheCodedProf309d6182023-01-18 18:10:29 -0500197 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100198 emoji = "CHANNEL.VOICE.EDIT";
199 readableType = "Voice";
200 displayName = "Voice Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500201 if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
Skyler Greyda16adf2023-03-05 10:22:12 +0000202 changes.bitrate = entry(
203 [(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()],
204 `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`
205 );
TheCodedProf309d6182023-01-18 18:10:29 -0500206 if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100207 changes.maxUsers = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000208 [
209 (oldChannel as VoiceChannel).userLimit.toString(),
210 (newChannel as VoiceChannel).userLimit.toString()
211 ],
212 `${
213 (oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"
214 } -> ${(newChannel as VoiceChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100215 );
TheCodedProf309d6182023-01-18 18:10:29 -0500216 if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100217 changes.region = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000218 [
219 (oldChannel as VoiceChannel).rtcRegion ?? "automatic",
220 (newChannel as VoiceChannel).rtcRegion ?? "automatic"
221 ],
222 `${capitalize(
223 (oldChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic"
224 )} -> ${capitalize((newChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100225 );
226 break;
227 }
TheCodedProf309d6182023-01-18 18:10:29 -0500228 case ChannelType.GuildStageVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100229 emoji = "CHANNEL.VOICE.EDIT";
230 readableType = "Stage";
231 displayName = "Stage Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500232 let oldTopic = (oldChannel as StageChannel).topic,
233 newTopic = (newChannel as StageChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100234 if (oldTopic) {
235 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100236 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100237 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
238 } else {
239 oldTopic = "None";
240 }
241 if (newTopic) {
242 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100243 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100244 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
245 } else {
246 newTopic = "None";
247 }
TheCodedProf309d6182023-01-18 18:10:29 -0500248 if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
Skyler Greyda16adf2023-03-05 10:22:12 +0000249 changes.bitrate = entry(
250 [(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()],
251 `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`
252 );
TheCodedProf309d6182023-01-18 18:10:29 -0500253 if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100254 changes.maxUsers = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000255 [
256 (oldChannel as StageChannel).userLimit.toString(),
257 (newChannel as StageChannel).userLimit.toString()
258 ],
259 `${
260 (oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"
261 } -> ${(newChannel as StageChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100262 );
TheCodedProf309d6182023-01-18 18:10:29 -0500263 if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100264 changes.region = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000265 [
266 (oldChannel as StageChannel).rtcRegion ?? "Automatic",
267 (newChannel as StageChannel).rtcRegion ?? "Automatic"
268 ],
269 `${capitalize(
270 (oldChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic"
271 )} -> ${capitalize((newChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100272 );
273 break;
274 }
TheCodedProf309d6182023-01-18 18:10:29 -0500275 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100276 emoji = "CHANNEL.CATEGORY.EDIT";
277 readableType = "Category";
278 displayName = "Category";
279 break;
280 }
281 default: {
282 emoji = "CHANNEL.TEXT.EDIT";
283 readableType = "Channel";
284 displayName = "Channel";
285 }
pineafan63fc5e22022-08-04 22:04:10 +0100286 }
PineaFan638eb132023-01-19 10:41:22 +0000287 const ocType = channelTypeEmoji[oldChannel.type],
TheCodedProf309d6182023-01-18 18:10:29 -0500288 ncType = channelTypeEmoji[newChannel.type];
Skyler Greyda16adf2023-03-05 10:22:12 +0000289 if (oldChannel.type !== newChannel.type) changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100290 if (!(Object.values(changes).length - 4)) return;
291 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100292 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100293 type: "channelUpdate",
294 displayName: displayName + " Edited",
295 calculateType: "channelUpdate",
296 color: NucleusColors.yellow,
297 emoji: emoji,
TheCodedProf309d6182023-01-18 18:10:29 -0500298 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100299 },
300 list: changes,
301 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000302 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100303 }
pineafan63fc5e22022-08-04 22:04:10 +0100304 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000305 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100306}