blob: f9f751e5421501d62babe1aae22b3f9ff12816f5 [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
10
11export class 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}>]`;
15 }
16 renderTime(t: number) {
pineafan63fc5e22022-08-04 22:04:10 +010017 t = Math.floor(t /= 1000);
pineafane625d782022-05-09 18:04:32 +010018 return `<t:${t}:D> at <t:${t}:T>`;
19 }
20 renderDelta(t: number) {
pineafan63fc5e22022-08-04 22:04:10 +010021 t = Math.floor(t /= 1000);
pineafane625d782022-05-09 18:04:32 +010022 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
23 }
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})`;
pineafane625d782022-05-09 18:04:32 +010027 }
pineafanbd02b4a2022-08-05 22:01:38 +010028 entry(value: string, displayValue: string) {
pineafan63fc5e22022-08-04 22:04:10 +010029 return { value: value, displayValue: displayValue };
pineafane625d782022-05-09 18:04:32 +010030 }
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}>]`;
33 }
34 renderRole(role: Discord.Role) {
35 return `${role.name} [<@&${role.id}>]`;
36 }
37 renderEmoji(emoji: Discord.GuildEmoji) {
pineafan63fc5e22022-08-04 22:04:10 +010038 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
pineafane625d782022-05-09 18:04:32 +010039 }
pineafan32767212022-03-14 21:27:39 +000040
pineafane625d782022-05-09 18:04:32 +010041 public readonly NucleusColors = {
42 red: 0xF27878,
43 yellow: 0xF2D478,
pineafan63fc5e22022-08-04 22:04:10 +010044 green: 0x68D49E
pineafan32767212022-03-14 21:27:39 +000045
pineafan63fc5e22022-08-04 22:04:10 +010046 };
pineafan32767212022-03-14 21:27:39 +000047
pineafanbd02b4a2022-08-05 22:01:38 +010048 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]>{
pineafan63fc5e22022-08-04 22:04:10 +010049 await wait(250);
50 const auditLog = await guild.fetchAuditLogs({type: event});
pineafanda6e5342022-07-03 10:03:16 +010051 return auditLog as unknown as Discord.GuildAuditLogsEntry[];
pineafane625d782022-05-09 18:04:32 +010052 }
pineafan32767212022-03-14 21:27:39 +000053
pineafan63fc5e22022-08-04 22:04:10 +010054 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010055 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010056 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010057 if (!config.logging.logs.enabled) return;
pineafane23c4ec2022-07-27 21:56:27 +010058 if (!(log.meta.calculateType === true)) {
pineafan63fc5e22022-08-04 22:04:10 +010059 if(!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) return console.log("Not logging this type of event");
pineafane625d782022-05-09 18:04:32 +010060 }
61 if (config.logging.logs.channel) {
pineafan63fc5e22022-08-04 22:04:10 +010062 const channel = await client.channels.fetch(config.logging.logs.channel) as Discord.TextChannel;
pineafanbd02b4a2022-08-05 22:01:38 +010063 const description: Record<string, string> = {};
pineafane625d782022-05-09 18:04:32 +010064 Object.entries(log.list).map(entry => {
pineafanbd02b4a2022-08-05 22:01:38 +010065 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010066 // eslint-disable-next-line @typescript-eslint/no-explicit-any
67 const value: any = entry[1];
pineafane625d782022-05-09 18:04:32 +010068 if(value.displayValue) {
69 description[key] = value.displayValue;
70 } else {
71 description[key] = value;
72 }
pineafan63fc5e22022-08-04 22:04:10 +010073 });
pineafane625d782022-05-09 18:04:32 +010074 if (channel) {
75 log.separate = log.separate || {};
pineafan63fc5e22022-08-04 22:04:10 +010076 const embed = new Discord.MessageEmbed()
pineafane625d782022-05-09 18:04:32 +010077 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
78 .setDescription(
79 (log.separate.start ? log.separate.start + "\n" : "") +
80 generateKeyValueList(description) +
81 (log.separate.end ? "\n" + log.separate.end : "")
82 )
83 .setTimestamp(log.meta.timestamp)
84 .setColor(log.meta.color);
85 channel.send({embeds: [embed]});
86 }
87 }
pineafane625d782022-05-09 18:04:32 +010088 }
pineafan32767212022-03-14 21:27:39 +000089}
90
91
pineafan63fc5e22022-08-04 22:04:10 +010092export default {};