Home > Software engineering >  How do I make a Dynamic Help Command with Subfolders and Categories?
How do I make a Dynamic Help Command with Subfolders and Categories?

Time:10-21

So I'm trying to make a dynamic help command that will be able to respond with commands in specific categories. For instance I would like the bot to respond as such when someone uses !help

Pick a category to show their commands:

Cat1

Cat2

Cat3

So if someone decided they wanted to see commands for Cat2, it would then display the list of commands in that specific category.

The information I have found so far on making a dynamic help command involves just displaying every command. How do I go about displaying the commands only in a specified subfolder instead?

CodePudding user response:

Well there's one way for that, if you have a command handler (a way to make it so that uou have a folder with multiple command files and a way to use them on your main file) You could put the different command categories on each file. For example, a file like this :

module.exports = {
  name : "dosomething",
  description : "will do some stuff"
  run : (client,message,args) =>{
  // stuff
}

You could add the category key so that you define each command on your bot on a specific category :

module.exports = {
  name : "dosomething",
  description : "will do some stuff",
  category : "useful"
  run : (client,message,args) =>{
  // stuff
}

This way, every comand has it's own category. On this case you'll have to put the exact same category for every command that belongs on it.

Then, if you want to display them you can use the .filter function on your array of commands so that you only display the commands who belong on a specific category

let args = message.content.split(' ').slice(1)
let category = args[0].toLowerCase() // The first argument on the command will be the category the user asked for

let allCommands = // Here you put an array of all your commands (in most guides, it's client.commands )

let commands = allCommands.filter(command => command.category === category)

if(!commands.length) return message.channel.send(`No command found for \`${category}\``);

message.channel.send(`The commands of the category \`${category}\` are : `  
commands.map(cmd => cmd.name) // We only want to show their names (it's not necessarly '.name', again it depends on your command handler and the key you set to have the value of the name of the command
     .join(' - ') // Since it is an array of command names we will have to convert it into a string to avoid errors.
)

If you want to show each one (for example), you could use .map on the categories themselves to show their own commands

let categories = ["Utility","NSFW","Useful","Image"...]

let embed = new Discord.MessageEmbed()
categories.map(category =>{
  category.name   ' : '   client.commands.filter(c => c.category == category).map(c.name)
})

Note that I don't recommend you using my exact code, I only created them just now so make sure to adapt them to your variables etc..

  • Related