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