Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi i was making my first bot using discord.js and i noticed after added few commands that my commands folder will get flooded easily I'm using a simple command handler from Discord.js Guide https://discordjs.guide/command-handling/ however i'll need slot of files just to make simple commands ii wanted to ask people who already had experience in this stuff on how to organize my commands

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
313 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to better organize your command files, you can separate the commands with categories and then create a folder for each category inside the commands folder.

for example:

??commands
 ┣ ??moderation
 ┗ ??fun

After that you can loop through each of these new folders and load the command files inside them the same way you've done with your command handler

// First get the category directories
const isDirectory = source => fs.lstatSync(source).isDirectory();
const getDirectories = source => fs.readdirSync(source).map(name => join(source, name)).filter(isDirectory);

// Then load the commands
getDirectories(__dirname + '/commands').forEach(category => {
  const commandFiles = fs.readdirSync(category).filter(file => file.endsWith('.js'));

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...