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