PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 1 | import { GuildChannel, AuditLogEvent } from 'discord.js'; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import humanizeDuration from "humanize-duration"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 3 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 5 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 6 | export const event = "channelUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 7 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 8 | export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) { |
| 9 | const config = await client.memory.readGuildInfo(newChannel.guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 10 | const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 11 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 12 | if (newChannel.parent && newChannel.parent.id === config.tickets.category) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 13 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 14 | const auditLog = await getAuditLog(newChannel.guild, "CHANNEL_UPDATE"); |
| 15 | const audit = auditLog.entries.filter((entry) => entry.target.id === newChannel.id).first(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | if (audit.executor.id === client.user.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 17 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | let emoji: string; |
| 19 | let readableType: string; |
| 20 | let displayName: string; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | const changes = { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 22 | channelId: entry(newChannel.id, `\`${newChannel.id}\``), |
| 23 | channel: entry(newChannel.id, renderChannel(newChannel)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | edited: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 25 | editedBy: entry(audit.executor.id, renderUser((await newChannel.guild.members.fetch(audit.executor.id)).user)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 26 | }; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 27 | if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`); |
| 28 | if (oldChannel.position !== newChannel.position) |
| 29 | changes.position = entry([oldChannel.position, newChannel.position], `${oldChannel.position} -> ${newChannel.position}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 31 | switch (newChannel.type) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | case "GUILD_TEXT": { |
| 33 | emoji = "CHANNEL.TEXT.EDIT"; |
| 34 | readableType = "Text"; |
| 35 | displayName = "Text Channel"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 36 | let oldTopic = oldChannel.topic, |
| 37 | newTopic = newChannel.topic; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | if (oldTopic) { |
| 39 | if (oldTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 40 | oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 42 | } else { |
| 43 | oldTopic = "None"; |
| 44 | } |
| 45 | if (newTopic) { |
| 46 | if (newTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 49 | } else { |
| 50 | newTopic = "None"; |
| 51 | } |
| 52 | const nsfw = ["", ""]; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 53 | nsfw[0] = oldChannel.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 54 | nsfw[1] = newChannel.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 55 | if (oldChannel.topic !== newChannel.topic) |
| 56 | changes.description = entry([oldChannel.topic, newChannel.topic], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`); |
| 57 | if (oldChannel.nsfw !== newChannel.nsfw) changes.nsfw = entry([oldChannel.nsfw, newChannel.nsfw], `${nsfw[0]} -> ${nsfw[1]}`); |
| 58 | if (oldChannel.rateLimitPerUser !== newChannel.rateLimitPerUser) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 59 | changes.rateLimitPerUser = entry( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 60 | [oldChannel.rateLimitPerUser, newChannel.rateLimitPerUser], |
| 61 | `${humanizeDuration(oldChannel.rateLimitPerUser * 1000)} -> ${humanizeDuration(newChannel.rateLimitPerUser * 1000)}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | break; |
| 65 | } |
| 66 | case "GUILD_NEWS": { |
| 67 | emoji = "CHANNEL.TEXT.EDIT"; |
| 68 | readableType = "News"; |
| 69 | displayName = "News Channel"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 70 | let oldTopic = oldChannel.topic, |
| 71 | newTopic = newChannel.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 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 86 | if (oldChannel.nsfw !== newChannel.nsfw) |
| 87 | changes.nsfw = entry([oldChannel.nsfw, newChannel.nsfw], `${oldChannel.nsfw ? "On" : "Off"} -> ${newChannel.nsfw ? "On" : "Off"}`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | case "GUILD_VOICE": { |
| 91 | emoji = "CHANNEL.VOICE.EDIT"; |
| 92 | readableType = "Voice"; |
| 93 | displayName = "Voice Channel"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 94 | if (oldChannel.bitrate !== newChannel.bitrate) |
| 95 | changes.bitrate = entry([oldChannel.bitrate, newChannel.bitrate], `${oldChannel.bitrate} -> ${newChannel.bitrate}`); |
| 96 | if (oldChannel.userLimit !== newChannel.userLimit) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | changes.maxUsers = entry( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 98 | [oldChannel.userLimit, newChannel.userLimit], |
| 99 | `${oldChannel.userLimit ? oldChannel.userLimit : "Unlimited"} -> ${newChannel.userLimit}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 100 | ); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 101 | if (oldChannel.rtcRegion !== newChannel.rtcRegion) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | changes.region = entry( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 103 | [oldChannel.rtcRegion, newChannel.rtcRegion], |
| 104 | `${oldChannel.rtcRegion || "Automatic"} -> ${newChannel.rtcRegion || "Automatic"}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | ); |
| 106 | break; |
| 107 | } |
| 108 | case "GUILD_STAGE": { |
| 109 | emoji = "CHANNEL.VOICE.EDIT"; |
| 110 | readableType = "Stage"; |
| 111 | displayName = "Stage Channel"; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 112 | let oldTopic = oldChannel.topic, |
| 113 | newTopic = newChannel.topic; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 114 | if (oldTopic) { |
| 115 | if (oldTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 116 | oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 118 | } else { |
| 119 | oldTopic = "None"; |
| 120 | } |
| 121 | if (newTopic) { |
| 122 | if (newTopic.length > 256) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 123 | newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 125 | } else { |
| 126 | newTopic = "None"; |
| 127 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 128 | if (oldChannel.bitrate !== newChannel.bitrate) |
| 129 | changes.bitrate = entry([oldChannel.bitrate, newChannel.bitrate], `${oldChannel.bitrate} -> ${newChannel.bitrate}`); |
| 130 | if (oldChannel.userLimit !== newChannel.userLimit) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | changes.maxUsers = entry( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 132 | [oldChannel.userLimit, newChannel.userLimit], |
| 133 | `${oldChannel.userLimit ? oldChannel.userLimit : "Unlimited"} -> ${newChannel.userLimit}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 134 | ); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 135 | if (oldChannel.rtcRegion !== newChannel.rtcRegion) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 136 | changes.region = entry( |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 137 | [oldChannel.rtcRegion, newChannel.rtcRegion], |
| 138 | `${oldChannel.rtcRegion || "Automatic"} -> ${newChannel.rtcRegion || "Automatic"}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 139 | ); |
| 140 | break; |
| 141 | } |
| 142 | case "GUILD_CATEGORY": { |
| 143 | emoji = "CHANNEL.CATEGORY.EDIT"; |
| 144 | readableType = "Category"; |
| 145 | displayName = "Category"; |
| 146 | break; |
| 147 | } |
| 148 | default: { |
| 149 | emoji = "CHANNEL.TEXT.EDIT"; |
| 150 | readableType = "Channel"; |
| 151 | displayName = "Channel"; |
| 152 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 154 | const t = oldChannel.type.split("_")[1]; |
| 155 | if (oldChannel.type !== newChannel.type) |
| 156 | changes.type = entry([oldChannel.type, newChannel.type], `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 157 | if (!(Object.values(changes).length - 4)) return; |
| 158 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 159 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | type: "channelUpdate", |
| 161 | displayName: displayName + " Edited", |
| 162 | calculateType: "channelUpdate", |
| 163 | color: NucleusColors.yellow, |
| 164 | emoji: emoji, |
| 165 | timestamp: audit.createdTimestamp |
| 166 | }, |
| 167 | list: changes, |
| 168 | hidden: { |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame^] | 169 | guild: newChannel.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 170 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 171 | }; |
| 172 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 173 | } |