Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

node.js - Discord.js bots: organizing commands

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

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

1 Reply

0 votes
by (71.8m points)

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);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...