From fa95596906f9806421ac580f671593a31a91c602 Mon Sep 17 00:00:00 2001 From: Joan Marin Date: Sun, 2 Apr 2023 22:50:10 -0400 Subject: [PATCH] Async loading of commands and events --- index.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 0e2cacbd..5614744d 100644 --- a/index.js +++ b/index.js @@ -41,11 +41,13 @@ async function loadCommandFromDir(dir) { for (const file of commandFiles) { const filePath = path.join(commandsPath, file); - let command = await import(filePath); - command = command.default; - - client.commands.set(command.data.name, command); - console.log(`Successfully loaded command \x1b[32m${command.category}/${command.data.name}\x1b[0m`); + import(filePath) + .then(importedCommand => { + const command = importedCommand.default; + client.commands.set(command.data.name, command); + console.log(`Successfully loaded command \x1b[32m${command.category}/${command.data.name}\x1b[0m`); + }) + .catch(error => console.error(`Failed to load command for path: ${filePath}`, error)); } } @@ -55,13 +57,16 @@ async function loadEventFromDir(dir, listener) { for (const file of eventFiles) { const filePath = path.join(eventsPath, file); - let event = await import(filePath); - event = event.default; - if (event.once) { - listener.once(event.name, (...args) => event.execute(...args, client)); - } - else { - listener.on(event.name, (...args) => event.execute(...args, client)); - } + import(filePath) + .then(importedEvent => { + const event = importedEvent.default; + if (event.once) { + listener.once(event.name, (...args) => event.execute(...args, client)); + } + else { + listener.on(event.name, (...args) => event.execute(...args, client)); + } + }) + .catch(error => console.error(`Failed to load event for path: ${filePath}`, error)); } }