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) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 14 | if (typeof user === 'string') return `${user} [<@${user}>]`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 32 | renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 49 | async getAuditLog(guild: Discord.Guild, event): Promise<Discord.GuildAuditLogsEntry[]>{ |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 50 | await wait(250) |
| 51 | let auditLog = await guild.fetchAuditLogs({type: event}); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 52 | return auditLog as unknown as Discord.GuildAuditLogsEntry[]; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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> { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 56 | let config = await client.database.guilds.read(log.hidden.guild); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 57 | if (!config.logging.logs.enabled) return; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 58 | if (!(log.meta.calculateType === true)) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 87 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | export default {} |