blob: bb95edaf51e9ca82e6652f7ee79b5057f1767d8f [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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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;
23 },
24 separate?: {
25 start?: string;
26 end?: string;
27 }
28}
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;
34 if (!toHexArray(config.logging.logs.toLog).includes(type)) { return false; }
35 return true;
36}
PineaFan64486c42022-12-28 09:21:04 +000037
38export const Logger = {
pineafane625d782022-05-09 18:04:32 +010039 renderUser(user: Discord.User | string) {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050040 if (typeof user === "string") user = client.users.cache.get(user)!;
pineafane625d782022-05-09 18:04:32 +010041 return `${user.username} [<@${user.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000042 },
pineafane625d782022-05-09 18:04:32 +010043 renderTime(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010045 return `<t:${t}:D> at <t:${t}:T>`;
PineaFan64486c42022-12-28 09:21:04 +000046 },
pineafane625d782022-05-09 18:04:32 +010047 renderDelta(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010049 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
PineaFan64486c42022-12-28 09:21:04 +000050 },
pineafanbd02b4a2022-08-05 22:01:38 +010051 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010052 const delta = num2 - num1;
53 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
PineaFan64486c42022-12-28 09:21:04 +000054 },
TheCodedProfa16d1672023-01-18 18:58:34 -050055 entry(value: string | number | boolean | null | (string | boolean)[], displayValue: string): { value: string | boolean | null | (string | boolean | number)[]; displayValue: string } {
PineaFan0d06edc2023-01-17 22:10:31 +000056 if (typeof value === "number") value = value.toString();
pineafan63fc5e22022-08-04 22:04:10 +010057 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000058 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050059 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | string) {
60 if (typeof channel === "string") channel = client.channels.cache.get(channel) as Discord.GuildChannel | Discord.ThreadChannel;
pineafane625d782022-05-09 18:04:32 +010061 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000062 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050063 renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) {
64 if (typeof role === "string") role = (typeof guild === "string" ? client.guilds.cache.get(guild) : guild)!.roles.cache.get(role)!;
pineafane625d782022-05-09 18:04:32 +010065 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000066 },
pineafane625d782022-05-09 18:04:32 +010067 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010068 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000069 },
70 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 red: 0xf27878,
72 yellow: 0xf2d478,
73 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000074 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050075 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable, delay?: number): Promise<Discord.GuildAuditLogsEntry[]> {
76 await wait(delay ?? 250);
PineaFan538d3752023-01-12 21:48:23 +000077 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map(m => m)
78 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000079 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050080 async log(log: LoggerOptions): Promise<void> {
81 if (!await isLogging(log.hidden.guild, log.meta.calculateType)) return;
Samuel Shuert3621bf92023-03-04 13:55:40 -050082 console.log(log.hidden.guild)
pineafan63fc5e22022-08-04 22:04:10 +010083 const config = await client.database.guilds.read(log.hidden.guild);
Samuel Shuert3621bf92023-03-04 13:55:40 -050084 console.log(config.logging.logs.channel)
85
pineafane625d782022-05-09 18:04:32 +010086 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010087 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010088 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010089 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010090 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010091 // eslint-disable-next-line @typescript-eslint/no-explicit-any
92 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010093 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010094 description[key] = value.displayValue;
95 } else {
96 description[key] = value;
97 }
pineafan63fc5e22022-08-04 22:04:10 +010098 });
pineafane625d782022-05-09 18:04:32 +010099 if (channel) {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500100 log.separate = log.separate ?? {};
TheCodedProf21c08592022-09-13 14:14:43 -0400101 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +0100102 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +0100103 .setDescription(
104 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +0000105 generateKeyValueList(description) +
106 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +0100107 )
108 .setTimestamp(log.meta.timestamp)
109 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +0100111 }
112 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500113 },
114 isLogging
PineaFan64486c42022-12-28 09:21:04 +0000115};
116
pineafan63fc5e22022-08-04 22:04:10 +0100117export default {};