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