feat(discord-bot): use new logger
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lbf-bot/utils": "workspace:*",
|
||||
"@lbf-bot/database": "workspace:*",
|
||||
"discord.js": "^14.21.0",
|
||||
"dotenv": "^17.2.3",
|
||||
|
||||
@@ -3,6 +3,9 @@ import { isWovPlayerTracked, untrackWovPlayer } from "~/services/tracking";
|
||||
import { searchPlayer } from "~/services/wov";
|
||||
import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord";
|
||||
import { env } from "~/env";
|
||||
import { createLogger } from "@lbf-bot/utils";
|
||||
|
||||
const trackingLogger = createLogger({ prefix: "tracking" });
|
||||
|
||||
export const tejtrackCommand: Command = async (message, args) => {
|
||||
// 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);
|
||||
if (!chan?.isSendable()) throw "Invalid tracking channel";
|
||||
if (!chan?.isSendable()) {
|
||||
return trackingLogger.fatal("Invalid 'DISCORD_TRACKING_CHANNEL'");
|
||||
}
|
||||
|
||||
await chan.send(
|
||||
createInfoEmbed(
|
||||
|
||||
@@ -3,6 +3,9 @@ import { trackWovPlayer, isWovPlayerTracked } from "~/services/tracking";
|
||||
import { searchPlayer } from "~/services/wov";
|
||||
import { replyError, createInfoEmbed, replySuccess } from "~/utils/discord";
|
||||
import { env } from "~/env";
|
||||
import { createLogger } from "@lbf-bot/utils";
|
||||
|
||||
const trackingLogger = createLogger({ prefix: "tracking" });
|
||||
|
||||
export const trackCommand: Command = async (message, args) => {
|
||||
// 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);
|
||||
if (!chan?.isSendable()) throw "Invalid tracking channel";
|
||||
if (!chan?.isSendable()) {
|
||||
return trackingLogger.fatal("Invalid 'DISCORD_TRACKING_CHANNEL'");
|
||||
}
|
||||
|
||||
await chan.send(
|
||||
createInfoEmbed(`### [NEW] \`${playerName}\` [\`${player.id}\`]`),
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { z } from "zod";
|
||||
import "dotenv/config";
|
||||
import { logger } from "@lbf-bot/utils";
|
||||
|
||||
// TODO: use parseEnv from utils
|
||||
|
||||
const schema = z.object({
|
||||
DISCORD_BOT_TOKEN: z.string(),
|
||||
@@ -29,13 +32,11 @@ const schema = z.object({
|
||||
|
||||
const result = schema.safeParse(process.env);
|
||||
if (!result.success) {
|
||||
console.log("❌ Invalid environments variables:");
|
||||
console.log(
|
||||
result.error.errors
|
||||
logger.fatal(
|
||||
`❌ Invalid environments variables:\n${result.error.errors
|
||||
.map((x) => `- ${x.path.join(".")}: ${x.message}`)
|
||||
.join("\n"),
|
||||
.join("\n")}`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export const env = result.data;
|
||||
|
||||
@@ -4,13 +4,14 @@ import { setupBotMode } from "~/modes/bot";
|
||||
import { setupUserMode } from "~/modes/user";
|
||||
import { parseArgs } from "~/utils/cli";
|
||||
import { runMigrations } from "@lbf-bot/database";
|
||||
import { logger } from "@lbf-bot/utils";
|
||||
|
||||
console.log("Running database migrations...");
|
||||
logger.info("Running database migrations...");
|
||||
await runMigrations();
|
||||
|
||||
const mode = parseArgs(process.argv.slice(2));
|
||||
|
||||
console.log(`Mode: ${mode.type}`);
|
||||
logger.info(`Mode: ${mode.type}`);
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
@@ -35,8 +36,7 @@ switch (mode.type) {
|
||||
|
||||
default: {
|
||||
// @ts-ignore
|
||||
console.error(`ERROR: Not implemented: '${mode.type}'`);
|
||||
process.exit(1);
|
||||
return logger.fatal(`ERROR: Not implemented: '${mode.type}'`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -2,11 +2,15 @@ import { ChannelType, type Client, type Message } from "discord.js";
|
||||
import { env } from "~/env";
|
||||
import { makeResultEmbed } from "~/utils/discord";
|
||||
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) => {
|
||||
const adminChannel = await client.channels.fetch(env.DISCORD_ADMIN_CHANNEL);
|
||||
if (!adminChannel || adminChannel.type !== ChannelType.GuildText)
|
||||
throw "Invalid admin channel provided";
|
||||
if (!adminChannel || adminChannel.type !== ChannelType.GuildText) {
|
||||
return questLogger.fatal("Invalid 'DISCORD_ADMIN_CHANNEL'");
|
||||
}
|
||||
|
||||
const top10 = quest.participants
|
||||
.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
|
||||
.split(",")
|
||||
@@ -107,9 +113,9 @@ export const askForGrinders = async (quest: QuestResult, client: Client) => {
|
||||
if (rewardChannel && rewardChannel.type === ChannelType.GuildText) {
|
||||
await rewardChannel.send(embed);
|
||||
} else {
|
||||
throw "Invalid reward channel";
|
||||
return questLogger.fatal("Invalid 'DISCORD_REWARDS_CHANNEL'");
|
||||
}
|
||||
|
||||
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
3
pnpm-lock.yaml
generated
@@ -17,6 +17,9 @@ importers:
|
||||
'@lbf-bot/database':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/database
|
||||
'@lbf-bot/utils':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/utils
|
||||
discord.js:
|
||||
specifier: ^14.21.0
|
||||
version: 14.25.1
|
||||
|
||||
Reference in New Issue
Block a user