Создание команд в подпапках - PullRequest
1 голос
/ 19 июня 2020

Я пытаюсь сделать так, чтобы я мог добавлять свои команды в подпапки, чтобы лучше организовать их, но при этом команды работали. Я пытался придумать, как сделать так, чтобы мой код был таким, но мой код выглядит совершенно иначе, чем у всех остальных, и я немного застрял.

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

1 Ответ

1 голос
/ 19 июня 2020

Вы можете попробовать следующее:

const client = new Discord.Client();
client.commands = new Discord.Collection();

const folders = fs.readdirSync('./commands'); // read the directory of folders

for (var folder of folders) {
    const files = fs.readdirSync(`./commands/${folder}`); // for each folder, read the files in the folder
    for (var file of files) {
        const command = require(`./commands/${folder}/${file}`); // for each file, set the command
        client.commands.set(command.name, command);
    }
}

Надеюсь, это поможет.

...