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