blob: 6baa0a5abcfb4a17bf51f60e7f7be9e75592f61c [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 Grey75ea9172022-08-06 10:22:23 +010037 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${
38 emoji.name
39 }:\`]`;
pineafane625d782022-05-09 18:04:32 +010040 }
pineafan32767212022-03-14 21:27:39 +000041
pineafane625d782022-05-09 18:04:32 +010042 public readonly NucleusColors = {
Skyler Grey75ea9172022-08-06 10:22:23 +010043 red: 0xf27878,
44 yellow: 0xf2d478,
45 green: 0x68d49e
pineafan63fc5e22022-08-04 22:04:10 +010046 };
pineafan32767212022-03-14 21:27:39 +000047
Skyler Grey75ea9172022-08-06 10:22:23 +010048 async getAuditLog(
49 guild: Discord.Guild,
50 event: Discord.GuildAuditLogsResolvable
51 ): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010052 await wait(250);
Skyler Grey75ea9172022-08-06 10:22:23 +010053 const auditLog = await guild.fetchAuditLogs({ type: event });
pineafanda6e5342022-07-03 10:03:16 +010054 return auditLog as unknown as Discord.GuildAuditLogsEntry[];
pineafane625d782022-05-09 18:04:32 +010055 }
pineafan32767212022-03-14 21:27:39 +000056
pineafan63fc5e22022-08-04 22:04:10 +010057 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010058 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010059 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010060 if (!config.logging.logs.enabled) return;
pineafane23c4ec2022-07-27 21:56:27 +010061 if (!(log.meta.calculateType === true)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010062 if (
63 !toHexArray(config.logging.logs.toLog).includes(
64 log.meta.calculateType
65 )
66 )
67 return console.log("Not logging this type of event");
pineafane625d782022-05-09 18:04:32 +010068 }
69 if (config.logging.logs.channel) {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 const channel = (await client.channels.fetch(
71 config.logging.logs.channel
72 )) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010073 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010074 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010075 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010076 // eslint-disable-next-line @typescript-eslint/no-explicit-any
77 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010078 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010079 description[key] = value.displayValue;
80 } else {
81 description[key] = value;
82 }
pineafan63fc5e22022-08-04 22:04:10 +010083 });
pineafane625d782022-05-09 18:04:32 +010084 if (channel) {
85 log.separate = log.separate || {};
pineafan63fc5e22022-08-04 22:04:10 +010086 const embed = new Discord.MessageEmbed()
Skyler Grey75ea9172022-08-06 10:22:23 +010087 .setTitle(
88 `${getEmojiByName(log.meta.emoji)} ${
89 log.meta.displayName
90 }`
91 )
pineafane625d782022-05-09 18:04:32 +010092 .setDescription(
93 (log.separate.start ? log.separate.start + "\n" : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +010094 generateKeyValueList(description) +
95 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010096 )
97 .setTimestamp(log.meta.timestamp)
98 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010099 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +0100100 }
101 }
pineafane625d782022-05-09 18:04:32 +0100102 }
pineafan32767212022-03-14 21:27:39 +0000103}
104
pineafan63fc5e22022-08-04 22:04:10 +0100105export default {};