blob: a7af4539cb458f410a940dd5a585d4e70e35f726 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import humanizeDuration from "humanize-duration";
2import getEmojiByName from "../utils/getEmojiByName.js";
pineafan32767212022-03-14 21:27:39 +00003
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +00005
6export async function callback(client, oc, nc) {
pineafan63fc5e22022-08-04 22:04:10 +01007 const config = await client.memory.readGuildInfo(nc.guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +01008 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger;
pineafan32767212022-03-14 21:27:39 +00009
Skyler Grey75ea9172022-08-06 10:22:23 +010010 if (nc.parent && nc.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000011
pineafan63fc5e22022-08-04 22:04:10 +010012 const auditLog = await getAuditLog(nc.guild, "CHANNEL_UPDATE");
Skyler Grey11236ba2022-08-08 21:13:33 +010013 const audit = auditLog.entries.filter((entry) => entry.target.id === nc.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010014 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000015
Skyler Grey75ea9172022-08-06 10:22:23 +010016 let emoji: string;
17 let readableType: string;
18 let displayName: string;
pineafan63fc5e22022-08-04 22:04:10 +010019 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 Grey11236ba2022-08-08 21:13:33 +010023 editedBy: entry(audit.executor.id, renderUser((await nc.guild.members.fetch(audit.executor.id)).user))
pineafan63fc5e22022-08-04 22:04:10 +010024 };
Skyler Grey11236ba2022-08-08 21:13:33 +010025 if (oc.name !== nc.name) changes.name = entry([oc.name, nc.name], `${oc.name} -> ${nc.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010026 if (oc.position !== nc.position)
Skyler Grey11236ba2022-08-08 21:13:33 +010027 changes.position = entry([oc.position, nc.position], `${oc.position} -> ${nc.position}`);
pineafan63fc5e22022-08-04 22:04:10 +010028
29 switch (nc.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010030 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 Grey11236ba2022-08-08 21:13:33 +010038 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010039 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
40 } else {
41 oldTopic = "None";
42 }
43 if (newTopic) {
44 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010045 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010046 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
47 } else {
48 newTopic = "None";
49 }
50 const nsfw = ["", ""];
Skyler Grey11236ba2022-08-08 21:13:33 +010051 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 Grey75ea9172022-08-06 10:22:23 +010053 if (oc.topic !== nc.topic)
Skyler Grey11236ba2022-08-08 21:13:33 +010054 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 Grey75ea9172022-08-06 10:22:23 +010056 if (oc.rateLimitPerUser !== nc.rateLimitPerUser)
57 changes.rateLimitPerUser = entry(
58 [oc.rateLimitPerUser, nc.rateLimitPerUser],
Skyler Grey11236ba2022-08-08 21:13:33 +010059 `${humanizeDuration(oc.rateLimitPerUser * 1000)} -> ${humanizeDuration(nc.rateLimitPerUser * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010060 );
pineafan63fc5e22022-08-04 22:04:10 +010061
Skyler Grey75ea9172022-08-06 10:22:23 +010062 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 Grey11236ba2022-08-08 21:13:33 +010072 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010073 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
74 } else {
75 oldTopic = "None";
76 }
77 if (newTopic) {
78 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010079 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010080 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
81 } else {
82 newTopic = "None";
83 }
84 if (oc.nsfw !== nc.nsfw)
Skyler Grey11236ba2022-08-08 21:13:33 +010085 changes.nsfw = entry([oc.nsfw, nc.nsfw], `${oc.nsfw ? "On" : "Off"} -> ${nc.nsfw ? "On" : "Off"}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010086 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 Grey11236ba2022-08-08 21:13:33 +010093 changes.bitrate = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010094 if (oc.userLimit !== nc.userLimit)
95 changes.maxUsers = entry(
96 [oc.userLimit, nc.userLimit],
Skyler Grey11236ba2022-08-08 21:13:33 +010097 `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +010098 );
99 if (oc.rtcRegion !== nc.rtcRegion)
100 changes.region = entry(
101 [oc.rtcRegion, nc.rtcRegion],
Skyler Grey11236ba2022-08-08 21:13:33 +0100102 `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 );
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 Grey11236ba2022-08-08 21:13:33 +0100114 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
116 } else {
117 oldTopic = "None";
118 }
119 if (newTopic) {
120 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100121 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
123 } else {
124 newTopic = "None";
125 }
126 if (oc.bitrate !== nc.bitrate)
Skyler Grey11236ba2022-08-08 21:13:33 +0100127 changes.bitrate = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`);
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 if (oc.userLimit !== nc.userLimit)
129 changes.maxUsers = entry(
130 [oc.userLimit, nc.userLimit],
Skyler Grey11236ba2022-08-08 21:13:33 +0100131 `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 );
133 if (oc.rtcRegion !== nc.rtcRegion)
134 changes.region = entry(
135 [oc.rtcRegion, nc.rtcRegion],
Skyler Grey11236ba2022-08-08 21:13:33 +0100136 `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 );
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 }
pineafan63fc5e22022-08-04 22:04:10 +0100151 }
152 const t = oc.type.split("_")[1];
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 if (oc.type !== nc.type)
Skyler Grey11236ba2022-08-08 21:13:33 +0100154 changes.type = entry([oc.type, nc.type], `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100155 if (!(Object.values(changes).length - 4)) return;
156 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100158 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
pineafane625d782022-05-09 18:04:32 +0100168 }
pineafan63fc5e22022-08-04 22:04:10 +0100169 };
170 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100171}