Command Handler
Guide on how to organize your commands in folders (Command Handler)
Step 1
Step 2
Step 3
Insert the following code in your main file (server.js) or (index.js) etc.
1
const fs = require('fs');
2
const folders = fs.readdirSync("./commands/")
3
4
for (const files of folders) {
5
const folder = fs.readdirSync(`./commands/${files}/`).filter(file => file.endsWith(".js"))
6
7
for (const commands of folder) {
8
const command = require(`./commands/${files}/${commands}`)
9
bot.Command(command);
10
console.log(`Loaded: ${command.name} | ${folders} folder - [${command.aliases}]`);
11
}
12
}
Copied!

Options to use to log in the console upon rebooting the bot:

You can choose to log in the console all your commands and file locations and even their aliases. By adding the console.log() to the command handler, you can do this upon every bot reboot.

I've supplied you a decent default format in this updated version of the handler above.

${folder} = File name with extension ${folders} = Folder name ${command.name} = command name ${command.aliases} = all the aliases of that command. Returns 'undefined' if no aliases are found
1: In the main directory, make a new folder called commands .
2: Inside the commandsfolder, make another folder with a name of your choice. Normally a name that helps you organize your commands how you choose.
3: Inside that folder will be all the command files related to that category.
The folder path should look something like this: Keep in mind though, I'm using Visual Studio Code (VSC) so your theme might look a little different than mine but it's the exact same thing.
1
commands/subfoldername/name.js
Copied!
Use the following format inside each command file:
You're replacing bot.Command({with module.exports = ({ in your command files.
1
module.exports = ({
2
name: "name",
3
code: `
4
Code Here
5
`
6
})
Copied!

You can now move all the commands that you have in the main file, into their own respective files. It should look similar to the images in step 2 and step 3.

Last modified 5mo ago
Copy link