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