refactor: replace console.* with logger in core lib files

Update loaders, handlers, and BotClient to use centralized logger:
- CommandLoader.ts and EventLoader.ts
- AutocompleteHandler.ts, CommandHandler.ts, ComponentInteractionHandler.ts
- BotClient.ts

Provides consistent formatting across all core library logging.
This commit is contained in:
syntaxbullet
2025-12-24 21:56:50 +01:00
parent a53d30a0b3
commit 10a760edf4
6 changed files with 34 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ import type { Command } from "@lib/types";
import { env } from "@lib/env"; import { env } from "@lib/env";
import { CommandLoader } from "@lib/loaders/CommandLoader"; import { CommandLoader } from "@lib/loaders/CommandLoader";
import { EventLoader } from "@lib/loaders/EventLoader"; import { EventLoader } from "@lib/loaders/EventLoader";
import { logger } from "@lib/logger";
export class Client extends DiscordClient { export class Client extends DiscordClient {
@@ -21,25 +22,25 @@ export class Client extends DiscordClient {
async loadCommands(reload: boolean = false) { async loadCommands(reload: boolean = false) {
if (reload) { if (reload) {
this.commands.clear(); this.commands.clear();
console.log("♻️ Reloading commands..."); logger.info("♻️ Reloading commands...");
} }
const commandsPath = join(import.meta.dir, '../commands'); const commandsPath = join(import.meta.dir, '../commands');
const result = await this.commandLoader.loadFromDirectory(commandsPath, reload); const result = await this.commandLoader.loadFromDirectory(commandsPath, reload);
console.log(`📦 Command loading complete: ${result.loaded} loaded, ${result.skipped} skipped, ${result.errors.length} errors`); logger.info(`📦 Command loading complete: ${result.loaded} loaded, ${result.skipped} skipped, ${result.errors.length} errors`);
} }
async loadEvents(reload: boolean = false) { async loadEvents(reload: boolean = false) {
if (reload) { if (reload) {
this.removeAllListeners(); this.removeAllListeners();
console.log("♻️ Reloading events..."); logger.info("♻️ Reloading events...");
} }
const eventsPath = join(import.meta.dir, '../events'); const eventsPath = join(import.meta.dir, '../events');
const result = await this.eventLoader.loadFromDirectory(eventsPath, reload); const result = await this.eventLoader.loadFromDirectory(eventsPath, reload);
console.log(`📦 Event loading complete: ${result.loaded} loaded, ${result.skipped} skipped, ${result.errors.length} errors`); logger.info(`📦 Event loading complete: ${result.loaded} loaded, ${result.skipped} skipped, ${result.errors.length} errors`);
} }
@@ -48,7 +49,7 @@ export class Client extends DiscordClient {
// We use env.DISCORD_BOT_TOKEN directly so this can run without client.login() // We use env.DISCORD_BOT_TOKEN directly so this can run without client.login()
const token = env.DISCORD_BOT_TOKEN; const token = env.DISCORD_BOT_TOKEN;
if (!token) { if (!token) {
console.error("DISCORD_BOT_TOKEN is not set."); logger.error("DISCORD_BOT_TOKEN is not set.");
return; return;
} }
@@ -58,16 +59,16 @@ export class Client extends DiscordClient {
const clientId = env.DISCORD_CLIENT_ID; const clientId = env.DISCORD_CLIENT_ID;
if (!clientId) { if (!clientId) {
console.error("DISCORD_CLIENT_ID is not set."); logger.error("DISCORD_CLIENT_ID is not set.");
return; return;
} }
try { try {
console.log(`Started refreshing ${commandsData.length} application (/) commands.`); logger.info(`Started refreshing ${commandsData.length} application (/) commands.`);
let data; let data;
if (guildId) { if (guildId) {
console.log(`Registering commands to guild: ${guildId}`); logger.info(`Registering commands to guild: ${guildId}`);
data = await rest.put( data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId), Routes.applicationGuildCommands(clientId, guildId),
{ body: commandsData }, { body: commandsData },
@@ -75,20 +76,20 @@ export class Client extends DiscordClient {
// Clear global commands to avoid duplicates // Clear global commands to avoid duplicates
await rest.put(Routes.applicationCommands(clientId), { body: [] }); await rest.put(Routes.applicationCommands(clientId), { body: [] });
} else { } else {
console.log('Registering commands globally'); logger.info('Registering commands globally');
data = await rest.put( data = await rest.put(
Routes.applicationCommands(clientId), Routes.applicationCommands(clientId),
{ body: commandsData }, { body: commandsData },
); );
} }
console.log(`Successfully reloaded ${(data as any).length} application (/) commands.`); logger.success(`Successfully reloaded ${(data as any).length} application (/) commands.`);
} catch (error: any) { } catch (error: any) {
if (error.code === 50001) { if (error.code === 50001) {
console.warn("⚠️ Missing Access: The bot is not in the guild or lacks 'applications.commands' scope."); logger.warn("Missing Access: The bot is not in the guild or lacks 'applications.commands' scope.");
console.warn(" If you are testing locally, make sure you invited the bot with scope 'bot applications.commands'."); logger.warn(" If you are testing locally, make sure you invited the bot with scope 'bot applications.commands'.");
} else { } else {
console.error(error); logger.error(error);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
import { AutocompleteInteraction } from "discord.js"; import { AutocompleteInteraction } from "discord.js";
import { AuroraClient } from "@/lib/BotClient"; import { AuroraClient } from "@/lib/BotClient";
import { logger } from "@lib/logger";
/** /**
* Handles autocomplete interactions for slash commands * Handles autocomplete interactions for slash commands
@@ -15,7 +16,7 @@ export class AutocompleteHandler {
try { try {
await command.autocomplete(interaction); await command.autocomplete(interaction);
} catch (error) { } catch (error) {
console.error(`Error handling autocomplete for ${interaction.commandName}:`, error); logger.error(`Error handling autocomplete for ${interaction.commandName}:`, error);
} }
} }
} }

View File

@@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, MessageFlags } from "discord.js";
import { AuroraClient } from "@/lib/BotClient"; import { AuroraClient } from "@/lib/BotClient";
import { userService } from "@/modules/user/user.service"; import { userService } from "@/modules/user/user.service";
import { createErrorEmbed } from "@lib/embeds"; import { createErrorEmbed } from "@lib/embeds";
import { logger } from "@lib/logger";
/** /**
* Handles slash command execution * Handles slash command execution
@@ -12,7 +13,7 @@ export class CommandHandler {
const command = AuroraClient.commands.get(interaction.commandName); const command = AuroraClient.commands.get(interaction.commandName);
if (!command) { if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`); logger.error(`No command matching ${interaction.commandName} was found.`);
return; return;
} }
@@ -20,13 +21,13 @@ export class CommandHandler {
try { try {
await userService.getOrCreateUser(interaction.user.id, interaction.user.username); await userService.getOrCreateUser(interaction.user.id, interaction.user.username);
} catch (error) { } catch (error) {
console.error("Failed to ensure user exists:", error); logger.error("Failed to ensure user exists:", error);
} }
try { try {
await command.execute(interaction); await command.execute(interaction);
} catch (error) { } catch (error) {
console.error(error); logger.error(String(error));
const errorEmbed = createErrorEmbed('There was an error while executing this command!'); const errorEmbed = createErrorEmbed('There was an error while executing this command!');
if (interaction.replied || interaction.deferred) { if (interaction.replied || interaction.deferred) {

View File

@@ -1,4 +1,5 @@
import { ButtonInteraction, StringSelectMenuInteraction, ModalSubmitInteraction } from "discord.js"; import { ButtonInteraction, StringSelectMenuInteraction, ModalSubmitInteraction } from "discord.js";
import { logger } from "@lib/logger";
type ComponentInteraction = ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction; type ComponentInteraction = ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction;
@@ -19,7 +20,7 @@ export class ComponentInteractionHandler {
await handlerMethod(interaction); await handlerMethod(interaction);
return; return;
} else { } else {
console.error(`Handler method ${route.method} not found in module`); logger.error(`Handler method ${route.method} not found in module`);
} }
} }
} }

View File

@@ -4,6 +4,7 @@ import type { Command } from "@lib/types";
import { config } from "@lib/config"; import { config } from "@lib/config";
import type { LoadResult, LoadError } from "./types"; import type { LoadResult, LoadError } from "./types";
import type { Client } from "../BotClient"; import type { Client } from "../BotClient";
import { logger } from "@lib/logger";
/** /**
* Handles loading commands from the file system * Handles loading commands from the file system
@@ -44,7 +45,7 @@ export class CommandLoader {
await this.loadCommandFile(filePath, reload, result); await this.loadCommandFile(filePath, reload, result);
} }
} catch (error) { } catch (error) {
console.error(`Error reading directory ${dir}:`, error); logger.error(`Error reading directory ${dir}:`, error);
result.errors.push({ file: dir, error }); result.errors.push({ file: dir, error });
} }
} }
@@ -59,7 +60,7 @@ export class CommandLoader {
const commands = Object.values(commandModule); const commands = Object.values(commandModule);
if (commands.length === 0) { if (commands.length === 0) {
console.warn(`⚠️ No commands found in ${filePath}`); logger.warn(`No commands found in ${filePath}`);
result.skipped++; result.skipped++;
return; return;
} }
@@ -73,21 +74,21 @@ export class CommandLoader {
const isEnabled = config.commands[command.data.name] !== false; const isEnabled = config.commands[command.data.name] !== false;
if (!isEnabled) { if (!isEnabled) {
console.log(`🚫 Skipping disabled command: ${command.data.name}`); logger.info(`🚫 Skipping disabled command: ${command.data.name}`);
result.skipped++; result.skipped++;
continue; continue;
} }
this.client.commands.set(command.data.name, command); this.client.commands.set(command.data.name, command);
console.log(`Loaded command: ${command.data.name}`); logger.success(`Loaded command: ${command.data.name}`);
result.loaded++; result.loaded++;
} else { } else {
console.warn(`⚠️ Skipping invalid command in ${filePath}`); logger.warn(`Skipping invalid command in ${filePath}`);
result.skipped++; result.skipped++;
} }
} }
} catch (error) { } catch (error) {
console.error(`Failed to load command from ${filePath}:`, error); logger.error(`Failed to load command from ${filePath}:`, error);
result.errors.push({ file: filePath, error }); result.errors.push({ file: filePath, error });
} }
} }

View File

@@ -3,6 +3,7 @@ import { join } from "node:path";
import type { Event } from "@lib/types"; import type { Event } from "@lib/types";
import type { LoadResult } from "./types"; import type { LoadResult } from "./types";
import type { Client } from "../BotClient"; import type { Client } from "../BotClient";
import { logger } from "@lib/logger";
/** /**
* Handles loading events from the file system * Handles loading events from the file system
@@ -43,7 +44,7 @@ export class EventLoader {
await this.loadEventFile(filePath, reload, result); await this.loadEventFile(filePath, reload, result);
} }
} catch (error) { } catch (error) {
console.error(`Error reading directory ${dir}:`, error); logger.error(`Error reading directory ${dir}:`, error);
result.errors.push({ file: dir, error }); result.errors.push({ file: dir, error });
} }
} }
@@ -63,14 +64,14 @@ export class EventLoader {
} else { } else {
this.client.on(event.name, (...args) => event.execute(...args)); this.client.on(event.name, (...args) => event.execute(...args));
} }
console.log(`Loaded event: ${event.name}`); logger.success(`Loaded event: ${event.name}`);
result.loaded++; result.loaded++;
} else { } else {
console.warn(`⚠️ Skipping invalid event in ${filePath}`); logger.warn(`Skipping invalid event in ${filePath}`);
result.skipped++; result.skipped++;
} }
} catch (error) { } catch (error) {
console.error(`Failed to load event from ${filePath}:`, error); logger.error(`Failed to load event from ${filePath}:`, error);
result.errors.push({ file: filePath, error }); result.errors.push({ file: filePath, error });
} }
} }