Discord bot help command with Command Handler

Asked

Viewed 878 times

-5

It is possible to make a help command where it looks for an element of the module.exports

guy... module.exports = { name: 'help', description: '...', execute(message, args){ }

and within the execute(message, args){ (aqui) } fetch the name of module.exports of all other command files in the Commands folder including that file and merge them into one Embed?

Remembering q I’m using an Handler command with Fs...

const { prefix, token } = require('./config.json');
const client = new Discord.Client();

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for(const file of commandsFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}


client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    } else if (command == 'help'){
        client.commands.get('help').execute(message, args);
    }
});

client.login(token);```

e a base de comando é 
```module.exports = {
    name: 'help',
    description: "This is a Help command!",
    execute(message, args){
        



    }
}```
  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

1

confused and unspecific, but I think I understand... That doesn’t sound very safe, but...

you can list the files and load them into an object...

const fs = require('fs'), path = require('path')
var comandos = {};
fs.readdirSync("./comandos").forEach(file => {
 file = "./comandos"+path.sep+file;
 if (!fs.lstatSync(file).isDirectory()) {
  var comando = require(file);
  comandos[comando.name] = comando;
 }
});

Then you could check if the command exists and call with:

if (comandos[name])
 comandos[name].execute(...args);
else
 responder(`${name} não é um comando válido`);

and for the help function, use the getOwnPropertyNames:

var helpstring = 'Comandos:\n'+Object.getOwnPropertyNames(comandos)
 .map((name)=>` - ${name} - ${comandos[name].description}`)
 .join('\n');

for the command object to be accessible from any file:

module.exports = comandos;
var comandos = require('../comandos.js');

or with global although not recommended, would dispense with the require()

global.comandos = comandos
comandos...

(... and I didn’t see anything specific from the Discord.js api on the issue)

  • So I’m doing a Command Handler, and I think module.exports = comandos;
var comandos = require('../comandos.js'); does not work with command Handler pq and a file . js for each command inside the command folder

  • the first line is an example to export, and the second to import, the files loaded by require are once cached, so it is no problem to keep calling the file with the list repeatedly for the @Pulse ... 'performance. /' is to access the top folder, this require was an example, from the 'help' command calling it, <commands.js><main.js>[folder commands/ <help.js>]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.