blob: 6717661f6dba5e785cffe6af57ea339cedb1f1d0 [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 Grey75ea9172022-08-06 10:22:23 +01008 const {
9 getAuditLog,
10 log,
11 NucleusColors,
12 entry,
13 renderDelta,
14 renderUser,
15 renderChannel
16 } = client.logger;
pineafan32767212022-03-14 21:27:39 +000017
Skyler Grey75ea9172022-08-06 10:22:23 +010018 if (nc.parent && nc.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000019
pineafan63fc5e22022-08-04 22:04:10 +010020 const auditLog = await getAuditLog(nc.guild, "CHANNEL_UPDATE");
Skyler Grey75ea9172022-08-06 10:22:23 +010021 const audit = auditLog.entries
22 .filter((entry) => entry.target.id === nc.id)
23 .first();
pineafan63fc5e22022-08-04 22:04:10 +010024 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000025
Skyler Grey75ea9172022-08-06 10:22:23 +010026 let emoji: string;
27 let readableType: string;
28 let displayName: string;
pineafan63fc5e22022-08-04 22:04:10 +010029 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 Grey75ea9172022-08-06 10:22:23 +010033 editedBy: entry(
34 audit.executor.id,
35 renderUser((await nc.guild.members.fetch(audit.executor.id)).user)
36 )
pineafan63fc5e22022-08-04 22:04:10 +010037 };
Skyler Grey75ea9172022-08-06 10:22:23 +010038 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 );
pineafan63fc5e22022-08-04 22:04:10 +010045
46 switch (nc.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010047 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 );
pineafan63fc5e22022-08-04 22:04:10 +010095
Skyler Grey75ea9172022-08-06 10:22:23 +010096 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 }
pineafan63fc5e22022-08-04 22:04:10 +0100210 }
211 const t = oc.type.split("_")[1];
Skyler Grey75ea9172022-08-06 10:22:23 +0100212 if (oc.type !== nc.type)
213 changes.type = entry(
214 [oc.type, nc.type],
215 `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`
216 );
pineafan63fc5e22022-08-04 22:04:10 +0100217 if (!(Object.values(changes).length - 4)) return;
218 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100219 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100220 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
pineafane625d782022-05-09 18:04:32 +0100230 }
pineafan63fc5e22022-08-04 22:04:10 +0100231 };
232 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100233}