blob: 238b6f4374ee88b6fc0bfb92b4b82f0bdbb6efc4 [file] [log] [blame]
pineafan32767212022-03-14 21:27:39 +00001import * as fs from 'fs';
2import * as Discord from 'discord.js';
3import getEmojiByName from './getEmojiByName.js';
4import readConfig from './readConfig.js';
5import { toHexArray } from './calculate.js';
6import { promisify } from 'util';
7import generateKeyValueList from './generateKeyValueList.js';
8
9const wait = promisify(setTimeout);
10
11
12export class Logger {
pineafane625d782022-05-09 18:04:32 +010013 renderUser(user: Discord.User | string) {
14 if (typeof user == 'string') return `${user} [<@${user}>]`;
15 return `${user.username} [<@${user.id}>]`;
16 }
17 renderTime(t: number) {
18 t = Math.floor(t /= 1000)
19 return `<t:${t}:D> at <t:${t}:T>`;
20 }
21 renderDelta(t: number) {
22 t = Math.floor(t /= 1000)
23 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
24 }
25 renderNumberDelta(num1, num2) {
26 let delta = num2 - num1;
27 return `${num1} -> ${num2} (${delta > 0 ? '+' : ''}${delta})`;
28 }
29 entry(value, displayValue) {
30 return { value: value, displayValue: displayValue }
31 }
32 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | Discord.NewsChannel) {
33 return `${channel.name} [<#${channel.id}>]`;
34 }
35 renderRole(role: Discord.Role) {
36 return `${role.name} [<@&${role.id}>]`;
37 }
38 renderEmoji(emoji: Discord.GuildEmoji) {
39 return `<${emoji.animated ? 'a' : ''}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
40 }
pineafan32767212022-03-14 21:27:39 +000041
pineafane625d782022-05-09 18:04:32 +010042 public readonly NucleusColors = {
43 red: 0xF27878,
44 yellow: 0xF2D478,
45 green: 0x68D49E,
pineafan32767212022-03-14 21:27:39 +000046
pineafane625d782022-05-09 18:04:32 +010047 }
pineafan32767212022-03-14 21:27:39 +000048
pineafane625d782022-05-09 18:04:32 +010049 async getAuditLog(guild: Discord.Guild, event) {
50 await wait(250)
51 let auditLog = await guild.fetchAuditLogs({type: event});
52 return auditLog;
53 }
pineafan32767212022-03-14 21:27:39 +000054
pineafane625d782022-05-09 18:04:32 +010055 async log(log: any, client): Promise<void> {
56 let config = await readConfig(log.hidden.guild);
57 if (!config.logging.logs.enabled) return;
58 if (!(log.meta.calculateType == true)) {
59 if(!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) return console.log('Not logging this type of event');
60 }
61 if (config.logging.logs.channel) {
62 let channel = await client.channels.fetch(config.logging.logs.channel) as Discord.TextChannel;
63 let description = {};
64 Object.entries(log.list).map(entry => {
65 let key = entry[0];
66 let value:any = entry[1];
67 if(value.displayValue) {
68 description[key] = value.displayValue;
69 } else {
70 description[key] = value;
71 }
72 })
73 if (channel) {
74 log.separate = log.separate || {};
75 let embed = new Discord.MessageEmbed()
76 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
77 .setDescription(
78 (log.separate.start ? log.separate.start + "\n" : "") +
79 generateKeyValueList(description) +
80 (log.separate.end ? "\n" + log.separate.end : "")
81 )
82 .setTimestamp(log.meta.timestamp)
83 .setColor(log.meta.color);
84 channel.send({embeds: [embed]});
85 }
86 }
87 saveLog(log);
88 }
pineafan32767212022-03-14 21:27:39 +000089}
90
91
92export default {}
93
94async function saveLog(log: any): Promise<void> {
95}
96
97export function readLogs(guild: string) {
pineafane625d782022-05-09 18:04:32 +010098
pineafan32767212022-03-14 21:27:39 +000099}
100
101export function readSpecificLog(guild: string, id: number) {
pineafane625d782022-05-09 18:04:32 +0100102
pineafan32767212022-03-14 21:27:39 +0000103}