feat(discord-bot): use new logger

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

View File

@@ -14,6 +14,7 @@
"typescript": "^5.7.2" "typescript": "^5.7.2"
}, },
"dependencies": { "dependencies": {
"@lbf-bot/utils": "workspace:*",
"@lbf-bot/database": "workspace:*", "@lbf-bot/database": "workspace:*",
"discord.js": "^14.21.0", "discord.js": "^14.21.0",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",

View File

@@ -3,6 +3,9 @@ import { isWovPlayerTracked, untrackWovPlayer } from "~/services/tracking";
import { searchPlayer } from "~/services/wov"; import { searchPlayer } from "~/services/wov";
import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord"; import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord";
import { env } from "~/env"; import { env } from "~/env";
import { createLogger } from "@lbf-bot/utils";
const trackingLogger = createLogger({ prefix: "tracking" });
export const tejtrackCommand: Command = async (message, args) => { export const tejtrackCommand: Command = async (message, args) => {
// check staff permission // check staff permission
@@ -45,7 +48,9 @@ export const tejtrackCommand: Command = async (message, args) => {
); );
const chan = message.client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL); const chan = message.client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL);
if (!chan?.isSendable()) throw "Invalid tracking channel"; if (!chan?.isSendable()) {
return trackingLogger.fatal("Invalid 'DISCORD_TRACKING_CHANNEL'");
}
await chan.send( await chan.send(
createInfoEmbed( createInfoEmbed(

View File

@@ -3,6 +3,9 @@ import { trackWovPlayer, isWovPlayerTracked } from "~/services/tracking";
import { searchPlayer } from "~/services/wov"; import { searchPlayer } from "~/services/wov";
import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord"; import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord";
import { env } from "~/env"; import { env } from "~/env";
import { createLogger } from "@lbf-bot/utils";
const trackingLogger = createLogger({ prefix: "tracking" });
export const trackCommand: Command = async (message, args) => { export const trackCommand: Command = async (message, args) => {
// check staff permission // check staff permission
@@ -46,7 +49,9 @@ export const trackCommand: Command = async (message, args) => {
); );
const chan = message.client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL); const chan = message.client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL);
if (!chan?.isSendable()) throw "Invalid tracking channel"; if (!chan?.isSendable()) {
return trackingLogger.fatal("Invalid 'DISCORD_TRACKING_CHANNEL'");
}
await chan.send( await chan.send(
createInfoEmbed(`### [NEW] \`${playerName}\` [\`${player.id}\`]`), createInfoEmbed(`### [NEW] \`${playerName}\` [\`${player.id}\`]`),

View File

@@ -1,5 +1,8 @@
import { z } from "zod"; import { z } from "zod";
import "dotenv/config"; import "dotenv/config";
import { logger } from "@lbf-bot/utils";
// TODO: use parseEnv from utils
const schema = z.object({ const schema = z.object({
DISCORD_BOT_TOKEN: z.string(), DISCORD_BOT_TOKEN: z.string(),
@@ -29,13 +32,11 @@ const schema = z.object({
const result = schema.safeParse(process.env); const result = schema.safeParse(process.env);
if (!result.success) { if (!result.success) {
console.log("❌ Invalid environments variables:"); logger.fatal(
console.log( `❌ Invalid environments variables:\n${result.error.errors
result.error.errors
.map((x) => `- ${x.path.join(".")}: ${x.message}`) .map((x) => `- ${x.path.join(".")}: ${x.message}`)
.join("\n"), .join("\n")}`,
); );
process.exit(1);
} }
export const env = result.data; export const env = result.data;

View File

@@ -4,13 +4,14 @@ import { setupBotMode } from "~/modes/bot";
import { setupUserMode } from "~/modes/user"; import { setupUserMode } from "~/modes/user";
import { parseArgs } from "~/utils/cli"; import { parseArgs } from "~/utils/cli";
import { runMigrations } from "@lbf-bot/database"; import { runMigrations } from "@lbf-bot/database";
import { logger } from "@lbf-bot/utils";
console.log("Running database migrations..."); logger.info("Running database migrations...");
await runMigrations(); await runMigrations();
const mode = parseArgs(process.argv.slice(2)); const mode = parseArgs(process.argv.slice(2));
console.log(`Mode: ${mode.type}`); logger.info(`Mode: ${mode.type}`);
const client = new Client({ const client = new Client({
intents: [ intents: [
@@ -35,8 +36,7 @@ switch (mode.type) {
default: { default: {
// @ts-ignore // @ts-ignore
console.error(`ERROR: Not implemented: '${mode.type}'`); return logger.fatal(`ERROR: Not implemented: '${mode.type}'`);
process.exit(1);
} }
} }

View File

@@ -1,4 +1,5 @@
import type { Client } from "discord.js"; import type { Client } from "discord.js";
import { createLogger, logger } from "@lbf-bot/utils";
import { env } from "~/env"; import { env } from "~/env";
import { import {
listTrackedPlayers, listTrackedPlayers,
@@ -10,15 +11,24 @@ import { createInfoEmbed } from "~/utils/discord";
import { askForGrinders } from "~/utils/quest"; import { askForGrinders } from "~/utils/quest";
import { commands } from "~/commands"; import { commands } from "~/commands";
const questsLogger = createLogger({ prefix: "quests" });
const trackingLogger = createLogger({ prefix: "tracking" });
const questCheckCron = async (client: Client) => { const questCheckCron = async (client: Client) => {
questsLogger.info("Checking for new quest");
const quest = await checkForNewQuest(); const quest = await checkForNewQuest();
if (quest) { if (quest) {
questsLogger.info(`New quest found: '${quest.quest.id}'`);
await askForGrinders(quest, client); await askForGrinders(quest, client);
} else {
questsLogger.info("No new quest found");
} }
}; };
const trackingCron = async (client: Client) => { const trackingCron = async (client: Client) => {
trackingLogger.info("Checking for tracked players");
const trackedPlayers = await listTrackedPlayers(); const trackedPlayers = await listTrackedPlayers();
trackingLogger.info(`${trackedPlayers.length} players to check`);
for (const playerId of trackedPlayers) { for (const playerId of trackedPlayers) {
const player = await getPlayer(playerId); const player = await getPlayer(playerId);
if (!player) continue; if (!player) continue;
@@ -29,8 +39,9 @@ const trackingCron = async (client: Client) => {
await addUsernameToHistory(playerId, player.username); await addUsernameToHistory(playerId, player.username);
const chan = client.channels.cache.get(env.DISCORD_TRACKING_CHANNEL); 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]; const lastUsername = usernames[usernames.length - 1];
await chan.send( await chan.send(
@@ -39,12 +50,17 @@ const trackingCron = async (client: Client) => {
0x00ea00, 0x00ea00,
), ),
); );
trackingLogger.info(
`Username changed: ${lastUsername}\` -> \`${player.username} [\`${playerId}\`]`,
);
} }
}; };
export const setupBotMode = (client: Client) => { export const setupBotMode = (client: Client) => {
client.on("clientReady", async (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); await questCheckCron(client);
setInterval(() => questCheckCron(client), env.WOV_FETCH_INTERVAL); setInterval(() => questCheckCron(client), env.WOV_FETCH_INTERVAL);
@@ -64,7 +80,17 @@ export const setupBotMode = (client: Client) => {
const commandHandler = commands[command]; const commandHandler = commands[command];
if (commandHandler) { if (commandHandler) {
const child = logger.child(
`cmd:${command}${args.length > 0 ? " " : ""}${args.join(" ")}`,
);
try {
const start = Date.now();
await commandHandler(message, args); 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 type { Client, TextChannel } from "discord.js";
import { ChannelType } from "discord.js"; import { ChannelType } from "discord.js";
import * as readline from "node:readline"; import * as readline from "node:readline";
export const setupUserMode = (client: Client, channelId: string) => { export const setupUserMode = (client: Client, channelId: string) => {
client.on("clientReady", (client) => { 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); const chan = client.channels.cache.get(channelId);
if (chan?.type !== ChannelType.GuildText) { if (chan?.type !== ChannelType.GuildText) {

View File

@@ -2,11 +2,15 @@ import { ChannelType, type Client, type Message } from "discord.js";
import { env } from "~/env"; import { env } from "~/env";
import { makeResultEmbed } from "~/utils/discord"; import { makeResultEmbed } from "~/utils/discord";
import type { QuestResult } from "~/services/wov"; import type { QuestResult } from "~/services/wov";
import { createLogger } from "@lbf-bot/utils";
const questLogger = createLogger({ prefix: "quests" });
export const askForGrinders = async (quest: QuestResult, client: Client) => { export const askForGrinders = async (quest: QuestResult, client: Client) => {
const adminChannel = await client.channels.fetch(env.DISCORD_ADMIN_CHANNEL); const adminChannel = await client.channels.fetch(env.DISCORD_ADMIN_CHANNEL);
if (!adminChannel || adminChannel.type !== ChannelType.GuildText) if (!adminChannel || adminChannel.type !== ChannelType.GuildText) {
throw "Invalid admin channel provided"; return questLogger.fatal("Invalid 'DISCORD_ADMIN_CHANNEL'");
}
const top10 = quest.participants const top10 = quest.participants
.filter((x) => !env.QUEST_EXCLUDE.includes(x.username)) .filter((x) => !env.QUEST_EXCLUDE.includes(x.username))
@@ -91,7 +95,9 @@ export const askForGrinders = async (quest: QuestResult, client: Client) => {
} }
} }
if (answer === null) throw "unreachable"; if (answer === null) {
return questLogger.fatal("Answer was 'null', this should be unreachable");
}
const exclude = answer const exclude = answer
.split(",") .split(",")
@@ -107,9 +113,9 @@ export const askForGrinders = async (quest: QuestResult, client: Client) => {
if (rewardChannel && rewardChannel.type === ChannelType.GuildText) { if (rewardChannel && rewardChannel.type === ChannelType.GuildText) {
await rewardChannel.send(embed); await rewardChannel.send(embed);
} else { } else {
throw "Invalid reward channel"; return questLogger.fatal("Invalid 'DISCORD_REWARDS_CHANNEL'");
} }
await adminChannel.send("Envoyé !"); await adminChannel.send("Envoyé !");
console.log(`Quest result posted at: ${new Date().toISOString()}`); questLogger.info(`Results posted at: ${new Date().toISOString()}`);
}; };

3
pnpm-lock.yaml generated
View File

@@ -17,6 +17,9 @@ importers:
'@lbf-bot/database': '@lbf-bot/database':
specifier: workspace:* specifier: workspace:*
version: link:../../packages/database version: link:../../packages/database
'@lbf-bot/utils':
specifier: workspace:*
version: link:../../packages/utils
discord.js: discord.js:
specifier: ^14.21.0 specifier: ^14.21.0
version: 14.25.1 version: 14.25.1