feat(discord-bot): use new logger

This commit is contained in:
2025-12-05 20:24:17 +01:00
parent a541b82404
commit cf6cc7ff7b
9 changed files with 70 additions and 21 deletions

View File

@@ -1,4 +1,5 @@
import type { Client } from "discord.js";
import { createLogger, logger } from "@lbf-bot/utils";
import { env } from "~/env";
import {
listTrackedPlayers,
@@ -10,15 +11,24 @@ import { createInfoEmbed } from "~/utils/discord";
import { askForGrinders } from "~/utils/quest";
import { commands } from "~/commands";
const questsLogger = createLogger({ prefix: "quests" });
const trackingLogger = createLogger({ prefix: "tracking" });
const questCheckCron = async (client: Client) => {
questsLogger.info("Checking for new quest");
const quest = await checkForNewQuest();
if (quest) {
questsLogger.info(`New quest found: '${quest.quest.id}'`);
await askForGrinders(quest, client);
} else {
questsLogger.info("No new quest found");
}
};
const trackingCron = async (client: Client) => {
trackingLogger.info("Checking for tracked players");
const trackedPlayers = await listTrackedPlayers();
trackingLogger.info(`${trackedPlayers.length} players to check`);
for (const playerId of trackedPlayers) {
const player = await getPlayer(playerId);
if (!player) continue;
@@ -29,8 +39,9 @@ const trackingCron = async (client: Client) => {
await addUsernameToHistory(playerId, player.username);
const chan = client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL);
if (!chan?.isSendable()) throw "Invalid tracking channel";
if (!chan?.isSendable()) {
return logger.fatal("Invalid 'DISCORD_TRACKING_CHANNEL'");
}
const lastUsername = usernames[usernames.length - 1];
await chan.send(
@@ -39,12 +50,17 @@ const trackingCron = async (client: Client) => {
0x00ea00,
),
);
trackingLogger.info(
`Username changed: ${lastUsername}\` -> \`${player.username} [\`${playerId}\`]`,
);
}
};
export const setupBotMode = (client: Client) => {
client.on("clientReady", async (client) => {
console.log(`Logged in as ${client.user.username}`);
logger.info(`Client ready`);
logger.info(`Connected as @${client.user.username}`);
await questCheckCron(client);
setInterval(() => questCheckCron(client), env.WOV_FETCH_INTERVAL);
@@ -64,7 +80,17 @@ export const setupBotMode = (client: Client) => {
const commandHandler = commands[command];
if (commandHandler) {
await commandHandler(message, args);
const child = logger.child(
`cmd:${command}${args.length > 0 ? " " : ""}${args.join(" ")}`,
);
try {
const start = Date.now();
await commandHandler(message, args);
const end = Date.now();
child.info(`Done in ${(end - start).toFixed(2)}ms`);
} catch (error: unknown) {
child.error("Failed:", error);
}
}
}
});

View File

@@ -1,10 +1,12 @@
import { logger } from "@lbf-bot/utils";
import type { Client, TextChannel } from "discord.js";
import { ChannelType } from "discord.js";
import * as readline from "node:readline";
export const setupUserMode = (client: Client, channelId: string) => {
client.on("clientReady", (client) => {
console.log(`Logged in as ${client.user.username}`);
logger.info(`Client ready`);
logger.info(`Connected as @${client.user.username}`);
const chan = client.channels.cache.get(channelId);
if (chan?.type !== ChannelType.GuildText) {