pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame^] | 1 | import * as fs from 'fs'; |
| 2 | import * as Discord from 'discord.js'; |
| 3 | import getEmojiByName from './getEmojiByName.js'; |
| 4 | import readConfig from './readConfig.js'; |
| 5 | import { toHexArray } from './calculate.js'; |
| 6 | import { promisify } from 'util'; |
| 7 | import generateKeyValueList from './generateKeyValueList.js'; |
| 8 | |
| 9 | const wait = promisify(setTimeout); |
| 10 | |
| 11 | |
| 12 | export class Logger { |
| 13 | renderUser(user: Discord.User) { |
| 14 | return `${user.username} [<@${user.id}>]`; |
| 15 | } |
| 16 | renderTime(t: number) { |
| 17 | t = Math.floor(t /= 1000) |
| 18 | return `<t:${t}:D> at <t:${t}:T>`; |
| 19 | } |
| 20 | renderDelta(t: number) { |
| 21 | t = Math.floor(t /= 1000) |
| 22 | return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`; |
| 23 | } |
| 24 | renderNumberDelta(num1, num2) { |
| 25 | let delta = num2 - num1; |
| 26 | return `${num1} -> ${num2} (${delta > 0 ? '+' : ''}${delta})`; |
| 27 | } |
| 28 | entry(value, displayValue) { |
| 29 | return { value: value, displayValue: displayValue } |
| 30 | } |
| 31 | renderChannel(channel: Discord.GuildChannel) { |
| 32 | return `${channel.name} [<#${channel.id}>]`; |
| 33 | } |
| 34 | renderRole(role: Discord.Role) { |
| 35 | return `${role.name} [<@&${role.id}>]`; |
| 36 | } |
| 37 | renderEmoji(emoji: Discord.GuildEmoji) { |
| 38 | return `<${emoji.animated ? 'a' : ''}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`; |
| 39 | } |
| 40 | |
| 41 | public readonly NucleusColors = { |
| 42 | red: 0xF27878, |
| 43 | yellow: 0xF2D478, |
| 44 | green: 0x68D49E, |
| 45 | |
| 46 | } |
| 47 | |
| 48 | |
| 49 | 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 | |
| 90 | export default {} |
| 91 | |
| 92 | async function saveLog(log: any): Promise<void> { |
| 93 | } |
| 94 | |
| 95 | export function readLogs(guild: string) { |
| 96 | |
| 97 | } |
| 98 | |
| 99 | export function readSpecificLog(guild: string, id: number) { |
| 100 | |
| 101 | } |