From 3638e2a7939a66e286aa49463289ba6cedb975f2 Mon Sep 17 00:00:00 2001
From: Dennis Postma <dennis@directonline.io>
Date: Mon, 30 Sep 2024 22:39:48 +0200
Subject: [PATCH] Fixed oopsies

---
 src/managers/commandManager.ts | 43 ++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 20 deletions(-)

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