blob: 5d6a8f6a31787e1f14dc4578d1b06a27a5dff7ec [file] [log] [blame]
PineaFane6ba7882023-01-18 20:41:16 +00001import { GuildChannel, AuditLogEvent } from 'discord.js';
pineafan63fc5e22022-08-04 22:04:10 +01002import humanizeDuration from "humanize-duration";
PineaFane6ba7882023-01-18 20:41:16 +00003import type { NucleusClient } from "../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +01004import getEmojiByName from "../utils/getEmojiByName.js";
pineafan32767212022-03-14 21:27:39 +00005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "channelUpdate";
pineafan32767212022-03-14 21:27:39 +00007
PineaFane6ba7882023-01-18 20:41:16 +00008export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
9 const config = await client.memory.readGuildInfo(newChannel.guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010010 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger;
pineafan32767212022-03-14 21:27:39 +000011
PineaFane6ba7882023-01-18 20:41:16 +000012 if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
pineafan32767212022-03-14 21:27:39 +000013
PineaFane6ba7882023-01-18 20:41:16 +000014 const auditLog = await getAuditLog(newChannel.guild, "CHANNEL_UPDATE");
15 const audit = auditLog.entries.filter((entry) => entry.target.id === newChannel.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010016 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000017
Skyler Grey75ea9172022-08-06 10:22:23 +010018 let emoji: string;
19 let readableType: string;
20 let displayName: string;
pineafan63fc5e22022-08-04 22:04:10 +010021 const changes = {
PineaFane6ba7882023-01-18 20:41:16 +000022 channelId: entry(newChannel.id, `\`${newChannel.id}\``),
23 channel: entry(newChannel.id, renderChannel(newChannel)),
pineafan63fc5e22022-08-04 22:04:10 +010024 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
PineaFane6ba7882023-01-18 20:41:16 +000025 editedBy: entry(audit.executor.id, renderUser((await newChannel.guild.members.fetch(audit.executor.id)).user))
pineafan63fc5e22022-08-04 22:04:10 +010026 };
PineaFane6ba7882023-01-18 20:41:16 +000027 if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
28 if (oldChannel.position !== newChannel.position)
TheCodedProf60a1f492023-01-18 16:59:20 -050029 changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`);
pineafan63fc5e22022-08-04 22:04:10 +010030
PineaFane6ba7882023-01-18 20:41:16 +000031 switch (newChannel.type) {
Skyler Grey75ea9172022-08-06 10:22:23 +010032 case "GUILD_TEXT": {
33 emoji = "CHANNEL.TEXT.EDIT";
34 readableType = "Text";
35 displayName = "Text Channel";
PineaFane6ba7882023-01-18 20:41:16 +000036 let oldTopic = oldChannel.topic,
37 newTopic = newChannel.topic;
Skyler Grey75ea9172022-08-06 10:22:23 +010038 if (oldTopic) {
39 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010040 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010041 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
42 } else {
43 oldTopic = "None";
44 }
45 if (newTopic) {
46 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010047 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010048 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
49 } else {
50 newTopic = "None";
51 }
52 const nsfw = ["", ""];
PineaFane6ba7882023-01-18 20:41:16 +000053 nsfw[0] = oldChannel.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
54 nsfw[1] = newChannel.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
55 if (oldChannel.topic !== newChannel.topic)
56 changes.description = entry([oldChannel.topic, newChannel.topic], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
57 if (oldChannel.nsfw !== newChannel.nsfw) changes.nsfw = entry([oldChannel.nsfw, newChannel.nsfw], `${nsfw[0]} -> ${nsfw[1]}`);
58 if (oldChannel.rateLimitPerUser !== newChannel.rateLimitPerUser)
Skyler Grey75ea9172022-08-06 10:22:23 +010059 changes.rateLimitPerUser = entry(
PineaFane6ba7882023-01-18 20:41:16 +000060 [oldChannel.rateLimitPerUser, newChannel.rateLimitPerUser],
61 `${humanizeDuration(oldChannel.rateLimitPerUser * 1000)} -> ${humanizeDuration(newChannel.rateLimitPerUser * 1000)}`
Skyler Grey75ea9172022-08-06 10:22:23 +010062 );
pineafan63fc5e22022-08-04 22:04:10 +010063
Skyler Grey75ea9172022-08-06 10:22:23 +010064 break;
65 }
66 case "GUILD_NEWS": {
67 emoji = "CHANNEL.TEXT.EDIT";
68 readableType = "News";
69 displayName = "News Channel";
PineaFane6ba7882023-01-18 20:41:16 +000070 let oldTopic = oldChannel.topic,
71 newTopic = newChannel.topic;
Skyler Grey75ea9172022-08-06 10:22:23 +010072 if (oldTopic) {
73 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010074 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010075 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
76 } else {
77 oldTopic = "None";
78 }
79 if (newTopic) {
80 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +010081 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +010082 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
83 } else {
84 newTopic = "None";
85 }
PineaFane6ba7882023-01-18 20:41:16 +000086 if (oldChannel.nsfw !== newChannel.nsfw)
87 changes.nsfw = entry([oldChannel.nsfw, newChannel.nsfw], `${oldChannel.nsfw ? "On" : "Off"} -> ${newChannel.nsfw ? "On" : "Off"}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010088 break;
89 }
90 case "GUILD_VOICE": {
91 emoji = "CHANNEL.VOICE.EDIT";
92 readableType = "Voice";
93 displayName = "Voice Channel";
PineaFane6ba7882023-01-18 20:41:16 +000094 if (oldChannel.bitrate !== newChannel.bitrate)
95 changes.bitrate = entry([oldChannel.bitrate, newChannel.bitrate], `${oldChannel.bitrate} -> ${newChannel.bitrate}`);
96 if (oldChannel.userLimit !== newChannel.userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +010097 changes.maxUsers = entry(
PineaFane6ba7882023-01-18 20:41:16 +000098 [oldChannel.userLimit, newChannel.userLimit],
99 `${oldChannel.userLimit ? oldChannel.userLimit : "Unlimited"} -> ${newChannel.userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 );
PineaFane6ba7882023-01-18 20:41:16 +0000101 if (oldChannel.rtcRegion !== newChannel.rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 changes.region = entry(
PineaFane6ba7882023-01-18 20:41:16 +0000103 [oldChannel.rtcRegion, newChannel.rtcRegion],
104 `${oldChannel.rtcRegion || "Automatic"} -> ${newChannel.rtcRegion || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 );
106 break;
107 }
108 case "GUILD_STAGE": {
109 emoji = "CHANNEL.VOICE.EDIT";
110 readableType = "Stage";
111 displayName = "Stage Channel";
PineaFane6ba7882023-01-18 20:41:16 +0000112 let oldTopic = oldChannel.topic,
113 newTopic = newChannel.topic;
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 if (oldTopic) {
115 if (oldTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100116 oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
118 } else {
119 oldTopic = "None";
120 }
121 if (newTopic) {
122 if (newTopic.length > 256)
Skyler Grey11236ba2022-08-08 21:13:33 +0100123 newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
125 } else {
126 newTopic = "None";
127 }
PineaFane6ba7882023-01-18 20:41:16 +0000128 if (oldChannel.bitrate !== newChannel.bitrate)
129 changes.bitrate = entry([oldChannel.bitrate, newChannel.bitrate], `${oldChannel.bitrate} -> ${newChannel.bitrate}`);
130 if (oldChannel.userLimit !== newChannel.userLimit)
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 changes.maxUsers = entry(
PineaFane6ba7882023-01-18 20:41:16 +0000132 [oldChannel.userLimit, newChannel.userLimit],
133 `${oldChannel.userLimit ? oldChannel.userLimit : "Unlimited"} -> ${newChannel.userLimit}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 );
PineaFane6ba7882023-01-18 20:41:16 +0000135 if (oldChannel.rtcRegion !== newChannel.rtcRegion)
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 changes.region = entry(
PineaFane6ba7882023-01-18 20:41:16 +0000137 [oldChannel.rtcRegion, newChannel.rtcRegion],
138 `${oldChannel.rtcRegion || "Automatic"} -> ${newChannel.rtcRegion || "Automatic"}`
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 );
140 break;
141 }
142 case "GUILD_CATEGORY": {
143 emoji = "CHANNEL.CATEGORY.EDIT";
144 readableType = "Category";
145 displayName = "Category";
146 break;
147 }
148 default: {
149 emoji = "CHANNEL.TEXT.EDIT";
150 readableType = "Channel";
151 displayName = "Channel";
152 }
pineafan63fc5e22022-08-04 22:04:10 +0100153 }
PineaFane6ba7882023-01-18 20:41:16 +0000154 const t = oldChannel.type.split("_")[1];
155 if (oldChannel.type !== newChannel.type)
156 changes.type = entry([oldChannel.type, newChannel.type], `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`);
pineafan63fc5e22022-08-04 22:04:10 +0100157 if (!(Object.values(changes).length - 4)) return;
158 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100160 type: "channelUpdate",
161 displayName: displayName + " Edited",
162 calculateType: "channelUpdate",
163 color: NucleusColors.yellow,
164 emoji: emoji,
165 timestamp: audit.createdTimestamp
166 },
167 list: changes,
168 hidden: {
PineaFane6ba7882023-01-18 20:41:16 +0000169 guild: newChannel.guild.id
pineafane625d782022-05-09 18:04:32 +0100170 }
pineafan63fc5e22022-08-04 22:04:10 +0100171 };
172 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100173}