blob: 3f46f8603e5d5b793b836b2b1481df0d4c4a87e9 [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 },
PineaFan538d3752023-01-12 21:48:23 +000028 entry(value: string | number | null, displayValue: string): { value: string | null; 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 },
pineafan41d93562022-07-30 22:10:15 +010032 renderChannel(channel: Discord.GuildChannel | Discord.ThreadChannel) {
pineafane625d782022-05-09 18:04:32 +010033 return `${channel.name} [<#${channel.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000034 },
pineafane625d782022-05-09 18:04:32 +010035 renderRole(role: Discord.Role) {
36 return `${role.name} [<@&${role.id}>]`;
PineaFan64486c42022-12-28 09:21:04 +000037 },
pineafane625d782022-05-09 18:04:32 +010038 renderEmoji(emoji: Discord.GuildEmoji) {
Skyler Grey11236ba2022-08-08 21:13:33 +010039 return `<${emoji.animated ? "a" : ""}:${emoji.name}:${emoji.id}> [\`:${emoji.name}:\`]`;
PineaFan64486c42022-12-28 09:21:04 +000040 },
41 NucleusColors: {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 red: 0xf27878,
43 yellow: 0xf2d478,
44 green: 0x68d49e
PineaFan64486c42022-12-28 09:21:04 +000045 },
46 async getAuditLog(guild: Discord.Guild, event: Discord.GuildAuditLogsResolvable): Promise<Discord.GuildAuditLogsEntry[]> {
pineafan63fc5e22022-08-04 22:04:10 +010047 await wait(250);
PineaFan538d3752023-01-12 21:48:23 +000048 const auditLog = (await guild.fetchAuditLogs({ type: event })).entries.map(m => m)
49 return auditLog as Discord.GuildAuditLogsEntry[];
PineaFan64486c42022-12-28 09:21:04 +000050 },
pineafan63fc5e22022-08-04 22:04:10 +010051 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010052 async log(log: any): Promise<void> {
pineafan63fc5e22022-08-04 22:04:10 +010053 const config = await client.database.guilds.read(log.hidden.guild);
pineafane625d782022-05-09 18:04:32 +010054 if (!config.logging.logs.enabled) return;
PineaFan538d3752023-01-12 21:48:23 +000055 if (!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) {
56 console.log("Not logging this type of event");
PineaFan64486c42022-12-28 09:21:04 +000057 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 {};