blob: 052acc1b0f31a3c9139952c1d67879beb1d22844 [file] [log] [blame]
import { GuildChannel, AuditLogEvent, ChannelType, TextChannel, VoiceChannel, StageChannel } from 'discord.js';
import type { GuildAuditLogsEntry } from 'discord.js';
//@ts-expect-error
import humanizeDuration from "humanize-duration";
import type { NucleusClient } from "../utils/client.js";
import getEmojiByName from "../utils/getEmojiByName.js";
import c from "../utils/client.js";
import { capitalize } from "../utils/generateKeyValueList.js";
let entry = c.logger.entry;
const channelTypeEmoji: Record<number, string> = {
0: "Text", // Text channel
2: "Voice", // Voice channel
5: "Announcement", // Announcement channel
13: "Stage", // Stage channel
15: "Forum", // Forum channel
99: "Rules" // Rules channel
};
interface channelChanges {
channelId: ReturnType<typeof entry>;
channel: ReturnType<typeof entry>;
edited: ReturnType<typeof entry>;
editedBy: ReturnType<typeof entry>;
type?: ReturnType<typeof entry>;
name?: ReturnType<typeof entry>;
position?: ReturnType<typeof entry>;
description?: ReturnType<typeof entry>;
nsfw?: ReturnType<typeof entry>;
slowmode?: ReturnType<typeof entry>;
topic?: ReturnType<typeof entry>;
bitrate?: ReturnType<typeof entry>;
userLimit?: ReturnType<typeof entry>;
parent?: ReturnType<typeof entry>;
permissionOverwrites?: ReturnType<typeof entry>;
region?: ReturnType<typeof entry>;
maxUsers?: ReturnType<typeof entry>;
autoArchiveDuration?: ReturnType<typeof entry>;
}
export const event = "channelUpdate";
export async function callback(client: NucleusClient, oldChannel: GuildChannel, newChannel: GuildChannel) {
const { getAuditLog, log, isLogging, NucleusColors, renderDelta, renderUser, renderChannel } = client.logger;
if (!await isLogging(newChannel.guild.id, "channelUpdate")) return;
const config = await client.memory.readGuildInfo(newChannel.guild.id);
entry = client.logger.entry;
if (newChannel.parent && newChannel.parent.id === config.tickets.category) return;
const auditLog: null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> = (await getAuditLog(newChannel.guild, AuditLogEvent.ChannelUpdate))
.filter((entry: GuildAuditLogsEntry) => (entry.target as GuildChannel)!.id === newChannel.id)[0] as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate> | null;
if (!auditLog) return;
if (auditLog.executor!.id === client.user!.id) return;
let emoji: string;
let readableType: string;
let displayName: string;
const changes: channelChanges = {
channelId: entry(newChannel.id, `\`${newChannel.id}\``),
channel: entry(newChannel.id, renderChannel(newChannel)),
edited: entry(Date.now(), renderDelta(Date.now())),
editedBy: entry(auditLog.executor!.id, renderUser((await newChannel.guild.members.fetch(auditLog.executor!.id)).user)),
};
if (oldChannel.name !== newChannel.name) changes.name = entry([oldChannel.name, newChannel.name], `${oldChannel.name} -> ${newChannel.name}`);
if (oldChannel.position !== newChannel.position)
changes.position = entry([oldChannel.position.toString(), newChannel.position.toString()], `${oldChannel.position} -> ${newChannel.position}`);
switch (newChannel.type) {
case ChannelType.PrivateThread:
case ChannelType.PublicThread: {
return;
}
case ChannelType.GuildText: {
emoji = "CHANNEL.TEXT.EDIT";
readableType = "Text";
displayName = "Text Channel";
let oldTopic = (oldChannel as TextChannel).topic ?? "*None*",
newTopic = (oldChannel as TextChannel).topic ?? "*None*";
if (oldTopic) {
if (oldTopic.length > 256)
oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
} else {
oldTopic = "None";
}
if (newTopic) {
if (newTopic.length > 256)
newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
} else {
newTopic = "None";
}
const nsfw = ["", ""];
nsfw[0] = (oldChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
nsfw[1] = (newChannel as TextChannel).nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
if (oldTopic !== newTopic)
changes.description = entry([(oldChannel as TextChannel).topic ?? "", (newChannel as TextChannel).topic ?? ""], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) changes.nsfw = entry([(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"], `${nsfw[0]} -> ${nsfw[1]}`);
if ((oldChannel as TextChannel).rateLimitPerUser !== (newChannel as TextChannel).rateLimitPerUser)
changes.slowmode = entry(
[((oldChannel as TextChannel).rateLimitPerUser).toString(), ((newChannel as TextChannel).rateLimitPerUser).toString()],
`${humanizeDuration((oldChannel as TextChannel).rateLimitPerUser * 1000)} -> ${humanizeDuration((newChannel as TextChannel).rateLimitPerUser * 1000)}`
);
if((oldChannel as TextChannel).defaultAutoArchiveDuration !== (newChannel as TextChannel).defaultAutoArchiveDuration) {
changes.autoArchiveDuration = entry(
[((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(), ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()],
`${humanizeDuration(((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)} -> ${humanizeDuration(((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)}`
);
}
break;
}
case ChannelType.GuildAnnouncement: {
emoji = "CHANNEL.TEXT.EDIT";
readableType = "Announcement";
displayName = "Announcement Channel";
let oldTopic = (oldChannel as TextChannel).topic,
newTopic = (newChannel as TextChannel).topic;
if (oldTopic) {
if (oldTopic.length > 256)
oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
} else {
oldTopic = "None";
}
if (newTopic) {
if (newTopic.length > 256)
newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
} else {
newTopic = "None";
}
if ((oldChannel as TextChannel).nsfw !== (newChannel as TextChannel).nsfw) {
changes.nsfw = entry([(oldChannel as TextChannel).nsfw ? "On" : "Off", (newChannel as TextChannel).nsfw ? "On" : "Off"], `${(oldChannel as TextChannel).nsfw ? "On" : "Off"} -> ${(newChannel as TextChannel).nsfw ? "On" : "Off"}`);
}
if((oldChannel as TextChannel).defaultAutoArchiveDuration !== (newChannel as TextChannel).defaultAutoArchiveDuration) {
changes.autoArchiveDuration = entry(
[((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString(), ((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320).toString()],
`${humanizeDuration(((oldChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)} -> ${humanizeDuration(((newChannel as TextChannel).defaultAutoArchiveDuration ?? 4320) * 60 * 1000)}`
);
}
break;
}
case ChannelType.GuildVoice: {
emoji = "CHANNEL.VOICE.EDIT";
readableType = "Voice";
displayName = "Voice Channel";
if ((oldChannel as VoiceChannel).bitrate !== (newChannel as VoiceChannel).bitrate)
changes.bitrate = entry([(oldChannel as VoiceChannel).bitrate.toString(), (newChannel as VoiceChannel).bitrate.toString()], `${(oldChannel as VoiceChannel).bitrate} -> ${(newChannel as VoiceChannel).bitrate}`);
if ((oldChannel as VoiceChannel).userLimit !== (newChannel as VoiceChannel).userLimit)
changes.maxUsers = entry(
[(oldChannel as VoiceChannel).userLimit.toString(), (newChannel as VoiceChannel).userLimit.toString()],
`${(oldChannel as VoiceChannel).userLimit ? (oldChannel as VoiceChannel).userLimit : "Unlimited"} -> ${(newChannel as VoiceChannel).userLimit}`
);
if ((oldChannel as VoiceChannel).rtcRegion !== (newChannel as VoiceChannel).rtcRegion)
changes.region = entry(
[(oldChannel as VoiceChannel).rtcRegion ?? "automatic", (newChannel as VoiceChannel).rtcRegion ?? "automatic"],
`${capitalize((oldChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")} -> ${capitalize((newChannel as VoiceChannel).rtcRegion?.toUpperCase() ?? "automatic")}`
);
break;
}
case ChannelType.GuildStageVoice: {
emoji = "CHANNEL.VOICE.EDIT";
readableType = "Stage";
displayName = "Stage Channel";
let oldTopic = (oldChannel as StageChannel).topic,
newTopic = (newChannel as StageChannel).topic;
if (oldTopic) {
if (oldTopic.length > 256)
oldTopic = `\`\`\`\n${oldTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else oldTopic = `\`\`\`\n${oldTopic.replace("`", "'")}\n\`\`\``;
} else {
oldTopic = "None";
}
if (newTopic) {
if (newTopic.length > 256)
newTopic = `\`\`\`\n${newTopic.replace("`", "'").substring(0, 253) + "..."}\n\`\`\``;
else newTopic = `\`\`\`\n${newTopic.replace("`", "'")}\n\`\`\``;
} else {
newTopic = "None";
}
if ((oldChannel as StageChannel).bitrate !== (newChannel as StageChannel).bitrate)
changes.bitrate = entry([(oldChannel as StageChannel).bitrate.toString(), (newChannel as StageChannel).bitrate.toString()], `${(oldChannel as StageChannel).bitrate} -> ${(newChannel as StageChannel).bitrate}`);
if ((oldChannel as StageChannel).userLimit !== (newChannel as StageChannel).userLimit)
changes.maxUsers = entry(
[(oldChannel as StageChannel).userLimit.toString(), (newChannel as StageChannel).userLimit.toString()],
`${(oldChannel as StageChannel).userLimit ? (oldChannel as StageChannel).userLimit : "Unlimited"} -> ${(newChannel as StageChannel).userLimit}`
);
if ((oldChannel as StageChannel).rtcRegion !== (newChannel as StageChannel).rtcRegion)
changes.region = entry(
[(oldChannel as StageChannel).rtcRegion ?? "Automatic", (newChannel as StageChannel).rtcRegion ?? "Automatic"],
`${capitalize((oldChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")} -> ${capitalize((newChannel as StageChannel).rtcRegion?.toLowerCase() ?? "automatic")}`
);
break;
}
case ChannelType.GuildCategory: {
emoji = "CHANNEL.CATEGORY.EDIT";
readableType = "Category";
displayName = "Category";
break;
}
default: {
emoji = "CHANNEL.TEXT.EDIT";
readableType = "Channel";
displayName = "Channel";
}
}
const ocType = channelTypeEmoji[oldChannel.type],
ncType = channelTypeEmoji[newChannel.type];
if (oldChannel.type !== newChannel.type)
changes.type = entry([ocType!, ncType!], `${ocType!} -> ${readableType}`);
if (!(Object.values(changes).length - 4)) return;
const data = {
meta: {
type: "channelUpdate",
displayName: displayName + " Edited",
calculateType: "channelUpdate",
color: NucleusColors.yellow,
emoji: emoji,
timestamp: auditLog.createdTimestamp
},
list: changes,
hidden: {
guild: newChannel.guild.id
}
};
log(data);
}