diff --git a/src/managers/commandManager.ts b/src/managers/commandManager.ts index 2d8bb6d..fe5d320 100644 --- a/src/managers/commandManager.ts +++ b/src/managers/commandManager.ts @@ -66,32 +66,35 @@ class CommandManager { const files: string[] = await fs.promises.readdir(commandsDir) for (const file of files) { - await this.loadCommand(commandsDir, file) + try { + const extension = config.ENV === 'development' ? '.ts' : '.js' + const commandName = path.basename(file, extension) + const commandPath = path.join(commandsDir, `${commandName}${extension}`) + + if (!fs.existsSync(commandPath)) { + commandLogger.warn(`Command file not found: ${commandPath}`) + continue + } + + // Use dynamic import + const CommandModule = await import(commandPath) + const CommandClass = CommandModule.default + + if (!CommandClass || typeof CommandClass !== 'function') { + commandLogger.warn(`Invalid command class in file: ${commandPath}`) + continue + } + + this.registerCommand(commandName, CommandClass) + } catch (error) { + commandLogger.error(`Failed to load command: ${file}: ${error}`) + } } } catch (error) { commandLogger.error(`Failed to read commands directory: ${error}`) } } - private async loadCommand(commandsDir: string, file: string) { - try { - const extension = config.ENV === 'development' ? '.ts' : '.js' - const commandName = path.basename(file, extension) - const commandPath = path.join(commandsDir, file) - - // Use dynamic import - const module = await import(commandPath) - - if (typeof module.default === 'function') { - this.registerCommand(commandName, module.default) - } else { - commandLogger.warn(`Unrecognized export in ${file}`) - } - } catch (error) { - commandLogger.error(`Failed to load command: ${file}: ${error}`) - } - } - private registerCommand(name: string, CommandClass: any) { if (this.commands.has(name)) { commandLogger.warn(`Command '${name}' is already registered. Overwriting...`)