blob: af792bc106dec7e9a498a4b59febfb5fbe2ea110 [file] [log] [blame]
import { AuditLogEvent, GuildAuditLogsEntry, ThreadChannel } from "discord.js";
// @ts-expect-error
import humanizeDuration from "humanize-duration";
import type { NucleusClient } from "../utils/client.js";
export const event = "threadUpdate";
export async function callback(client: NucleusClient, oldThread: ThreadChannel, newThread: ThreadChannel) {
const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
if (!await isLogging(newThread.guild.id, "channelUpdate")) return;
const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate))
.filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0]!;
if (auditLog.executor!.id === client.user!.id) return;
const list: Record<string, ReturnType<typeof entry>> = {
threadId: entry(newThread.id, `\`${newThread.id}\``),
thread: entry(newThread.name, renderChannel(newThread)),
parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!))
};
if (oldThread.name !== newThread.name) {
list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`);
}
if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
list["autoArchiveDuration"] = entry(
[oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()],
`${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, {
round: true
})} -> ${humanizeDuration((newThread.autoArchiveDuration ?? 0) * 60 * 1000, {
round: true
})}`
);
}
if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
list["slowmode"] = entry(
[oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()],
`${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
(newThread.rateLimitPerUser ?? 0) * 1000
)}`
);
}
if (!(Object.keys(list).length - 3)) return;
list["updated"] = entry(Date.now(), renderDelta(Date.now()));
list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
const data = {
meta: {
type: "channelUpdate",
displayName: "Thread Edited",
calculateType: "channelUpdate",
color: NucleusColors.yellow,
emoji: "CHANNEL.TEXT.EDIT",
timestamp: Date.now()
},
list: list,
hidden: {
guild: newThread.guild.id
}
};
log(data);
}