blob: 2679fd65a22523cafbb0ac99a57bb9db3ca15efa [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import * as Discord from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import { toHexArray } from "./calculate.js";
3import { promisify } from "util";
4import generateKeyValueList from "./generateKeyValueList.js";
5import client from "./client.js";
Skyler Grey67691762023-03-06 09:58:19 +00006import { DiscordAPIError } from "discord.js";
TheCodedProf4a7c25d2023-06-07 17:09:45 -04007import EmojiEmbed from "./generateEmojiEmbed.js";
pineafan32767212022-03-14 21:27:39 +00008
9const wait = promisify(setTimeout);
10
TheCodedProf6ec331b2023-02-20 12:13:06 -050011export interface LoggerOptions {
12 meta: {
13 type: string;
14 displayName: string;
15 calculateType: string;
16 color: number;
17 emoji: string;
18 timestamp: number;
pineafan67c9f1f2023-06-23 22:50:26 +010019 buttons?: { buttonText: string, buttonId: string, buttonStyle: Discord.ButtonStyle }[];
20 imageData?: string;
TheCodedProf6ec331b2023-02-20 12:13:06 -050021 };
TheCodedProf7b985d82023-06-08 16:40:41 -040022 list: Record<string | symbol | number, unknown>;
TheCodedProf6ec331b2023-02-20 12:13:06 -050023 hidden: {
24 guild: string;
Skyler Greyda16adf2023-03-05 10:22:12 +000025 };
TheCodedProf6ec331b2023-02-20 12:13:06 -050026 separate?: {
27 start?: string;
28 end?: string;
Skyler Greyda16adf2023-03-05 10:22:12 +000029 };
TheCodedProf6ec331b2023-02-20 12:13:06 -050030}
31
32async function isLogging(guild: string, type: string): Promise<boolean> {
33 const config = await client.database.guilds.read(guild);
34 if (!config.logging.logs.enabled) return false;
35 if (!config.logging.logs.channel) return false;
Skyler Greyda16adf2023-03-05 10:22:12 +000036 if (!toHexArray(config.logging.logs.toLog).includes(type)) {
37 return false;
38 }
TheCodedProf6ec331b2023-02-20 12:13:06 -050039 return true;
40}
PineaFan64486c42022-12-28 09:21:04 +000041
pineafan67c9f1f2023-06-23 22:50:26 +010042const NucleusColors = {
43 red: 0xf27878,
44 yellow: 0xf2d478,
45 green: 0x68d49e,
46 blue: 0x72aef5,
47};
48
PineaFan64486c42022-12-28 09:21:04 +000049export const Logger = {
pineafane625d782022-05-09 18:04:32 +010050 renderUser(user: Discord.User | string) {
PineaFanb0d0c242023-02-05 10:59:45 +000051 if (typeof user === "string") user = client.users.cache.get(user)!;
pineafane625d782022-05-09 18:04:32 +010052 return `${user.username} [<@${user.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000053 },
pineafane625d782022-05-09 18:04:32 +010054 renderTime(t: number) {
Skyler Grey67691762023-03-06 09:58:19 +000055 if (isNaN(t)) return "Unknown";
Skyler Grey75ea9172022-08-06 10:22:23 +010056 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010057 return `<t:${t}:D> at <t:${t}:T>`;
PineaFan64486c42022-12-28 09:21:04 +000058 },
pineafane625d782022-05-09 18:04:32 +010059 renderDelta(t: number) {
Skyler Grey67691762023-03-06 09:58:19 +000060 if (isNaN(t)) return "Unknown";
Skyler Grey75ea9172022-08-06 10:22:23 +010061 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010062 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
PineaFan64486c42022-12-28 09:21:04 +000063 },
pineafanbd02b4a2022-08-05 22:01:38 +010064 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010065 const delta = num2 - num1;
66 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
PineaFan64486c42022-12-28 09:21:04 +000067 },
Skyler Greyda16adf2023-03-05 10:22:12 +000068 entry(
69 value: string | number | boolean | null | (string | boolean)[],
70 displayValue: string
71 ): { value: string | boolean | null | (string | boolean | number)[]; displayValue: string } {
PineaFan0d06edc2023-01-17 22:10:31 +000072 if (typeof value === "number") value = value.toString();
pineafan63fc5e22022-08-04 22:04:10 +010073 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000074 },
TheCodedProf4a6d5712023-01-19 15:54:40 -050075 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | string) {
Skyler Greyda16adf2023-03-05 10:22:12 +000076 if (typeof channel === "string")
77 channel = client.channels.cache.get(channel) as Discord.GuildChannel | Discord.ThreadChannel;
pineafane625d782022-05-09 18:04:32 +010078 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000079 },
TheCodedProf486bca32023-02-02 16:49:44 -050080 renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) {
Skyler Greyda16adf2023-03-05 10:22:12 +000081 if (typeof role === "string")
82 role = (typeof guild === "string" ? client.guilds.cache.get(guild) : guild)!.roles.cache.get(role)!;
pineafane625d782022-05-09 18:04:32 +010083 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000084 },
pineafane625d782022-05-09 18:04:32 +010085 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010086 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000087 },
pineafan67c9f1f2023-06-23 22:50:26 +010088 NucleusColors,
Skyler Greyda16adf2023-03-05 10:22:12 +000089 async getAuditLog(
90 guild: Discord.Guild,
91 event: Discord.GuildAuditLogsResolvable,
92 delay?: number
93 ): Promise<Discord.GuildAuditLogsEntry[]> {
Skyler Grey1b669632023-03-05 10:58:11 +000094 if (!guild.members.me?.permissions.has("ViewAuditLog")) return [];
TheCodedProf686829f2023-02-22 15:08:01 -050095 await wait(delay ?? 250);
Skyler Grey1b669632023-03-05 10:58:11 +000096 try {
97 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map((m) => m);
98 return auditLog as Discord.GuildAuditLogsEntry[];
99 } catch (e) {
100 if (e instanceof DiscordAPIError) return [];
101 throw e;
102 }
PineaFan64486c42022-12-28 09:21:04 +0000103 },
pineafan67c9f1f2023-06-23 22:50:26 +0100104
TheCodedProf6ec331b2023-02-20 12:13:06 -0500105 async log(log: LoggerOptions): Promise<void> {
Skyler Greyda16adf2023-03-05 10:22:12 +0000106 if (!(await isLogging(log.hidden.guild, log.meta.calculateType))) return;
pineafan63fc5e22022-08-04 22:04:10 +0100107 const config = await client.database.guilds.read(log.hidden.guild);
Samuel Shuerta1511f92023-03-04 13:55:33 -0500108
pineafane625d782022-05-09 18:04:32 +0100109 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +0100111 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +0100113 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +0100114 // eslint-disable-next-line @typescript-eslint/no-explicit-any
115 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +0100117 description[key] = value.displayValue;
118 } else {
119 description[key] = value;
120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121 });
pineafan67c9f1f2023-06-23 22:50:26 +0100122 console.log("imageData", log.meta.imageData)
pineafane625d782022-05-09 18:04:32 +0100123 if (channel) {
TheCodedProf1807fb32023-02-20 14:33:48 -0500124 log.separate = log.separate ?? {};
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400125 const messageOptions: Parameters<Discord.TextChannel["send"]>[0] = {};
126 const components: Discord.ActionRowBuilder<Discord.ButtonBuilder> = new Discord.ActionRowBuilder();
127 messageOptions.embeds = [
128 new EmojiEmbed()
129 .setEmoji(log.meta.emoji)
130 .setTitle(log.meta.displayName)
131 .setDescription(
132 (log.separate.start ? log.separate.start + "\n" : "") +
133 generateKeyValueList(description) +
134 (log.separate.end ? "\n" + log.separate.end : "")
135 )
136 .setTimestamp(log.meta.timestamp)
137 .setColor(log.meta.color)
pineafan67c9f1f2023-06-23 22:50:26 +0100138 .setImage(log.meta.imageData ? "attachment://extra_log_data.json.base64" : null)
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400139 ];
pineafan67c9f1f2023-06-23 22:50:26 +0100140 if (log.meta.buttons) {
141 const buttons = []
142 for (const button of log.meta.buttons) {
143 buttons.push(
144 new Discord.ButtonBuilder()
145 .setCustomId(button.buttonId)
146 .setLabel(button.buttonText)
147 .setStyle(button.buttonStyle)
148 )
149 }
150 components.addComponents(buttons);
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400151 messageOptions.components = [components];
152 }
pineafan67c9f1f2023-06-23 22:50:26 +0100153 if (log.meta.imageData) {
154 messageOptions.files = [
155 {
156 attachment: Buffer.from(btoa(log.meta.imageData), "utf-8"), // Use base 64 to prevent virus scanning (EICAR)Buffer.from(log.meta.imageData, "base64"),
157 name: "extra_log_data.json.base64"
158 }
159 ];
160 }
TheCodedProf4a7c25d2023-06-07 17:09:45 -0400161 await channel.send(messageOptions);
pineafane625d782022-05-09 18:04:32 +0100162 }
163 }
TheCodedProf6ec331b2023-02-20 12:13:06 -0500164 },
165 isLogging
PineaFan64486c42022-12-28 09:21:04 +0000166};
167
pineafan63fc5e22022-08-04 22:04:10 +0100168export default {};