blob: 14e97508d66b2e55a74a1d1dd22ec19c4a85a200 [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 {
pineafan377794f2022-04-18 19:01:01 +010013 renderUser(user: Discord.User | string) {
14 if (typeof user == 'string') return `${user} [<@${user}>]`;
pineafan32767212022-03-14 21:27:39 +000015 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) {
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 }
41
42 public readonly NucleusColors = {
43 red: 0xF27878,
44 yellow: 0xF2D478,
45 green: 0x68D49E,
46
47 }
48
pineafan32767212022-03-14 21:27:39 +000049 async getAuditLog(guild: Discord.Guild, event) {
50 await wait(250)
51 let auditLog = await guild.fetchAuditLogs({type: event});
52 return auditLog;
53 }
54
55 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)) if(!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) return console.log('Not logging this type of event');
59 if (config.logging.logs.channel) {
60 let channel = await client.channels.fetch(config.logging.logs.channel) as Discord.TextChannel;
61 let description = {};
62 Object.entries(log.list).map(entry => {
63 let key = entry[0];
64 let value:any = entry[1];
65 if(value.displayValue) {
66 description[key] = value.displayValue;
67 } else {
68 description[key] = value;
69 }
70 })
71 if (channel) {
72 log.separate = log.separate || {};
73 let embed = new Discord.MessageEmbed()
74 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
75 .setDescription(
76 (log.separate.start ? log.separate.start + "\n" : "") +
77 generateKeyValueList(description) +
78 (log.separate.end ? "\n" + log.separate.end : "")
79 )
80 .setTimestamp(log.meta.timestamp)
81 .setColor(log.meta.color);
82 channel.send({embeds: [embed]});
83 }
84 }
85 saveLog(log);
86 }
87}
88
89
90export default {}
91
92async function saveLog(log: any): Promise<void> {
93}
94
95export function readLogs(guild: string) {
96
97}
98
99export function readSpecificLog(guild: string, id: number) {
100
101}