blob: 6c8687e0fe54d40d7cfd36b1e91c1a20acfb5fd6 [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>;
35 rateLimitPerUser?: ReturnType<typeof entry>;
36 parent?: ReturnType<typeof entry>;
37 permissionOverwrites?: ReturnType<typeof entry>;
38 region?: ReturnType<typeof entry>;
39 maxUsers?: 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) {
47 const config = await client.memory.readGuildInfo(newChannel.guild.id);
TheCodedProfb493b8a2023-01-18 21:11:00 -050048 const { getAuditLog, log, NucleusColors, renderDelta, renderUser, renderChannel } = client.logger;
49 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
PineaFanc4d6c3f2023-01-19 12:17:25 +000052 const auditLog: null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate))
53 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id)[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> | null;
54 if (!auditLog) return;
TheCodedProf309d6182023-01-18 18:10:29 -050055 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000056
Skyler Grey75ea9172022-08-06 10:22:23 +010057 let emoji: string;
58 let readableType: string;
59 let displayName: string;
TheCodedProf309d6182023-01-18 18:10:29 -050060 const changes: channelChanges = {
PineaFane6ba7882023-01-18 20:41:16 +000061 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
62 channel: entry(newChannel.id, renderChannel(newChannel)),
pineafan63fc5e22022-08-04 22:04:10 +010063 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProf309d6182023-01-18 18:10:29 -050064 editedBy: entry(auditLog.executor!.id, renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)),
pineafan63fc5e22022-08-04 22:04:10 +010065 };
PineaFane6ba7882023-01-18 20:41:16 +000066 if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
67 if (oldChannel.position !== newChannel.position)
TheCodedProf60a1f492023-01-18 16:59:20 -050068 changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`);
pineafan63fc5e22022-08-04 22:04:10 +010069
PineaFane6ba7882023-01-18 20:41:16 +000070 switch (newChannel.type) {
TheCodedProf309d6182023-01-18 18:10:29 -050071 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010072 emoji = "CHANNEL.TEXT.EDIT";
73 readableType = "Text";
74 displayName = "Text Channel";
TheCodedProf309d6182023-01-18 18:10:29 -050075 let oldTopic = (oldChannel as TextChannel).topic,
76 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +010077 if (oldTopic) {
78 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010079 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010080 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
81 } else {
82 oldTopic = "None";
83 }
84 if (newTopic) {
85 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010086 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010087 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
88 } else {
89 newTopic = "None";
90 }
91 const nsfw = ["", ""];
TheCodedProf309d6182023-01-18 18:10:29 -050092 nsfw[0] = (oldChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
93 nsfw[1] = (newChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
94 if ((oldChannel as TextChannel).topic !== (newChannel as TextChannel).topic)
95 changes.description = entry([(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
96 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]}`);
PineaFan638eb132023-01-19 10:41:22 +000097 if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser && (oldChannel as TextChannel).rateLimitPerUser !== 0)
Skyler Grey75ea9172022-08-06 10:22:23 +010098 changes.rateLimitPerUser = entry(
PineaFan638eb132023-01-19 10:41:22 +000099 [((oldChannel as TextChannel).rateLimitPerUser).toString(), ((newChannel as TextChannel).rateLimitPerUser).toString()],
TheCodedProf309d6182023-01-18 18:10:29 -0500100 `${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration((newChannel as TextChannel).rateLimitPerUser * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 );
pineafan63fc5e22022-08-04 22:04:10 +0100102
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 break;
104 }
TheCodedProf309d6182023-01-18 18:10:29 -0500105 case ChannelType.GuildAnnouncement: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 emoji = "CHANNEL.TEXT.EDIT";
TheCodedProf309d6182023-01-18 18:10:29 -0500107 readableType = "Announcement";
PineaFan638eb132023-01-19 10:41:22 +0000108 displayName = "Announcement Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500109 let oldTopic = (oldChannel as TextChannel).topic,
110 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 if (oldTopic) {
112 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
115 } else {
116 oldTopic = "None";
117 }
118 if (newTopic) {
119 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100120 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
122 } else {
123 newTopic = "None";
124 }
TheCodedProf309d6182023-01-18 18:10:29 -0500125 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw)
126 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"}`);
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 break;
128 }
TheCodedProf309d6182023-01-18 18:10:29 -0500129 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 emoji = "CHANNEL.VOICE.EDIT";
131 readableType = "Voice";
132 displayName = "Voice Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500133 if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
134 changes.bitrate = entry([(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()], `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`);
135 if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500137 [(oldChannel as VoiceChannel).userLimit.toString(), (newChannel as VoiceChannel).userLimit.toString()],
138 `${(oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"} -> ${(newChannel as VoiceChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 );
TheCodedProf309d6182023-01-18 18:10:29 -0500140 if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 changes.region = entry(
PineaFan19dc9b82023-01-19 12:25:54 +0000142 [(oldChannel as VoiceChannel).rtcRegion ?? "automatic", (newChannel as VoiceChannel).rtcRegion ?? "automatic"],
143 `${capitalize((oldChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")} -> ${capitalize((newChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 );
145 break;
146 }
TheCodedProf309d6182023-01-18 18:10:29 -0500147 case ChannelType.GuildStageVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 emoji = "CHANNEL.VOICE.EDIT";
149 readableType = "Stage";
150 displayName = "Stage Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500151 let oldTopic = (oldChannel as StageChannel).topic,
152 newTopic = (newChannel as StageChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 if (oldTopic) {
154 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100155 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100156 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
157 } else {
158 oldTopic = "None";
159 }
160 if (newTopic) {
161 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100162 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100163 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
164 } else {
165 newTopic = "None";
166 }
TheCodedProf309d6182023-01-18 18:10:29 -0500167 if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
168 changes.bitrate = entry([(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()], `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`);
169 if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500171 [(oldChannel as StageChannel).userLimit.toString(), (newChannel as StageChannel).userLimit.toString()],
172 `${(oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"} -> ${(newChannel as StageChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100173 );
TheCodedProf309d6182023-01-18 18:10:29 -0500174 if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100175 changes.region = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500176 [(oldChannel as StageChannel).rtcRegion ?? "Automatic", (newChannel as StageChannel).rtcRegion ?? "Automatic"],
PineaFan5d98a4b2023-01-19 16:15:47 +0000177 `${capitalize((oldChannel as StageChannel).rtcRegion?.toUpperCase() ?? "automatic")} -> ${capitalize((newChannel as StageChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 );
179 break;
180 }
TheCodedProf309d6182023-01-18 18:10:29 -0500181 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 emoji = "CHANNEL.CATEGORY.EDIT";
183 readableType = "Category";
184 displayName = "Category";
185 break;
186 }
187 default: {
188 emoji = "CHANNEL.TEXT.EDIT";
189 readableType = "Channel";
190 displayName = "Channel";
191 }
pineafan63fc5e22022-08-04 22:04:10 +0100192 }
PineaFan638eb132023-01-19 10:41:22 +0000193 const ocType = channelTypeEmoji[oldChannel.type],
TheCodedProf309d6182023-01-18 18:10:29 -0500194 ncType = channelTypeEmoji[newChannel.type];
PineaFane6ba7882023-01-18 20:41:16 +0000195 if (oldChannel.type !== newChannel.type)
TheCodedProf309d6182023-01-18 18:10:29 -0500196 changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100197 if (!(Object.values(changes).length - 4)) return;
198 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100199 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100200 type: "channelUpdate",
201 displayName: displayName + " Edited",
202 calculateType: "channelUpdate",
203 color: NucleusColors.yellow,
204 emoji: emoji,
TheCodedProf309d6182023-01-18 18:10:29 -0500205 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100206 },
207 list: changes,
208 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000209 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100210 }
pineafan63fc5e22022-08-04 22:04:10 +0100211 };
212 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100213}