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