1
0
forked from noxious/server

Fixed oopsies

This commit is contained in:
Dennis Postma 2024-09-30 22:39:48 +02:00
parent 6ac827630a
commit 3638e2a793

View File

@ -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...`)