pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import humanizeDuration from "humanize-duration"; |
| 2 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 3 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | export const event = "channelUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 5 | |
| 6 | export async function callback(client, oc, nc) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | const config = await client.memory.readGuildInfo(nc.guild.id); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 8 | const { |
| 9 | getAuditLog, |
| 10 | log, |
| 11 | NucleusColors, |
| 12 | entry, |
| 13 | renderDelta, |
| 14 | renderUser, |
| 15 | renderChannel |
| 16 | } = client.logger; |
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 | if (nc.parent && nc.parent.id === config.tickets.category) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 19 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | const auditLog = await getAuditLog(nc.guild, "CHANNEL_UPDATE"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 21 | const audit = auditLog.entries |
| 22 | .filter((entry) => entry.target.id === nc.id) |
| 23 | .first(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | if (audit.executor.id === client.user.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 25 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 26 | let emoji: string; |
| 27 | let readableType: string; |
| 28 | let displayName: string; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | const changes = { |
| 30 | channelId: entry(nc.id, `\`${nc.id}\``), |
| 31 | channel: entry(nc.id, renderChannel(nc)), |
| 32 | edited: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 33 | editedBy: entry( |
| 34 | audit.executor.id, |
| 35 | renderUser((await nc.guild.members.fetch(audit.executor.id)).user) |
| 36 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 38 | if (oc.name !== nc.name) |
| 39 | changes.name = entry([oc.name, nc.name], `${oc.name} -> ${nc.name}`); |
| 40 | if (oc.position !== nc.position) |
| 41 | changes.position = entry( |
| 42 | [oc.position, nc.position], |
| 43 | `${oc.position} -> ${nc.position}` |
| 44 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | |
| 46 | switch (nc.type) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 47 | case "GUILD_TEXT": { |
| 48 | emoji = "CHANNEL.TEXT.EDIT"; |
| 49 | readableType = "Text"; |
| 50 | displayName = "Text Channel"; |
| 51 | let oldTopic = oc.topic, |
| 52 | newTopic = nc.topic; |
| 53 | if (oldTopic) { |
| 54 | if (oldTopic.length > 256) |
| 55 | oldTopic = `\`\`\`\n${ |
| 56 | oldTopic.replace("`", "'").substring(0, 253) + "..." |
| 57 | }\n\`\`\``; |
| 58 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 59 | } else { |
| 60 | oldTopic = "None"; |
| 61 | } |
| 62 | if (newTopic) { |
| 63 | if (newTopic.length > 256) |
| 64 | newTopic = `\`\`\`\n${ |
| 65 | newTopic.replace("`", "'").substring(0, 253) + "..." |
| 66 | }\n\`\`\``; |
| 67 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 68 | } else { |
| 69 | newTopic = "None"; |
| 70 | } |
| 71 | const nsfw = ["", ""]; |
| 72 | nsfw[0] = oc.nsfw |
| 73 | ? `${getEmojiByName("CONTROL.TICK")} Yes` |
| 74 | : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 75 | nsfw[1] = nc.nsfw |
| 76 | ? `${getEmojiByName("CONTROL.TICK")} Yes` |
| 77 | : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 78 | if (oc.topic !== nc.topic) |
| 79 | changes.description = entry( |
| 80 | [oc.topic, nc.topic], |
| 81 | `\nBefore: ${oldTopic}\nAfter: ${newTopic}` |
| 82 | ); |
| 83 | if (oc.nsfw !== nc.nsfw) |
| 84 | changes.nsfw = entry( |
| 85 | [oc.nsfw, nc.nsfw], |
| 86 | `${nsfw[0]} -> ${nsfw[1]}` |
| 87 | ); |
| 88 | if (oc.rateLimitPerUser !== nc.rateLimitPerUser) |
| 89 | changes.rateLimitPerUser = entry( |
| 90 | [oc.rateLimitPerUser, nc.rateLimitPerUser], |
| 91 | `${humanizeDuration( |
| 92 | oc.rateLimitPerUser * 1000 |
| 93 | )} -> ${humanizeDuration(nc.rateLimitPerUser * 1000)}` |
| 94 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 96 | break; |
| 97 | } |
| 98 | case "GUILD_NEWS": { |
| 99 | emoji = "CHANNEL.TEXT.EDIT"; |
| 100 | readableType = "News"; |
| 101 | displayName = "News Channel"; |
| 102 | let oldTopic = oc.topic, |
| 103 | newTopic = nc.topic; |
| 104 | if (oldTopic) { |
| 105 | if (oldTopic.length > 256) |
| 106 | oldTopic = `\`\`\`\n${ |
| 107 | oldTopic.replace("`", "'").substring(0, 253) + "..." |
| 108 | }\n\`\`\``; |
| 109 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 110 | } else { |
| 111 | oldTopic = "None"; |
| 112 | } |
| 113 | if (newTopic) { |
| 114 | if (newTopic.length > 256) |
| 115 | newTopic = `\`\`\`\n${ |
| 116 | newTopic.replace("`", "'").substring(0, 253) + "..." |
| 117 | }\n\`\`\``; |
| 118 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 119 | } else { |
| 120 | newTopic = "None"; |
| 121 | } |
| 122 | if (oc.nsfw !== nc.nsfw) |
| 123 | changes.nsfw = entry( |
| 124 | [oc.nsfw, nc.nsfw], |
| 125 | `${oc.nsfw ? "On" : "Off"} -> ${nc.nsfw ? "On" : "Off"}` |
| 126 | ); |
| 127 | break; |
| 128 | } |
| 129 | case "GUILD_VOICE": { |
| 130 | emoji = "CHANNEL.VOICE.EDIT"; |
| 131 | readableType = "Voice"; |
| 132 | displayName = "Voice Channel"; |
| 133 | if (oc.bitrate !== nc.bitrate) |
| 134 | changes.bitrate = entry( |
| 135 | [oc.bitrate, nc.bitrate], |
| 136 | `${oc.bitrate} -> ${nc.bitrate}` |
| 137 | ); |
| 138 | if (oc.userLimit !== nc.userLimit) |
| 139 | changes.maxUsers = entry( |
| 140 | [oc.userLimit, nc.userLimit], |
| 141 | `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${ |
| 142 | nc.userLimit |
| 143 | }` |
| 144 | ); |
| 145 | if (oc.rtcRegion !== nc.rtcRegion) |
| 146 | changes.region = entry( |
| 147 | [oc.rtcRegion, nc.rtcRegion], |
| 148 | `${oc.rtcRegion || "Automatic"} -> ${ |
| 149 | nc.rtcRegion || "Automatic" |
| 150 | }` |
| 151 | ); |
| 152 | break; |
| 153 | } |
| 154 | case "GUILD_STAGE": { |
| 155 | emoji = "CHANNEL.VOICE.EDIT"; |
| 156 | readableType = "Stage"; |
| 157 | displayName = "Stage Channel"; |
| 158 | let oldTopic = oc.topic, |
| 159 | newTopic = nc.topic; |
| 160 | if (oldTopic) { |
| 161 | if (oldTopic.length > 256) |
| 162 | oldTopic = `\`\`\`\n${ |
| 163 | oldTopic.replace("`", "'").substring(0, 253) + "..." |
| 164 | }\n\`\`\``; |
| 165 | else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``; |
| 166 | } else { |
| 167 | oldTopic = "None"; |
| 168 | } |
| 169 | if (newTopic) { |
| 170 | if (newTopic.length > 256) |
| 171 | newTopic = `\`\`\`\n${ |
| 172 | newTopic.replace("`", "'").substring(0, 253) + "..." |
| 173 | }\n\`\`\``; |
| 174 | else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``; |
| 175 | } else { |
| 176 | newTopic = "None"; |
| 177 | } |
| 178 | if (oc.bitrate !== nc.bitrate) |
| 179 | changes.bitrate = entry( |
| 180 | [oc.bitrate, nc.bitrate], |
| 181 | `${oc.bitrate} -> ${nc.bitrate}` |
| 182 | ); |
| 183 | if (oc.userLimit !== nc.userLimit) |
| 184 | changes.maxUsers = entry( |
| 185 | [oc.userLimit, nc.userLimit], |
| 186 | `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${ |
| 187 | nc.userLimit |
| 188 | }` |
| 189 | ); |
| 190 | if (oc.rtcRegion !== nc.rtcRegion) |
| 191 | changes.region = entry( |
| 192 | [oc.rtcRegion, nc.rtcRegion], |
| 193 | `${oc.rtcRegion || "Automatic"} -> ${ |
| 194 | nc.rtcRegion || "Automatic" |
| 195 | }` |
| 196 | ); |
| 197 | break; |
| 198 | } |
| 199 | case "GUILD_CATEGORY": { |
| 200 | emoji = "CHANNEL.CATEGORY.EDIT"; |
| 201 | readableType = "Category"; |
| 202 | displayName = "Category"; |
| 203 | break; |
| 204 | } |
| 205 | default: { |
| 206 | emoji = "CHANNEL.TEXT.EDIT"; |
| 207 | readableType = "Channel"; |
| 208 | displayName = "Channel"; |
| 209 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 210 | } |
| 211 | const t = oc.type.split("_")[1]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 212 | if (oc.type !== nc.type) |
| 213 | changes.type = entry( |
| 214 | [oc.type, nc.type], |
| 215 | `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}` |
| 216 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 217 | if (!(Object.values(changes).length - 4)) return; |
| 218 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 219 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 220 | type: "channelUpdate", |
| 221 | displayName: displayName + " Edited", |
| 222 | calculateType: "channelUpdate", |
| 223 | color: NucleusColors.yellow, |
| 224 | emoji: emoji, |
| 225 | timestamp: audit.createdTimestamp |
| 226 | }, |
| 227 | list: changes, |
| 228 | hidden: { |
| 229 | guild: nc.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 230 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 231 | }; |
| 232 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 233 | } |