blob: 34b24c6deb3b1e03377eed67e48f766838e27113 [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 }
24 renderNumberDelta(num1, num2) {
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 }
28 entry(value, displayValue) {
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
pineafanda6e5342022-07-03 10:03:16 +010048 async getAuditLog(guild: Discord.Guild, event): 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;
63 const description = {};
pineafane625d782022-05-09 18:04:32 +010064 Object.entries(log.list).map(entry => {
pineafan63fc5e22022-08-04 22:04:10 +010065 const key = entry[0];
66 // 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 {};