pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import * as Discord from "discord.js"; |
| 2 | import getEmojiByName from "./getEmojiByName.js"; |
| 3 | import { toHexArray } from "./calculate.js"; |
| 4 | import { promisify } from "util"; |
| 5 | import generateKeyValueList from "./generateKeyValueList.js"; |
| 6 | import client from "./client.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 7 | |
| 8 | const wait = promisify(setTimeout); |
| 9 | |
| 10 | |
| 11 | export class Logger { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 12 | renderUser(user: Discord.User | string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | if (typeof user === "string") return `${user} [<@${user}>]`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 14 | return `${user.username} [<@${user.id}>]`; |
| 15 | } |
| 16 | renderTime(t: number) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | t = Math.floor(t /= 1000); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 18 | return `<t:${t}:D> at <t:${t}:T>`; |
| 19 | } |
| 20 | renderDelta(t: number) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | t = Math.floor(t /= 1000); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 22 | return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`; |
| 23 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 24 | renderNumberDelta(num1: number, num2: number) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const delta = num2 - num1; |
| 26 | return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 27 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 28 | entry(value: string, displayValue: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | return { value: value, displayValue: displayValue }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 30 | } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 31 | renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 32 | return `${channel.name} [<#${channel.id}>]`; |
| 33 | } |
| 34 | renderRole(role: Discord.Role) { |
| 35 | return `${role.name} [<@&${role.id}>]`; |
| 36 | } |
| 37 | renderEmoji(emoji: Discord.GuildEmoji) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 39 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 40 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 41 | public readonly NucleusColors = { |
| 42 | red: 0xF27878, |
| 43 | yellow: 0xF2D478, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | green: 0x68D49E |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 45 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | }; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 47 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 48 | async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]>{ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | await wait(250); |
| 50 | const auditLog = await guild.fetchAuditLogs({type: event}); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 51 | return auditLog as unknown as Discord.GuildAuditLogsEntry[]; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 52 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 53 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 55 | async log(log: any): Promise<void> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | const 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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | if(!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) return console.log("Not logging this type of event"); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 60 | } |
| 61 | if (config.logging.logs.channel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | const channel = await client.channels.fetch(config.logging.logs.channel) as Discord.TextChannel; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 63 | const description: Record<string, string> = {}; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 64 | Object.entries(log.list).map(entry => { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 65 | const key: string = entry[0]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 67 | const value: any = entry[1]; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 68 | if(value.displayValue) { |
| 69 | description[key] = value.displayValue; |
| 70 | } else { |
| 71 | description[key] = value; |
| 72 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 74 | if (channel) { |
| 75 | log.separate = log.separate || {}; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | const embed = new Discord.MessageEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 77 | .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`) |
| 78 | .setDescription( |
| 79 | (log.separate.start ? log.separate.start + "\n" : "") + |
| 80 | generateKeyValueList(description) + |
| 81 | (log.separate.end ? "\n" + log.separate.end : "") |
| 82 | ) |
| 83 | .setTimestamp(log.meta.timestamp) |
| 84 | .setColor(log.meta.color); |
| 85 | channel.send({embeds: [embed]}); |
| 86 | } |
| 87 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 88 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | export default {}; |