Reputation: 59
So Im pretty new to JS and Id like to have multiple folders for different commands. I only have one file for commands called "befehle". This is the current command handler for me.
const { readdirSync } = require("fs")
const ascii = require("ascii-table")
const table = new ascii().setHeading("Command", "Load Status");
module.exports = (client) => {
const commands = readdirSync(`./befehle/`).filter(f => f.endsWith(".js"));
for (let file of commands) {
let pull = require(`../befehle/${file}`);
if (pull.name) {
client.commands.set(pull.name, pull);
table.addRow(file, "✔")
} else {
table.addRow(file, '❌ Not working');
continue;
}
if (pull.aliases && Array.isArray(pull.aliases))
pull.aliases.forEach(alias => client.aliases.set(alias, pull.name));
}
console.log(table.toString())
}
I really have no idea how to have multiple folders, can anyone take a look please?
Upvotes: 1
Views: 189
Reputation: 1880
At first, I'm sorry for not correcting your code, but this my suggestion for you:
Before I show you my command handler, let's take a look at how your folder structure should be for this:
commands
│
├──admin-commands
│ ban.js
│ kick.js
│
├──fun-commands
│ ping.js
│ meme.js
│
└──misc
test.js
(File names are just examples)
Now that you have structured your commands folder like this, you can use this as your command handler:
const fs = require('fs');
const path = require('path');
const rootDir = path.dirname(require.main.filename);
module.exports = (client, Discord) => {
const fileArray = [];
function readCommands(dir) {
const __dirname = rootDir;
// Read out all command files
const files = fs.readdirSync(path.join(__dirname, dir));
// Loop through all the files in ./commands
for (const file of files) {
// Get the status of 'file' (is it a file or directory?)
const stat = fs.lstatSync(path.join(__dirname, dir, file));
// If the 'file' is a directory, call the 'readCommands' function
// again with the path of the subdirectory
if (stat.isDirectory()) {
readCommands(path.join(dir, file));
}
else {
const fileDir = dir.replace('\\', '/');
fileArray.push(fileDir + '/' + file);
// fs.readdirSync(dir).filter(cmdFile => cmdFile.endsWith('.js'));
}
}
}
};
I think the comments are explaining the process pretty well. But let's move on!
After you pasted that into your project you still need to call the readCommands()
function...
readCommands('commands');
for(const file of fileArray) {
const command = require(`../${file}`);
if(command.name) {
client.commands.set(command.name, command);
}
}
The parameter commands
is the name of your root command folder. So if you decide to give it another name, e.g. 'commandFolder', you need to pass that name as parameter!
In the command handler code, rigth underneath the module.exports
I'm creating an array called fileArray
. This stores all your command files the code above has found. Now we loop through this array, import the command and check if it's empty or not.
If it's not empty, we'll set this command to client.commands
, so you can get it later in your code, when you need to execute it.
...you can paste this into the same file as the 'main code' of the command handler, so that both code snippets are in the same file. Also make sure to paste this inside of the module.exports
block, not outside of it ;)
Upvotes: 2