blob: 6491cc9d463988892fef247b95897215be5aeb14 [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";
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> = {
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
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
pineafan63fc5e22022-08-04 22:04:10 +010042export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +000043
PineaFane6ba7882023-01-18 20:41:16 +000044export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050045 const { getAuditLog, log, isLogging, NucleusColors, renderDelta, renderUser, renderChannel } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000046 if (!(await isLogging(newChannel.guild.id, "channelUpdate"))) return;
PineaFane6ba7882023-01-18 20:41:16 +000047 const config = await client.memory.readGuildInfo(newChannel.guild.id);
TheCodedProfb493b8a2023-01-18 21:11:00 -050048 entry = client.logger.entry;
PineaFane6ba7882023-01-18 20:41:16 +000049 if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000050
Skyler Greyda16adf2023-03-05 10:22:12 +000051 const auditLog: null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (
52 await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate)
53 ).filter(
54 (entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id
55 )[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> | null;
PineaFanc4d6c3f2023-01-19 12:17:25 +000056 if (!auditLog) return;
TheCodedProf309d6182023-01-18 18:10:29 -050057 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000058
Skyler Grey75ea9172022-08-06 10:22:23 +010059 let emoji: string;
60 let readableType: string;
61 let displayName: string;
TheCodedProf309d6182023-01-18 18:10:29 -050062 const changes: channelChanges = {
PineaFane6ba7882023-01-18 20:41:16 +000063 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
64 channel: entry(newChannel.id, renderChannel(newChannel)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050065 edited: entry(Date.now(), renderDelta(Date.now())),
Skyler Greyda16adf2023-03-05 10:22:12 +000066 editedBy: entry(
67 auditLog.executor!.id,
68 renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)
69 )
pineafan63fc5e22022-08-04 22:04:10 +010070 };
Skyler Greyda16adf2023-03-05 10:22:12 +000071 if (oldChannel.name !== newChannel.name)
72 changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
PineaFane6ba7882023-01-18 20:41:16 +000073 if (oldChannel.position !== newChannel.position)
Skyler Greyda16adf2023-03-05 10:22:12 +000074 changes.position = entry(
75 [oldChannel.position.toString(), newChannel.position.toString()],
76 `${oldChannel.position} -> ${newChannel.position}`
77 );
pineafan63fc5e22022-08-04 22:04:10 +010078
PineaFane6ba7882023-01-18 20:41:16 +000079 switch (newChannel.type) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050080 case ChannelType.PrivateThread:
81 case ChannelType.PublicThread: {
82 return;
83 }
TheCodedProf309d6182023-01-18 18:10:29 -050084 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010085 emoji = "CHANNEL.TEXT.EDIT";
86 readableType = "Text";
87 displayName = "Text Channel";
TheCodedProf6ec331b2023-02-20 12:13:06 -050088 let oldTopic = (oldChannel as TextChannel).topic ?? "*None*",
89 newTopic = (oldChannel as TextChannel).topic ?? "*None*";
Skyler Grey75ea9172022-08-06 10:22:23 +010090 if (oldTopic) {
91 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010092 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010093 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
94 } else {
95 oldTopic = "None";
96 }
97 if (newTopic) {
98 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010099 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
101 } else {
102 newTopic = "None";
103 }
104 const nsfw = ["", ""];
Skyler Greyda16adf2023-03-05 10:22:12 +0000105 nsfw[0] = (oldChannel as TextChannel).nsfw
106 ? `${getEmojiByName("CONTROL.TICK")} Yes`
107 : `${getEmojiByName("CONTROL.CROSS")} No`;
108 nsfw[1] = (newChannel as TextChannel).nsfw
109 ? `${getEmojiByName("CONTROL.TICK")} Yes`
110 : `${getEmojiByName("CONTROL.CROSS")} No`;
TheCodedProf6ec331b2023-02-20 12:13:06 -0500111 if (oldTopic !== newTopic)
Skyler Greyda16adf2023-03-05 10:22:12 +0000112 changes.description = entry(
113 [(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""],
114 `\nBefore: ${oldTopic}\nAfter: ${newTopic}`
115 );
116 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw)
117 changes.nsfw = entry(
118 [(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"],
119 `${nsfw[0]} -> ${nsfw[1]}`
120 );
TheCodedProf6ec331b2023-02-20 12:13:06 -0500121 if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser)
122 changes.slowmode = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000123 [
124 (oldChannel as TextChannel).rateLimitPerUser.toString(),
125 (newChannel as TextChannel).rateLimitPerUser.toString()
126 ],
127 `${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration(
128 (newChannel as TextChannel).rateLimitPerUser * 1000
129 )}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 );
Skyler Greyda16adf2023-03-05 10:22:12 +0000131 if (
132 (oldChannel as TextChannel).defaultAutoArchiveDuration !==
133 (newChannel as TextChannel).defaultAutoArchiveDuration
134 ) {
TheCodedProf6ec331b2023-02-20 12:13:06 -0500135 changes.autoArchiveDuration = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000136 [
137 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(),
138 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()
139 ],
140 `${humanizeDuration(
141 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
142 )} -> ${humanizeDuration(
143 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
144 )}`
TheCodedProf6ec331b2023-02-20 12:13:06 -0500145 );
146 }
pineafan63fc5e22022-08-04 22:04:10 +0100147
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 break;
149 }
TheCodedProf309d6182023-01-18 18:10:29 -0500150 case ChannelType.GuildAnnouncement: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 emoji = "CHANNEL.TEXT.EDIT";
TheCodedProf309d6182023-01-18 18:10:29 -0500152 readableType = "Announcement";
PineaFan638eb132023-01-19 10:41:22 +0000153 displayName = "Announcement Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500154 let oldTopic = (oldChannel as TextChannel).topic,
155 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100156 if (oldTopic) {
157 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100158 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
160 } else {
161 oldTopic = "None";
162 }
163 if (newTopic) {
164 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100165 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
167 } else {
168 newTopic = "None";
169 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500170 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000171 changes.nsfw = entry(
172 [(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"],
173 `${(oldChannel as TextChannel).nsfw ? "On" : "Off"} -> ${
174 (newChannel as TextChannel).nsfw ? "On" : "Off"
175 }`
176 );
TheCodedProf6ec331b2023-02-20 12:13:06 -0500177 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000178 if (
179 (oldChannel as TextChannel).defaultAutoArchiveDuration !==
180 (newChannel as TextChannel).defaultAutoArchiveDuration
181 ) {
TheCodedProf6ec331b2023-02-20 12:13:06 -0500182 changes.autoArchiveDuration = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000183 [
184 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(),
185 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()
186 ],
187 `${humanizeDuration(
188 ((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
189 )} -> ${humanizeDuration(
190 ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000
191 )}`
TheCodedProf6ec331b2023-02-20 12:13:06 -0500192 );
193 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100194 break;
195 }
TheCodedProf309d6182023-01-18 18:10:29 -0500196 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100197 emoji = "CHANNEL.VOICE.EDIT";
198 readableType = "Voice";
199 displayName = "Voice Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500200 if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
Skyler Greyda16adf2023-03-05 10:22:12 +0000201 changes.bitrate = entry(
202 [(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()],
203 `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`
204 );
TheCodedProf309d6182023-01-18 18:10:29 -0500205 if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100206 changes.maxUsers = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000207 [
208 (oldChannel as VoiceChannel).userLimit.toString(),
209 (newChannel as VoiceChannel).userLimit.toString()
210 ],
211 `${
212 (oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"
213 } -> ${(newChannel as VoiceChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100214 );
TheCodedProf309d6182023-01-18 18:10:29 -0500215 if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100216 changes.region = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000217 [
218 (oldChannel as VoiceChannel).rtcRegion ?? "automatic",
219 (newChannel as VoiceChannel).rtcRegion ?? "automatic"
220 ],
221 `${capitalize(
222 (oldChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic"
223 )} -> ${capitalize((newChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100224 );
225 break;
226 }
TheCodedProf309d6182023-01-18 18:10:29 -0500227 case ChannelType.GuildStageVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100228 emoji = "CHANNEL.VOICE.EDIT";
229 readableType = "Stage";
230 displayName = "Stage Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500231 let oldTopic = (oldChannel as StageChannel).topic,
232 newTopic = (newChannel as StageChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100233 if (oldTopic) {
234 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100235 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100236 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
237 } else {
238 oldTopic = "None";
239 }
240 if (newTopic) {
241 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100242 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100243 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
244 } else {
245 newTopic = "None";
246 }
TheCodedProf309d6182023-01-18 18:10:29 -0500247 if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
Skyler Greyda16adf2023-03-05 10:22:12 +0000248 changes.bitrate = entry(
249 [(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()],
250 `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`
251 );
TheCodedProf309d6182023-01-18 18:10:29 -0500252 if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100253 changes.maxUsers = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000254 [
255 (oldChannel as StageChannel).userLimit.toString(),
256 (newChannel as StageChannel).userLimit.toString()
257 ],
258 `${
259 (oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"
260 } -> ${(newChannel as StageChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100261 );
TheCodedProf309d6182023-01-18 18:10:29 -0500262 if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100263 changes.region = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +0000264 [
265 (oldChannel as StageChannel).rtcRegion ?? "Automatic",
266 (newChannel as StageChannel).rtcRegion ?? "Automatic"
267 ],
268 `${capitalize(
269 (oldChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic"
270 )} -> ${capitalize((newChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100271 );
272 break;
273 }
TheCodedProf309d6182023-01-18 18:10:29 -0500274 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100275 emoji = "CHANNEL.CATEGORY.EDIT";
276 readableType = "Category";
277 displayName = "Category";
278 break;
279 }
280 default: {
281 emoji = "CHANNEL.TEXT.EDIT";
282 readableType = "Channel";
283 displayName = "Channel";
284 }
pineafan63fc5e22022-08-04 22:04:10 +0100285 }
PineaFan638eb132023-01-19 10:41:22 +0000286 const ocType = channelTypeEmoji[oldChannel.type],
TheCodedProf309d6182023-01-18 18:10:29 -0500287 ncType = channelTypeEmoji[newChannel.type];
Skyler Greyda16adf2023-03-05 10:22:12 +0000288 if (oldChannel.type !== newChannel.type) changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100289 if (!(Object.values(changes).length - 4)) return;
290 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100291 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100292 type: "channelUpdate",
293 displayName: displayName + " Edited",
294 calculateType: "channelUpdate",
295 color: NucleusColors.yellow,
296 emoji: emoji,
TheCodedProf309d6182023-01-18 18:10:29 -0500297 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100298 },
299 list: changes,
300 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000301 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100302 }
pineafan63fc5e22022-08-04 22:04:10 +0100303 };
304 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100305}