blob: 06018634b6c9dbd747d76428bfadc3e949950fb5 [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
TheCodedProf6ec331b2023-02-20 12:13:06 -050010export interface LoggerOptions {
11 meta: {
12 type: string;
13 displayName: string;
14 calculateType: string;
15 color: number;
16 emoji: string;
17 timestamp: number;
18 };
19 // eslint-disable-next-line @typescript-eslint/no-explicit-any
20 list: any;
21 hidden: {
22 guild: string;
Skyler Greyda16adf2023-03-05 10:22:12 +000023 };
TheCodedProf6ec331b2023-02-20 12:13:06 -050024 separate?: {
25 start?: string;
26 end?: string;
Skyler Greyda16adf2023-03-05 10:22:12 +000027 };
TheCodedProf6ec331b2023-02-20 12:13:06 -050028}
29
30async function isLogging(guild: string, type: string): Promise<boolean> {
31 const config = await client.database.guilds.read(guild);
32 if (!config.logging.logs.enabled) return false;
33 if (!config.logging.logs.channel) return false;
Skyler Greyda16adf2023-03-05 10:22:12 +000034 if (!toHexArray(config.logging.logs.toLog).includes(type)) {
35 return false;
36 }
TheCodedProf6ec331b2023-02-20 12:13:06 -050037 return true;
38}
PineaFan64486c42022-12-28 09:21:04 +000039
40export const Logger = {
pineafane625d782022-05-09 18:04:32 +010041 renderUser(user: Discord.User | string) {
PineaFanb0d0c242023-02-05 10:59:45 +000042 if (typeof user === "string") user = client.users.cache.get(user)!;
pineafane625d782022-05-09 18:04:32 +010043 return `${user.username} [<@${user.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000044 },
pineafane625d782022-05-09 18:04:32 +010045 renderTime(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010046 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010047 return `<t:${t}:D> at <t:${t}:T>`;
PineaFan64486c42022-12-28 09:21:04 +000048 },
pineafane625d782022-05-09 18:04:32 +010049 renderDelta(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010050 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010051 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
PineaFan64486c42022-12-28 09:21:04 +000052 },
pineafanbd02b4a2022-08-05 22:01:38 +010053 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010054 const delta = num2 - num1;
55 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
PineaFan64486c42022-12-28 09:21:04 +000056 },
Skyler Greyda16adf2023-03-05 10:22:12 +000057 entry(
58 value: string | number | boolean | null | (string | boolean)[],
59 displayValue: string
60 ): { value: string | boolean | null | (string | boolean | number)[]; displayValue: string } {
PineaFan0d06edc2023-01-17 22:10:31 +000061 if (typeof value === "number") value = value.toString();
pineafan63fc5e22022-08-04 22:04:10 +010062 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000063 },
TheCodedProf4a6d5712023-01-19 15:54:40 -050064 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | string) {
Skyler Greyda16adf2023-03-05 10:22:12 +000065 if (typeof channel === "string")
66 channel = client.channels.cache.get(channel) as Discord.GuildChannel | Discord.ThreadChannel;
pineafane625d782022-05-09 18:04:32 +010067 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000068 },
TheCodedProf486bca32023-02-02 16:49:44 -050069 renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) {
Skyler Greyda16adf2023-03-05 10:22:12 +000070 if (typeof role === "string")
71 role = (typeof guild === "string" ? client.guilds.cache.get(guild) : guild)!.roles.cache.get(role)!;
pineafane625d782022-05-09 18:04:32 +010072 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000073 },
pineafane625d782022-05-09 18:04:32 +010074 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010075 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000076 },
77 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 red: 0xf27878,
79 yellow: 0xf2d478,
80 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000081 },
Skyler Greyda16adf2023-03-05 10:22:12 +000082 async getAuditLog(
83 guild: Discord.Guild,
84 event: Discord.GuildAuditLogsResolvable,
85 delay?: number
86 ): Promise<Discord.GuildAuditLogsEntry[]> {
TheCodedProf686829f2023-02-22 15:08:01 -050087 await wait(delay ?? 250);
Skyler Greyda16adf2023-03-05 10:22:12 +000088 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map((m) => m);
PineaFan538d3752023-01-12 21:48:23 +000089 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000090 },
TheCodedProf6ec331b2023-02-20 12:13:06 -050091 async log(log: LoggerOptions): Promise<void> {
Skyler Greyda16adf2023-03-05 10:22:12 +000092 if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return;
93 console.log(log.hidden.guild);
pineafan63fc5e22022-08-04 22:04:10 +010094 const config = await client.database.guilds.read(log.hidden.guild);
Skyler Greyda16adf2023-03-05 10:22:12 +000095 console.log(config.logging.logs.channel);
Samuel Shuerta1511f92023-03-04 13:55:33 -050096
pineafane625d782022-05-09 18:04:32 +010097 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010098 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010099 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +0100101 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +0100102 // eslint-disable-next-line @typescript-eslint/no-explicit-any
103 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +0100105 description[key] = value.displayValue;
106 } else {
107 description[key] = value;
108 }
pineafan63fc5e22022-08-04 22:04:10 +0100109 });
pineafane625d782022-05-09 18:04:32 +0100110 if (channel) {
TheCodedProf1807fb32023-02-20 14:33:48 -0500111 log.separate = log.separate ?? {};
TheCodedProf21c08592022-09-13 14:14:43 -0400112 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +0100114 .setDescription(
115 (log.separate.start ? log.separate.start + "\n" : "") +
Skyler Greyda16adf2023-03-05 10:22:12 +0000116 generateKeyValueList(description) +
117 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +0100118 )
119 .setTimestamp(log.meta.timestamp)
120 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +0100122 }
123 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500124 },
125 isLogging
PineaFan64486c42022-12-28 09:21:04 +0000126};
127
pineafan63fc5e22022-08-04 22:04:10 +0100128export default {};