blob: 5a4f8f78d23d8ec76cb13e127ee8ea6d8c347286 [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";
pineafan32767212022-03-14 21:27:39 +00007
TheCodedProf309d6182023-01-18 18:10:29 -05008const channelTypeEmoji: Record<number, string> = {
9 0: "Text", // Text channel
10 2: "Voice", // Voice channel
11 5: "Announcement", // Announcement channel
12 13: "Stage", // Stage channel
13 15: "Forum", // Forum channel
14 99: "Rules" // Rules channel
15};
16
17interface channelChanges {
18 channelId: { value: string | boolean | string[] | null; displayValue: string; };
19 channel: { value: string | boolean | string[] | null; displayValue: string; };
20 edited: { value: string | boolean | string[] | null; displayValue: string; };
21 editedBy: { value: string | boolean | string[] | null; displayValue: string; };
22 type?: { value: string | boolean | string[] | null; displayValue: string; };
23 name?: { value: string | boolean | string[] | null; displayValue: string; };
24 position?: { value: string | boolean | string[] | null; displayValue: string; };
25 description?: { value: string | boolean | string[] | null; displayValue: string; };
26 nsfw?: { value: string | boolean | string[] | null; displayValue: string; };
27 slowmode?: { value: string | boolean | string[] | null; displayValue: string; };
28 topic?: { value: string | boolean | string[] | null; displayValue: string; };
29 bitrate?: { value: string | boolean | string[] | null; displayValue: string; };
30 userLimit?: { value: string | boolean | string[] | null; displayValue: string; };
31 rateLimitPerUser?: { value: string | boolean | string[] | null; displayValue: string; };
32 parent?: { value: string | boolean | string[] | null; displayValue: string; };
33 permissionOverwrites?: { value: string | boolean | string[] | null; displayValue: string; };
34 region?: { value: string | boolean | string[] | null; displayValue: string; };
35 maxUsers?: { value: string | boolean | string[] | null; displayValue: string; };
36}
37
38
39
pineafan63fc5e22022-08-04 22:04:10 +010040export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +000041
PineaFane6ba7882023-01-18 20:41:16 +000042export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
43 const config = await client.memory.readGuildInfo(newChannel.guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010044 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger;
pineafan32767212022-03-14 21:27:39 +000045
PineaFane6ba7882023-01-18 20:41:16 +000046 if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000047
TheCodedProf309d6182023-01-18 18:10:29 -050048 const auditLog: GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate))
49 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id)[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate>;
50 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000051
Skyler Grey75ea9172022-08-06 10:22:23 +010052 let emoji: string;
53 let readableType: string;
54 let displayName: string;
TheCodedProf309d6182023-01-18 18:10:29 -050055 const changes: channelChanges = {
PineaFane6ba7882023-01-18 20:41:16 +000056 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
57 channel: entry(newChannel.id, renderChannel(newChannel)),
pineafan63fc5e22022-08-04 22:04:10 +010058 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProf309d6182023-01-18 18:10:29 -050059 editedBy: entry(auditLog.executor!.id, renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)),
pineafan63fc5e22022-08-04 22:04:10 +010060 };
PineaFane6ba7882023-01-18 20:41:16 +000061 if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
62 if (oldChannel.position !== newChannel.position)
TheCodedProf60a1f492023-01-18 16:59:20 -050063 changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`);
pineafan63fc5e22022-08-04 22:04:10 +010064
PineaFane6ba7882023-01-18 20:41:16 +000065 switch (newChannel.type) {
TheCodedProf309d6182023-01-18 18:10:29 -050066 case ChannelType.GuildText: {
Skyler Grey75ea9172022-08-06 10:22:23 +010067 emoji = "CHANNEL.TEXT.EDIT";
68 readableType = "Text";
69 displayName = "Text Channel";
TheCodedProf309d6182023-01-18 18:10:29 -050070 let oldTopic = (oldChannel as TextChannel).topic,
71 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +010072 if (oldTopic) {
73 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010074 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010075 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
76 } else {
77 oldTopic = "None";
78 }
79 if (newTopic) {
80 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010081 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010082 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
83 } else {
84 newTopic = "None";
85 }
86 const nsfw = ["", ""];
TheCodedProf309d6182023-01-18 18:10:29 -050087console.log((oldChannel as TextChannel).rateLimitPerUser, (newChannel as TextChannel).rateLimitPerUser);
88 nsfw[0] = (oldChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
89 nsfw[1] = (newChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
90 if ((oldChannel as TextChannel).topic !== (newChannel as TextChannel).topic)
91 changes.description = entry([(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
92 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]}`);
93 if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser && (oldChannel as TextChannel).rateLimitPerUser !== undefined)
Skyler Grey75ea9172022-08-06 10:22:23 +010094 changes.rateLimitPerUser = entry(
TheCodedProf309d6182023-01-18 18:10:29 -050095 [((oldChannel as TextChannel).rateLimitPerUser ?? 0).toString(), ((newChannel as TextChannel).rateLimitPerUser ?? 0).toString()],
96 `${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration((newChannel as TextChannel).rateLimitPerUser * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010097 );
pineafan63fc5e22022-08-04 22:04:10 +010098
Skyler Grey75ea9172022-08-06 10:22:23 +010099 break;
100 }
TheCodedProf309d6182023-01-18 18:10:29 -0500101 case ChannelType.GuildAnnouncement: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 emoji = "CHANNEL.TEXT.EDIT";
TheCodedProf309d6182023-01-18 18:10:29 -0500103 readableType = "Announcement";
104 displayName = "Announcment Channel";
105 let oldTopic = (oldChannel as TextChannel).topic,
106 newTopic = (newChannel as TextChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 if (oldTopic) {
108 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100109 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
111 } else {
112 oldTopic = "None";
113 }
114 if (newTopic) {
115 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100116 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
118 } else {
119 newTopic = "None";
120 }
TheCodedProf309d6182023-01-18 18:10:29 -0500121 if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw)
122 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 +0100123 break;
124 }
TheCodedProf309d6182023-01-18 18:10:29 -0500125 case ChannelType.GuildVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100126 emoji = "CHANNEL.VOICE.EDIT";
127 readableType = "Voice";
128 displayName = "Voice Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500129 if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
130 changes.bitrate = entry([(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()], `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`);
131 if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500133 [(oldChannel as VoiceChannel).userLimit.toString(), (newChannel as VoiceChannel).userLimit.toString()],
134 `${(oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"} -> ${(newChannel as VoiceChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 );
TheCodedProf309d6182023-01-18 18:10:29 -0500136 if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 changes.region = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500138 [(oldChannel as VoiceChannel).rtcRegion ?? "Automatic", (newChannel as VoiceChannel).rtcRegion ?? "Automatic"],
139 `${(oldChannel as VoiceChannel).rtcRegion?.toUpperCase() || "Automatic"} -> ${(newChannel as VoiceChannel).rtcRegion?.toUpperCase() || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 );
141 break;
142 }
TheCodedProf309d6182023-01-18 18:10:29 -0500143 case ChannelType.GuildStageVoice: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 emoji = "CHANNEL.VOICE.EDIT";
145 readableType = "Stage";
146 displayName = "Stage Channel";
TheCodedProf309d6182023-01-18 18:10:29 -0500147 let oldTopic = (oldChannel as StageChannel).topic,
148 newTopic = (newChannel as StageChannel).topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 if (oldTopic) {
150 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100151 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
153 } else {
154 oldTopic = "None";
155 }
156 if (newTopic) {
157 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100158 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
160 } else {
161 newTopic = "None";
162 }
TheCodedProf309d6182023-01-18 18:10:29 -0500163 if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
164 changes.bitrate = entry([(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()], `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`);
165 if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 changes.maxUsers = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500167 [(oldChannel as StageChannel).userLimit.toString(), (newChannel as StageChannel).userLimit.toString()],
168 `${(oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"} -> ${(newChannel as StageChannel).userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100169 );
TheCodedProf309d6182023-01-18 18:10:29 -0500170 if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100171 changes.region = entry(
TheCodedProf309d6182023-01-18 18:10:29 -0500172 [(oldChannel as StageChannel).rtcRegion ?? "Automatic", (newChannel as StageChannel).rtcRegion ?? "Automatic"],
173 `${(oldChannel as StageChannel).rtcRegion?.toUpperCase() || "Automatic"} -> ${(newChannel as StageChannel).rtcRegion?.toUpperCase() || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 );
175 break;
176 }
TheCodedProf309d6182023-01-18 18:10:29 -0500177 case ChannelType.GuildCategory: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 emoji = "CHANNEL.CATEGORY.EDIT";
179 readableType = "Category";
180 displayName = "Category";
181 break;
182 }
183 default: {
184 emoji = "CHANNEL.TEXT.EDIT";
185 readableType = "Channel";
186 displayName = "Channel";
187 }
pineafan63fc5e22022-08-04 22:04:10 +0100188 }
TheCodedProf309d6182023-01-18 18:10:29 -0500189 let ocType = channelTypeEmoji[oldChannel.type],
190 ncType = channelTypeEmoji[newChannel.type];
PineaFane6ba7882023-01-18 20:41:16 +0000191 if (oldChannel.type !== newChannel.type)
TheCodedProf309d6182023-01-18 18:10:29 -0500192 changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100193 if (!(Object.values(changes).length - 4)) return;
194 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100196 type: "channelUpdate",
197 displayName: displayName + " Edited",
198 calculateType: "channelUpdate",
199 color: NucleusColors.yellow,
200 emoji: emoji,
TheCodedProf309d6182023-01-18 18:10:29 -0500201 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +0100202 },
203 list: changes,
204 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000205 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100206 }
pineafan63fc5e22022-08-04 22:04:10 +0100207 };
208 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100209}
TheCodedProf309d6182023-01-18 18:10:29 -0500210//TODO: Capitialize RTC Regions