blob: 7ab7903fa8b1ca9409eec56ec6a2a4a97105d833 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import * as Discord from "discord.js";
2import getEmojiByName from "./getEmojiByName.js";
3import { toHexArray } from "./calculate.js";
4import { promisify } from "util";
5import generateKeyValueList from "./generateKeyValueList.js";
6import client from "./client.js";
pineafan32767212022-03-14 21:27:39 +00007
8const wait = promisify(setTimeout);
9
PineaFan64486c42022-12-28 09:21:04 +000010
11export const Logger = {
pineafane625d782022-05-09 18:04:32 +010012 renderUser(user: Discord.User | string) {
pineafan63fc5e22022-08-04 22:04:10 +010013 if (typeof user === "string") return `${user} [<@${user}>]`;
pineafane625d782022-05-09 18:04:32 +010014 return `${user.username} [<@${user.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000015 },
pineafane625d782022-05-09 18:04:32 +010016 renderTime(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010017 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010018 return `<t:${t}:D> at <t:${t}:T>`;
PineaFan64486c42022-12-28 09:21:04 +000019 },
pineafane625d782022-05-09 18:04:32 +010020 renderDelta(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010022 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
PineaFan64486c42022-12-28 09:21:04 +000023 },
pineafanbd02b4a2022-08-05 22:01:38 +010024 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010025 const delta = num2 - num1;
26 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
PineaFan64486c42022-12-28 09:21:04 +000027 },
PineaFan538d3752023-01-12 21:48:23 +000028 entry(value: string | number | null, displayValue: string): { value: string | null; displayValue: string } {
pineafan63fc5e22022-08-04 22:04:10 +010029 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000030 },
pineafan41d93562022-07-30 22:10:15 +010031 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) {
pineafane625d782022-05-09 18:04:32 +010032 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000033 },
pineafane625d782022-05-09 18:04:32 +010034 renderRole(role: Discord.Role) {
35 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000036 },
pineafane625d782022-05-09 18:04:32 +010037 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010038 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000039 },
40 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 red: 0xf27878,
42 yellow: 0xf2d478,
43 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000044 },
45 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010046 await wait(250);
PineaFan538d3752023-01-12 21:48:23 +000047 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map(m => m)
48 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000049 },
pineafan63fc5e22022-08-04 22:04:10 +010050 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010051 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010052 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010053 if (!config.logging.logs.enabled) return;
PineaFan538d3752023-01-12 21:48:23 +000054 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) {
55 console.log("Not logging this type of event");
PineaFan64486c42022-12-28 09:21:04 +000056 return;
pineafane625d782022-05-09 18:04:32 +010057 }
58 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010059 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010060 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010061 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010062 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010063 // eslint-disable-next-line @typescript-eslint/no-explicit-any
64 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010065 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010066 description[key] = value.displayValue;
67 } else {
68 description[key] = value;
69 }
pineafan63fc5e22022-08-04 22:04:10 +010070 });
pineafane625d782022-05-09 18:04:32 +010071 if (channel) {
72 log.separate = log.separate || {};
TheCodedProf21c08592022-09-13 14:14:43 -040073 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +010074 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +010075 .setDescription(
76 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +000077 generateKeyValueList(description) +
78 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010079 )
80 .setTimestamp(log.meta.timestamp)
81 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010082 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +010083 }
84 }
pineafane625d782022-05-09 18:04:32 +010085 }
PineaFan64486c42022-12-28 09:21:04 +000086};
87
pineafan32767212022-03-14 21:27:39 +000088
pineafan63fc5e22022-08-04 22:04:10 +010089export default {};