blob: 184f29a91d54cc084fdb7da1dbaeb8d61c2623a7 [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 },
pineafane625d782022-05-09 18:04:32 +010036 renderRole(role: Discord.Role) {
37 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000038 },
pineafane625d782022-05-09 18:04:32 +010039 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010040 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000041 },
42 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010043 red: 0xf27878,
44 yellow: 0xf2d478,
45 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000046 },
47 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010048 await wait(250);
PineaFan538d3752023-01-12 21:48:23 +000049 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map(m => m)
50 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000051 },
pineafan63fc5e22022-08-04 22:04:10 +010052 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010053 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010054 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010055 if (!config.logging.logs.enabled) return;
PineaFan538d3752023-01-12 21:48:23 +000056 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) {
57 console.log("Not logging this type of event");
PineaFan64486c42022-12-28 09:21:04 +000058 return;
pineafane625d782022-05-09 18:04:32 +010059 }
60 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010061 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010062 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010063 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010064 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010065 // eslint-disable-next-line @typescript-eslint/no-explicit-any
66 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010067 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010068 description[key] = value.displayValue;
69 } else {
70 description[key] = value;
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 });
pineafane625d782022-05-09 18:04:32 +010073 if (channel) {
74 log.separate = log.separate || {};
TheCodedProf21c08592022-09-13 14:14:43 -040075 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +010076 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +010077 .setDescription(
78 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +000079 generateKeyValueList(description) +
80 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010081 )
82 .setTimestamp(log.meta.timestamp)
83 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010084 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +010085 }
86 }
pineafane625d782022-05-09 18:04:32 +010087 }
PineaFan64486c42022-12-28 09:21:04 +000088};
89
pineafan32767212022-03-14 21:27:39 +000090
pineafan63fc5e22022-08-04 22:04:10 +010091export default {};