blob: 2d151920cb8b7a68e3956eb2b949ca96d08efbd6 [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;
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) {
PineaFanb0d0c242023-02-05 10:59:45 +000040 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 },
TheCodedProf4a6d5712023-01-19 15:54:40 -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 },
TheCodedProf486bca32023-02-02 16:49:44 -050063 renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) {
PineaFanb0d0c242023-02-05 10:59:45 +000064 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 },
75 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010076 await wait(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 },
TheCodedProf6ec331b2023-02-20 12:13:06 -050080 async log(log: LoggerOptions): Promise<void> {
81 if (!await isLogging(log.hidden.guild, log.meta.calculateType)) return;
pineafan63fc5e22022-08-04 22:04:10 +010082 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010083 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010084 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010085 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010086 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010087 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010088 // eslint-disable-next-line @typescript-eslint/no-explicit-any
89 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010090 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010091 description[key] = value.displayValue;
92 } else {
93 description[key] = value;
94 }
pineafan63fc5e22022-08-04 22:04:10 +010095 });
pineafane625d782022-05-09 18:04:32 +010096 if (channel) {
97 log.separate = log.separate || {};
TheCodedProf21c08592022-09-13 14:14:43 -040098 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +010099 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +0100100 .setDescription(
101 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +0000102 generateKeyValueList(description) +
103 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +0100104 )
105 .setTimestamp(log.meta.timestamp)
106 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +0100108 }
109 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500110 },
111 isLogging
PineaFan64486c42022-12-28 09:21:04 +0000112};
113
pineafan63fc5e22022-08-04 22:04:10 +0100114export default {};