blob: 4eff4e2c63a4793212fc37165bed853edeee9ecb [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import * as Discord from "discord.js";
2import getEmojiByName from "./getEmojiByName.js";
3import { toHexArray } from "./calculate.js";
4import { promisify } from "util";
5import generateKeyValueList from "./generateKeyValueList.js";
6import client from "./client.js";
pineafan32767212022-03-14 21:27:39 +00007
8const wait = promisify(setTimeout);
9
PineaFan64486c42022-12-28 09:21:04 +000010
11export const Logger = {
pineafane625d782022-05-09 18:04:32 +010012 renderUser(user: Discord.User | string) {
pineafan63fc5e22022-08-04 22:04:10 +010013 if (typeof user === "string") return `${user} [<@${user}>]`;
pineafane625d782022-05-09 18:04:32 +010014 return `${user.username} [<@${user.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000015 },
pineafane625d782022-05-09 18:04:32 +010016 renderTime(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010017 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010018 return `<t:${t}:D> at <t:${t}:T>`;
PineaFan64486c42022-12-28 09:21:04 +000019 },
pineafane625d782022-05-09 18:04:32 +010020 renderDelta(t: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 t = Math.floor((t /= 1000));
pineafane625d782022-05-09 18:04:32 +010022 return `<t:${t}:R> (<t:${t}:D> at <t:${t}:T>)`;
PineaFan64486c42022-12-28 09:21:04 +000023 },
pineafanbd02b4a2022-08-05 22:01:38 +010024 renderNumberDelta(num1: number, num2: number) {
pineafan63fc5e22022-08-04 22:04:10 +010025 const delta = num2 - num1;
26 return `${num1} -> ${num2} (${delta > 0 ? "+" : ""}${delta})`;
PineaFan64486c42022-12-28 09:21:04 +000027 },
TheCodedProfa16d1672023-01-18 18:58:34 -050028 entry(value: string | number | boolean | null | (string | boolean)[], displayValue: string): { value: string | boolean | null | (string | boolean | number)[]; displayValue: string } {
PineaFan0d06edc2023-01-17 22:10:31 +000029 if (typeof value === "number") value = value.toString();
pineafan63fc5e22022-08-04 22:04:10 +010030 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000031 },
TheCodedProf4a6d5712023-01-19 15:54:40 -050032 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel | string) {
33 if (typeof channel === "string") channel = client.channels.cache.get(channel) as Discord.GuildChannel | Discord.ThreadChannel;
pineafane625d782022-05-09 18:04:32 +010034 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000035 },
TheCodedProf486bca32023-02-02 16:49:44 -050036 renderRole(role: Discord.Role | string, guild?: Discord.Guild | string) {
37 if (typeof role === "string") role = (typeof guild === "string" ? client.guilds.cache.get(guild) : guild)!.roles.cache.get(role) as Discord.Role;
pineafane625d782022-05-09 18:04:32 +010038 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000039 },
pineafane625d782022-05-09 18:04:32 +010040 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010041 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000042 },
43 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 red: 0xf27878,
45 yellow: 0xf2d478,
46 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000047 },
48 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010049 await wait(250);
PineaFan538d3752023-01-12 21:48:23 +000050 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map(m => m)
51 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000052 },
pineafan63fc5e22022-08-04 22:04:10 +010053 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010054 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010055 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010056 if (!config.logging.logs.enabled) return;
PineaFan538d3752023-01-12 21:48:23 +000057 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) {
58 console.log("Not logging this type of event");
PineaFan64486c42022-12-28 09:21:04 +000059 return;
pineafane625d782022-05-09 18:04:32 +010060 }
61 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010062 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010063 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010064 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010065 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010066 // eslint-disable-next-line @typescript-eslint/no-explicit-any
67 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010068 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010069 description[key] = value.displayValue;
70 } else {
71 description[key] = value;
72 }
pineafan63fc5e22022-08-04 22:04:10 +010073 });
pineafane625d782022-05-09 18:04:32 +010074 if (channel) {
75 log.separate = log.separate || {};
TheCodedProf21c08592022-09-13 14:14:43 -040076 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +010077 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +010078 .setDescription(
79 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +000080 generateKeyValueList(description) +
81 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010082 )
83 .setTimestamp(log.meta.timestamp)
84 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010085 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +010086 }
87 }
pineafane625d782022-05-09 18:04:32 +010088 }
PineaFan64486c42022-12-28 09:21:04 +000089};
90
pineafan32767212022-03-14 21:27:39 +000091
pineafan63fc5e22022-08-04 22:04:10 +010092export default {};