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