blob: a578b69672792e44588749027c1479052dc39ebd [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 },
pineafan4e425942022-08-08 22:01:47 +010028 entry(value: string, displayValue: string): { value: string; displayValue: string } {
pineafan63fc5e22022-08-04 22:04:10 +010029 return { value: value, displayValue: displayValue };
PineaFan64486c42022-12-28 09:21:04 +000030 },
pineafan41d93562022-07-30 22:10:15 +010031 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) {
pineafane625d782022-05-09 18:04:32 +010032 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000033 },
pineafane625d782022-05-09 18:04:32 +010034 renderRole(role: Discord.Role) {
35 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000036 },
pineafane625d782022-05-09 18:04:32 +010037 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010038 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000039 },
40 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 red: 0xf27878,
42 yellow: 0xf2d478,
43 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000044 },
45 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010046 await wait(250);
Skyler Grey75ea9172022-08-06 10:22:23 +010047 const auditLog = await guild.fetchAuditLogs({ type: event });
pineafanda6e5342022-07-03 10:03:16 +010048 return auditLog as unknown as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000049 },
pineafan63fc5e22022-08-04 22:04:10 +010050 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010051 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010052 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010053 if (!config.logging.logs.enabled) return;
pineafane23c4ec2022-07-27 21:56:27 +010054 if (!(log.meta.calculateType === true)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010055 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType))
PineaFan64486c42022-12-28 09:21:04 +000056 console.log("Not logging this type of event");
57 return;
pineafane625d782022-05-09 18:04:32 +010058 }
59 if (config.logging.logs.channel) {
Skyler Grey11236ba2022-08-08 21:13:33 +010060 const channel = (await client.channels.fetch(config.logging.logs.channel)) as Discord.TextChannel | null;
pineafanbd02b4a2022-08-05 22:01:38 +010061 const description: Record<string, string> = {};
Skyler Grey75ea9172022-08-06 10:22:23 +010062 Object.entries(log.list).map((entry) => {
pineafanbd02b4a2022-08-05 22:01:38 +010063 const key: string = entry[0];
pineafan63fc5e22022-08-04 22:04:10 +010064 // eslint-disable-next-line @typescript-eslint/no-explicit-any
65 const value: any = entry[1];
Skyler Grey75ea9172022-08-06 10:22:23 +010066 if (value.displayValue) {
pineafane625d782022-05-09 18:04:32 +010067 description[key] = value.displayValue;
68 } else {
69 description[key] = value;
70 }
pineafan63fc5e22022-08-04 22:04:10 +010071 });
pineafane625d782022-05-09 18:04:32 +010072 if (channel) {
73 log.separate = log.separate || {};
TheCodedProf21c08592022-09-13 14:14:43 -040074 const embed = new Discord.EmbedBuilder()
Skyler Grey11236ba2022-08-08 21:13:33 +010075 .setTitle(`${getEmojiByName(log.meta.emoji)} ${log.meta.displayName}`)
pineafane625d782022-05-09 18:04:32 +010076 .setDescription(
77 (log.separate.start ? log.separate.start + "\n" : "") +
PineaFan64486c42022-12-28 09:21:04 +000078 generateKeyValueList(description) +
79 (log.separate.end ? "\n" + log.separate.end : "")
pineafane625d782022-05-09 18:04:32 +010080 )
81 .setTimestamp(log.meta.timestamp)
82 .setColor(log.meta.color);
Skyler Grey75ea9172022-08-06 10:22:23 +010083 channel.send({ embeds: [embed] });
pineafane625d782022-05-09 18:04:32 +010084 }
85 }
pineafane625d782022-05-09 18:04:32 +010086 }
PineaFan64486c42022-12-28 09:21:04 +000087};
88
pineafan32767212022-03-14 21:27:39 +000089
pineafan63fc5e22022-08-04 22:04:10 +010090export default {};