From 05e83b5e14203bf08463e8636ffc12821df94ece Mon Sep 17 00:00:00 2001 From: shni Date: Sat, 27 Sep 2025 16:05:40 -0500 Subject: [PATCH] feat: improve component and command loading functions with directory existence checks --- src/core/lib/components.ts | 9 ++++++++- src/core/loaders/loader.ts | 11 +++++++++-- src/core/loaders/loaderEvents.ts | 8 +++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/core/lib/components.ts b/src/core/lib/components.ts index f59e8ef..de885aa 100644 --- a/src/core/lib/components.ts +++ b/src/core/lib/components.ts @@ -8,7 +8,13 @@ export const modals: Collection = new Collection() export const selectmenus: Collection = new Collection(); export const contextmenus: Collection = new Collection(); -export function loadComponents(dir: string = path.join(__dirname, "..", "components")) { +export function loadComponents(dir: string = path.resolve(__dirname, "../../components")) { + // Evitar fallo si el directorio no existe en el entorno + if (!fs.existsSync(dir)) { + console.warn(`⚠️ Directorio de componentes no encontrado: ${dir}`); + return; + } + const files = fs.readdirSync(dir); for (const file of files) { @@ -21,6 +27,7 @@ export function loadComponents(dir: string = path.join(__dirname, "..", "compone } if (!file.endsWith(".ts") && !file.endsWith(".js")) continue; + if (file.endsWith('.d.ts')) continue; try { const imported = require(fullPath); diff --git a/src/core/loaders/loader.ts b/src/core/loaders/loader.ts index 23e3284..e434621 100644 --- a/src/core/loaders/loader.ts +++ b/src/core/loaders/loader.ts @@ -4,7 +4,13 @@ import { Collection } from "discord.js"; export const commands = new Collection(); -export function loadCommands(dir: string = path.join(__dirname, '..', 'commands')) { +export function loadCommands(dir: string = path.resolve(__dirname, "../../commands")) { + // Evitar fallo si el directorio no existe en el entorno + if (!fs.existsSync(dir)) { + console.warn(`⚠️ Directorio de comandos no encontrado: ${dir}`); + return; + } + const files = fs.readdirSync(dir); for (const file of files) { @@ -16,7 +22,8 @@ export function loadCommands(dir: string = path.join(__dirname, '..', 'commands' continue; } - if (!file.endsWith('.ts')) continue; + if (!file.endsWith('.ts') && !file.endsWith('.js')) continue; + if (file.endsWith('.d.ts')) continue; const imported = require(fullPath); const command = imported.command ?? imported.default ?? imported; diff --git a/src/core/loaders/loaderEvents.ts b/src/core/loaders/loaderEvents.ts index cf038aa..da7c9e8 100644 --- a/src/core/loaders/loaderEvents.ts +++ b/src/core/loaders/loaderEvents.ts @@ -2,7 +2,13 @@ import { bot } from "../../main"; import path from "node:path"; import * as fs from "node:fs"; -export function loadEvents(dir: string = path.join(__dirname, "../events")) { +export function loadEvents(dir: string = path.resolve(__dirname, "../../events")) { + // Evitar fallo si el directorio no existe + if (!fs.existsSync(dir)) { + console.warn(`⚠️ Directorio de eventos no encontrado: ${dir}`); + return; + } + const files = fs.readdirSync(dir); for (const file of files) {