pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 2 | import getEmojiByName from '../utils/getEmojiByName.js'; |
| 3 | |
| 4 | export const event = 'channelUpdate'; |
| 5 | |
| 6 | export async function callback(client, oc, nc) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 7 | try { |
| 8 | let config = await client.memory.readGuildInfo(nc.guild.id); |
| 9 | const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 10 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +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 | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | let auditLog = await getAuditLog(nc.guild, 'CHANNEL_UPDATE'); |
| 14 | let audit = auditLog.entries.filter(entry => entry.target.id == nc.id).first(); |
| 15 | if (audit.executor.id == client.user.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 16 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 17 | let emoji:string; |
| 18 | let readableType:string; |
| 19 | let displayName:string ; |
| 20 | let changes = { |
| 21 | id: entry(nc.id, `\`${nc.id}\``), |
| 22 | channel: entry(nc.id, renderChannel(nc)), |
| 23 | edited: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 24 | editedBy: entry(audit.executor.id, renderUser((await nc.guild.members.fetch(audit.executor.id)).user)), |
| 25 | } |
| 26 | if (oc.name != nc.name) changes["name"] = entry([oc.name, nc.name], `${oc.name} -> ${nc.name}`); |
| 27 | if (oc.position != nc.position) changes["position"] = entry([oc.position, nc.position], `${oc.position} -> ${nc.position}`); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 28 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 29 | switch (nc.type) { |
| 30 | case 'GUILD_TEXT': { |
| 31 | emoji = "CHANNEL.TEXT.EDIT"; |
| 32 | readableType = "Text"; |
| 33 | displayName = "Text Channel" |
| 34 | let oldTopic = oc.topic, newTopic = nc.topic; |
| 35 | if (oldTopic) { |
| 36 | if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 37 | else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\`` |
| 38 | } else { oldTopic = "None"; } |
| 39 | if (newTopic) { |
| 40 | if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 41 | else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\`` |
| 42 | } else { newTopic = "None"; } |
| 43 | let nsfw = ["", ""] |
| 44 | nsfw[0] = oc.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 45 | nsfw[1] = nc.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 46 | if (oc.topic != nc.topic) changes["description"] = entry([oc.topic, nc.topic], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`); |
| 47 | if (oc.nsfw != nc.nsfw) changes["nsfw"] = entry([oc.nsfw, nc.nsfw], `${nsfw[0]} -> ${nsfw[1]}`); |
| 48 | if (oc.rateLimitPerUser != nc.rateLimitPerUser) changes["rateLimitPerUser"] = entry( |
| 49 | [oc.rateLimitPerUser, nc.rateLimitPerUser], |
| 50 | `${humanizeDuration(oc.rateLimitPerUser * 1000)} -> ${humanizeDuration(nc.rateLimitPerUser * 1000)}` |
| 51 | ); |
| 52 | |
| 53 | break; |
| 54 | } |
| 55 | case 'GUILD_NEWS': { |
| 56 | emoji = "CHANNEL.TEXT.EDIT"; |
| 57 | readableType = "News"; |
| 58 | displayName = "News Channel" |
| 59 | let oldTopic = oc.topic, newTopic = nc.topic; |
| 60 | if (oldTopic) { |
| 61 | if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 62 | else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\`` |
| 63 | } else { oldTopic = "None"; } |
| 64 | if (newTopic) { |
| 65 | if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 66 | else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\`` |
| 67 | } else { newTopic = "None"; } |
| 68 | if (oc.nsfw != nc.nsfw) changes["nsfw"] = entry([oc.nsfw, nc.nsfw], `${oc.nsfw ? "On" : "Off"} -> ${nc.nsfw ? "On" : "Off"}`); |
| 69 | break; |
| 70 | } |
| 71 | case 'GUILD_VOICE': { |
| 72 | emoji = "CHANNEL.VOICE.EDIT"; |
| 73 | readableType = "Voice"; |
| 74 | displayName = "Voice Channel" |
| 75 | if (oc.bitrate != nc.bitrate) changes["bitrate"] = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`); |
| 76 | if (oc.userLimit != nc.userLimit) changes["maxUsers"] = entry([oc.userLimit, nc.userLimit], `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`); |
| 77 | if (oc.rtcRegion != nc.rtcRegion) changes["region"] = entry( |
| 78 | [oc.rtcRegion, nc.rtcRegion], |
| 79 | `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}` |
| 80 | ); |
| 81 | break; |
| 82 | } |
| 83 | case 'GUILD_STAGE': { |
| 84 | emoji = "CHANNEL.VOICE.EDIT"; |
| 85 | readableType = "Stage"; |
| 86 | displayName = "Stage Channel" |
| 87 | let oldTopic = oc.topic, newTopic = nc.topic; |
| 88 | if (oldTopic) { |
| 89 | if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 90 | else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\`` |
| 91 | } else { oldTopic = "None"; } |
| 92 | if (newTopic) { |
| 93 | if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\`` |
| 94 | else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\`` |
| 95 | } else { newTopic = "None"; } |
| 96 | if (oc.bitrate != nc.bitrate) changes["bitrate"] = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`); |
| 97 | if (oc.userLimit != nc.userLimit) changes["maxUsers"] = entry([oc.userLimit, nc.userLimit], `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`); |
| 98 | if (oc.rtcRegion != nc.rtcRegion) changes["region"] = entry( |
| 99 | [oc.rtcRegion, nc.rtcRegion], |
| 100 | `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}` |
| 101 | ); |
| 102 | break; |
| 103 | } |
| 104 | case 'GUILD_CATEGORY': { |
| 105 | emoji = "CHANNEL.CATEGORY.EDIT"; |
| 106 | readableType = "Category"; |
| 107 | displayName = "Category" |
| 108 | break; |
| 109 | } |
| 110 | default: { |
| 111 | emoji = "CHANNEL.TEXT.EDIT"; |
| 112 | readableType = "Channel"; |
| 113 | displayName = "Channel" |
| 114 | } |
| 115 | } |
| 116 | let t = oc.type.split("_")[1]; |
| 117 | if (oc.type != nc.type) changes["type"] = entry([oc.type, nc.type], `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`); |
| 118 | if (!(Object.values(changes).length - 4)) return |
| 119 | let data = { |
| 120 | meta:{ |
| 121 | type: 'channelUpdate', |
| 122 | displayName: displayName + ' Edited', |
| 123 | calculateType: 'channelUpdate', |
| 124 | color: NucleusColors.yellow, |
| 125 | emoji: emoji, |
| 126 | timestamp: audit.createdTimestamp |
| 127 | }, |
| 128 | list: changes, |
| 129 | hidden: { |
| 130 | guild: nc.guild.id |
| 131 | } |
| 132 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame^] | 133 | log(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 134 | } catch {} |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 135 | } |