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