blob: f374edd26c2bd00173767090d3cc2173caeb8ce4 [file] [log] [blame]
pineafan32767212022-03-14 21:27:39 +00001import humanizeDuration from 'humanize-duration';
2import readConfig from '../utils/readConfig.js'
3import getEmojiByName from '../utils/getEmojiByName.js';
4
5export const event = 'channelUpdate';
6
7export async function callback(client, oc, nc) {
8 let config = await readConfig(nc.guild.id);
9 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger
10
11 if (nc.parent && (nc.parent.id == config.tickets.category)) return
12
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;
16
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)),
pineafan377794f2022-04-18 19:01:01 +010023 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan32767212022-03-14 21:27:39 +000024 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}`);
28
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 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 let 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}`);
pineafan377794f2022-04-18 19:01:01 +0100117 if (!(Object.values(changes).length - 4)) return
pineafan32767212022-03-14 21:27:39 +0000118 let 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
130 }
131 }
132 log(data, client);
133}