TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 1 | import { GuildChannel, AuditLogEvent, ChannelType, TextChannel, VoiceChannel, StageChannel } from 'discord.js'; |
| 2 | import type { GuildAuditLogsEntry } from 'discord.js'; |
| 3 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import humanizeDuration from "humanize-duration"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 5 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 6 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 7 | |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 8 | const 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 | |
| 17 | interface 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 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | export const event = "channelUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 41 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 42 | export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) { |
| 43 | const config = await client.memory.readGuildInfo(newChannel.guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 44 | const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 45 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 46 | if (newChannel.parent && newChannel.parent.id === config.tickets.category) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 47 | |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 48 | 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; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 51 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | let emoji: string; |
| 53 | let readableType: string; |
| 54 | let displayName: string; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 55 | const changes: channelChanges = { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 56 | channelId: entry(newChannel.id, `\`${newChannel.id}\``), |
| 57 | channel: entry(newChannel.id, renderChannel(newChannel)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | edited: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 59 | editedBy: entry(auditLog.executor!.id, renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | }; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 61 | if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`); |
| 62 | if (oldChannel.position !== newChannel.position) |
TheCodedProf | 60a1f49 | 2023-01-18 16:59:20 -0500 | [diff] [blame] | 63 | changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 65 | switch (newChannel.type) { |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 66 | case ChannelType.GuildText: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | emoji = "CHANNEL.TEXT.EDIT"; |
| 68 | readableType = "Text"; |
| 69 | displayName = "Text Channel"; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 70 | let oldTopic = (oldChannel as TextChannel).topic, |
| 71 | newTopic = (newChannel as TextChannel).topic; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 72 | if (oldTopic) { |
| 73 | if (oldTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 74 | oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 76 | } else { |
| 77 | oldTopic = "None"; |
| 78 | } |
| 79 | if (newTopic) { |
| 80 | if (newTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 81 | newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 83 | } else { |
| 84 | newTopic = "None"; |
| 85 | } |
| 86 | const nsfw = ["", ""]; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 87 | console.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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | changes.rateLimitPerUser = entry( |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 95 | [((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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 98 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 99 | break; |
| 100 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 101 | case ChannelType.GuildAnnouncement: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | emoji = "CHANNEL.TEXT.EDIT"; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 103 | readableType = "Announcement"; |
| 104 | displayName = "Announcment Channel"; |
| 105 | let oldTopic = (oldChannel as TextChannel).topic, |
| 106 | newTopic = (newChannel as TextChannel).topic; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | if (oldTopic) { |
| 108 | if (oldTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 109 | oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 111 | } else { |
| 112 | oldTopic = "None"; |
| 113 | } |
| 114 | if (newTopic) { |
| 115 | if (newTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 116 | newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 118 | } else { |
| 119 | newTopic = "None"; |
| 120 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 121 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | break; |
| 124 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 125 | case ChannelType.GuildVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 126 | emoji = "CHANNEL.VOICE.EDIT"; |
| 127 | readableType = "Voice"; |
| 128 | displayName = "Voice Channel"; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 129 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | changes.maxUsers = entry( |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 133 | [(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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | ); |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 136 | if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 137 | changes.region = entry( |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 138 | [(oldChannel as VoiceChannel).rtcRegion ?? "Automatic", (newChannel as VoiceChannel).rtcRegion ?? "Automatic"], |
| 139 | `${(oldChannel as VoiceChannel).rtcRegion?.toUpperCase() || "Automatic"} -> ${(newChannel as VoiceChannel).rtcRegion?.toUpperCase() || "Automatic"}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | ); |
| 141 | break; |
| 142 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 143 | case ChannelType.GuildStageVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | emoji = "CHANNEL.VOICE.EDIT"; |
| 145 | readableType = "Stage"; |
| 146 | displayName = "Stage Channel"; |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 147 | let oldTopic = (oldChannel as StageChannel).topic, |
| 148 | newTopic = (newChannel as StageChannel).topic; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | if (oldTopic) { |
| 150 | if (oldTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 151 | oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 153 | } else { |
| 154 | oldTopic = "None"; |
| 155 | } |
| 156 | if (newTopic) { |
| 157 | if (newTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 158 | newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 159 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 160 | } else { |
| 161 | newTopic = "None"; |
| 162 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 163 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 166 | changes.maxUsers = entry( |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 167 | [(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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 169 | ); |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 170 | if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 171 | changes.region = entry( |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 172 | [(oldChannel as StageChannel).rtcRegion ?? "Automatic", (newChannel as StageChannel).rtcRegion ?? "Automatic"], |
| 173 | `${(oldChannel as StageChannel).rtcRegion?.toUpperCase() || "Automatic"} -> ${(newChannel as StageChannel).rtcRegion?.toUpperCase() || "Automatic"}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 174 | ); |
| 175 | break; |
| 176 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 177 | case ChannelType.GuildCategory: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 178 | 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 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 188 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 189 | let ocType = channelTypeEmoji[oldChannel.type], |
| 190 | ncType = channelTypeEmoji[newChannel.type]; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 191 | if (oldChannel.type !== newChannel.type) |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 192 | changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 193 | if (!(Object.values(changes).length - 4)) return; |
| 194 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 195 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | type: "channelUpdate", |
| 197 | displayName: displayName + " Edited", |
| 198 | calculateType: "channelUpdate", |
| 199 | color: NucleusColors.yellow, |
| 200 | emoji: emoji, |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 201 | timestamp: auditLog.createdTimestamp |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 202 | }, |
| 203 | list: changes, |
| 204 | hidden: { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 205 | guild: newChannel.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 206 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 207 | }; |
| 208 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 209 | } |
TheCodedProf | 309d618 | 2023-01-18 18:10:29 -0500 | [diff] [blame] | 210 | //TODO: Capitialize RTC Regions |