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'; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 4 | import { toHexArray } from './calculate.js'; |
| 5 | import { promisify } from 'util'; |
| 6 | import generateKeyValueList from './generateKeyValueList.js'; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 7 | import client from './client.js'; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
| 9 | const wait = promisify(setTimeout); |
| 10 | |
| 11 | |
| 12 | export class Logger { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 41 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 42 | public readonly NucleusColors = { |
| 43 | red: 0xF27878, |
| 44 | yellow: 0xF2D478, |
| 45 | green: 0x68D49E, |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 46 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 47 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 48 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 49 | async getAuditLog(guild: Discord.Guild, event) { |
| 50 | await wait(250) |
| 51 | let auditLog = await guild.fetchAuditLogs({type: event}); |
| 52 | return auditLog; |
| 53 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 54 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 55 | async log(log: any): Promise<void> { |
| 56 | let config = await client.database.read(log.hidden.guild); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | export default {} |
| 93 | |
| 94 | async function saveLog(log: any): Promise<void> { |
| 95 | } |
| 96 | |
| 97 | export function readLogs(guild: string) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 98 | |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | export function readSpecificLog(guild: string, id: number) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 102 | |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 103 | } |