ETLegacy
ETLegacy

Reputation: 35

How to fetch load my commands folder and its sub-directories?

I'm writing a bot using NodeJS, and I'm pretty stuck. I'm attempting on creating a help command for the bot, and my folders look something like this: commands/slash and after the /slash I have my commands categorised, so like Moderation commands are in the Moderation folder, and so on..

so this is my current code:

import Command from "../../../structures/Command.js"
import { Embed } from "eris-addons"
import fs from "fs"
import { readdirSync } from "fs"
import path from "path"
import { join } from "path"

import { fileURLToPath } from "url"

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Map of category names to command objects
const categories = new Map();

// Load commands from the "commands/slash" folder and its subdirectories
const commandFolders = readdirSync(path.join(__dirname, '../'));
for (const folder of commandFolders) {
    const fullPath = path.join(__dirname, '../', folder);
    if (fs.lstatSync(fullPath).isDirectory()) {
        const commandFiles = fs.readdirSync(fullPath).filter((file) => file.endsWith('.js'));
        for (const file of commandFiles) {
            const filePath = path.join(fullPath, file);
            const { default: CommandClass } = await import(filePath);
            const command = new CommandClass();
            if (!categories.has(folder)) {
                categories.set(folder, []);
            }
            categories.get(folder).push(command);
        }
    }
}

and my error is Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

coming from const { default: CommandClass } = await import(filePath);

how can i fix this?

Upvotes: 0

Views: 88

Answers (0)

Related Questions