blob: fdf17ef5d95d7cd61503808c0b203883a763cc47 [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
pineafan32767212022-03-14 21:27:39 +000010export class Logger {
pineafane625d782022-05-09 18:04:32 +010011 renderUser(user: Discord.User | string) {
pineafan63fc5e22022-08-04 22:04:10 +010012 if (typeof user === "string") return `${user} [<@${user}>]`;
pineafane625d782022-05-09 18:04:32 +010013 return `${user.username} [<@${user.id}>]`;
14 }
15 renderTime(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010016 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010017 return `<t:${t}:D> at <t:${t}:T>`;
18 }
19 renderDelta(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010020 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010021 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
22 }
pineafanbd02b4a2022-08-05 22:01:38 +010023 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010024 const delta = num2 - num1;
25 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
pineafane625d782022-05-09 18:04:32 +010026 }
pineafanbd02b4a2022-08-05 22:01:38 +010027 entry(value: string, displayValue: string) {
pineafan63fc5e22022-08-04 22:04:10 +010028 return { value: value, displayValue: displayValue };
pineafane625d782022-05-09 18:04:32 +010029 }
pineafan41d93562022-07-30 22:10:15 +010030 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) {
pineafane625d782022-05-09 18:04:32 +010031 return `${channel.name} [<#${channel.id}>]`;
32 }
33 renderRole(role: Discord.Role) {
34 return `${role.name} [<@&${role.id}>]`;
35 }
36 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010037 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
pineafane625d782022-05-09 18:04:32 +010038 }
pineafan32767212022-03-14 21:27:39 +000039
pineafane625d782022-05-09 18:04:32 +010040 public readonly NucleusColors = {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 red: 0xf27878,
42 yellow: 0xf2d478,
43 green: 0x68d49e
pineafan63fc5e22022-08-04 22:04:10 +010044 };
pineafan32767212022-03-14 21:27:39 +000045
Skyler Grey75ea9172022-08-06 10:22:23 +010046 async getAuditLog(
47 guild: Discord.Guild,
48 event: Discord.GuildAuditLogsResolvable
49 ): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010050 await wait(250);
Skyler Grey75ea9172022-08-06 10:22:23 +010051 const auditLog = await guild.fetchAuditLogs({ type: event });
pineafanda6e5342022-07-03 10:03:16 +010052 return auditLog as unknown as Discord.GuildAuditLogsEntry[];
pineafane625d782022-05-09 18:04:32 +010053 }
pineafan32767212022-03-14 21:27:39 +000054
pineafan63fc5e22022-08-04 22:04:10 +010055 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010056 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010057 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010058 if (!config.logging.logs.enabled) return;
pineafane23c4ec2022-07-27 21:56:27 +010059 if (!(log.meta.calculateType === true)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010060 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType))
Skyler Grey75ea9172022-08-06 10:22:23 +010061 return console.log("Not logging this type of event");
pineafane625d782022-05-09 18:04:32 +010062 }
63 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010064 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010065 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010066 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010067 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010068 // eslint-disable-next-line @typescript-eslint/no-explicit-any
69 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010070 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010071 description[key] = value.displayValue;
72 } else {
73 description[key] = value;
74 }
pineafan63fc5e22022-08-04 22:04:10 +010075 });
pineafane625d782022-05-09 18:04:32 +010076 if (channel) {
77 log.separate = log.separate || {};
pineafan63fc5e22022-08-04 22:04:10 +010078 const embed = new Discord.MessageEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +010079 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +010080 .setDescription(
81 (log.separate.start ? log.separate.start + "\n" : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +010082 generateKeyValueList(description) +
83 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010084 )
85 .setTimestamp(log.meta.timestamp)
86 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010087 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +010088 }
89 }
pineafane625d782022-05-09 18:04:32 +010090 }
pineafan32767212022-03-14 21:27:39 +000091}
92
pineafan63fc5e22022-08-04 22:04:10 +010093export default {};