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"; |
Skyler Grey | 1b66963 | 2023-03-05 10:58:11 +0000 | [diff] [blame^] | 7 | import {DiscordAPIError} from "discord.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
| 9 | const wait = promisify(setTimeout); |
| 10 | |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 11 | export interface LoggerOptions { |
| 12 | meta: { |
| 13 | type: string; |
| 14 | displayName: string; |
| 15 | calculateType: string; |
| 16 | color: number; |
| 17 | emoji: string; |
| 18 | timestamp: number; |
| 19 | }; |
| 20 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 21 | list: any; |
| 22 | hidden: { |
| 23 | guild: string; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 24 | }; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 25 | separate?: { |
| 26 | start?: string; |
| 27 | end?: string; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 28 | }; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | async function isLogging(guild: string, type: string): Promise<boolean> { |
| 32 | const config = await client.database.guilds.read(guild); |
| 33 | if (!config.logging.logs.enabled) return false; |
| 34 | if (!config.logging.logs.channel) return false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 35 | if (!toHexArray(config.logging.logs.toLog).includes(type)) { |
| 36 | return false; |
| 37 | } |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 38 | return true; |
| 39 | } |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 40 | |
| 41 | export const Logger = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 42 | renderUser(user: Discord.User | string) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 43 | if (typeof user === "string") user = client.users.cache.get(user)!; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 44 | return `${user.username} [<@${user.id}>]`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 45 | }, |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 46 | renderTime(t: number) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 47 | t = Math.floor((t /= 1000)); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 48 | return `<t:${t}:D> at <t:${t}:T>`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 49 | }, |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 50 | renderDelta(t: number) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 51 | t = Math.floor((t /= 1000)); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 52 | return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 53 | }, |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 54 | renderNumberDelta(num1: number, num2: number) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | const delta = num2 - num1; |
| 56 | return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 57 | }, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 58 | entry( |
| 59 | value: string | number | boolean | null | (string | boolean)[], |
| 60 | displayValue: string |
| 61 | ): { value: string | boolean | null | (string | boolean | number)[]; displayValue: string } { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 62 | if (typeof value === "number") value = value.toString(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | return { value: value, displayValue: displayValue }; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 64 | }, |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 65 | renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | string) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 66 | if (typeof channel === "string") |
| 67 | channel = client.channels.cache.get(channel) as Discord.GuildChannel | Discord.ThreadChannel; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 68 | return `${channel.name} [<#${channel.id}>]`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 69 | }, |
TheCodedProf | 486bca3 | 2023-02-02 16:49:44 -0500 | [diff] [blame] | 70 | renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 71 | if (typeof role === "string") |
| 72 | role = (typeof guild === "string" ? client.guilds.cache.get(guild) : guild)!.roles.cache.get(role)!; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 73 | return `${role.name} [<@&${role.id}>]`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 74 | }, |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 75 | renderEmoji(emoji: Discord.GuildEmoji) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 76 | return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 77 | }, |
| 78 | NucleusColors: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | red: 0xf27878, |
| 80 | yellow: 0xf2d478, |
| 81 | green: 0x68d49e |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 82 | }, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 83 | async getAuditLog( |
| 84 | guild: Discord.Guild, |
| 85 | event: Discord.GuildAuditLogsResolvable, |
| 86 | delay?: number |
| 87 | ): Promise<Discord.GuildAuditLogsEntry[]> { |
Skyler Grey | 1b66963 | 2023-03-05 10:58:11 +0000 | [diff] [blame^] | 88 | if (!guild.members.me?.permissions.has("ViewAuditLog")) return []; |
TheCodedProf | 686829f | 2023-02-22 15:08:01 -0500 | [diff] [blame] | 89 | await wait(delay ?? 250); |
Skyler Grey | 1b66963 | 2023-03-05 10:58:11 +0000 | [diff] [blame^] | 90 | try { |
| 91 | const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map((m) => m); |
| 92 | return auditLog as Discord.GuildAuditLogsEntry[]; |
| 93 | } catch (e) { |
| 94 | if (e instanceof DiscordAPIError) return []; |
| 95 | throw e; |
| 96 | } |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 97 | }, |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 98 | async log(log: LoggerOptions): Promise<void> { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 99 | if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return; |
| 100 | console.log(log.hidden.guild); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | const config = await client.database.guilds.read(log.hidden.guild); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 102 | console.log(config.logging.logs.channel); |
Samuel Shuert | a1511f9 | 2023-03-04 13:55:33 -0500 | [diff] [blame] | 103 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 104 | if (config.logging.logs.channel) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 105 | const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 106 | const description: Record<string, string> = {}; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | Object.entries(log.list).map((entry) => { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 108 | const key: string = entry[0]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 110 | const value: any = entry[1]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 111 | if (value.displayValue) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 112 | description[key] = value.displayValue; |
| 113 | } else { |
| 114 | description[key] = value; |
| 115 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 116 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 117 | if (channel) { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 118 | log.separate = log.separate ?? {}; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 119 | const embed = new Discord.EmbedBuilder() |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 121 | .setDescription( |
| 122 | (log.separate.start ? log.separate.start + "\n" : "") + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 123 | generateKeyValueList(description) + |
| 124 | (log.separate.end ? "\n" + log.separate.end : "") |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 125 | ) |
| 126 | .setTimestamp(log.meta.timestamp) |
| 127 | .setColor(log.meta.color); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | channel.send({ embeds: [embed] }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 131 | }, |
| 132 | isLogging |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | export default {}; |