discord.js list all my bot commands

一个人想着一个人 提交于 2021-01-25 04:34:31

问题


i made a discord bot with discord.js and tried to do a help command to show the user all available commands.

example command: avatar.js

module.exports.run = async(bot, message, args) => {
    let msg = await message.channel.send("doing some magic ...");
    let target = message.mentions.users.first() || message.author;

    await message.channel.send({files: [
        {
            attachment: target.displayAvatarURL,
            name: "avatar.png"
        }
    ]});

    msg.delete();
}

module.exports.help = {
    name: "avatar",
    description: "show the avatar of a user",
    usage: "[@user]"
}

Then i tried to send a message with the complete list of the commands like:

  • command 1
  • description
  • usage
  • command 2
  • description
  • usage
  • ...

help.js

const fs = require("fs");
const Discord = require("discord.js");

module.exports.run = async(bot, message, args, con) => {
    fs.readdir("./cmds/", (err, files) => {
        if(err) console.error(err);

        let jsfiles = files.filter(f => f.split(".").pop() === "js");
        if(jsfiles.length <= 0) {
            console.log("No commands to load!");
            return;
        }

        var namelist = "";
        var desclist = "";
        var usage = "";

        let result = jsfiles.forEach((f, i) => {
            let props = require(`./${f}`);
            namelist = props.help.name;
            desclist = props.help.description;
            usage = props.help.usage;
        });

        message.author.send(`**${namelist}** \n${desclist} \n${usage}`);
    });
}

module.exports.help = {
    name: "help",
    description: "show all commands",
    usage: ""
}

my code is kinda working but it only sends the first command.

Im pretty new to javascript and i can't find a solution to this. I tried to google everything on foreach maps discord collections and stuff but i cant find a example where the results get combined together.

If anybody can help me or give me a hint where i can search for something like this. Would be awesome.


回答1:


The reason your code is only sending the one command is because your code only calls message.author.send('...' once. You successfully set the variables namelist, desclist, and usage with data from every file, but your .forEach(... loop just overwrites all of the data when it moves to the next files.

Try to send data inside each iteration of the .forEach(... loop like this:

var namelist = "";
var desclist = "";
var usage = "";

let result = jsfiles.forEach((f, i) => {
    let props = require(`./${f}`);
    namelist = props.help.name;
    desclist = props.help.description;
    usage = props.help.usage;

    // send help text
    message.author.send(`**${namelist}** \n${desclist} \n${usage}`);
});



回答2:


you should do this in an Array and it'll solve the problem, so it should look like this.

module.exports.run = async(bot, message, args, con) => {
    fs.readdir("./cmds/", (err, files) => {
        if(err) console.error(err);

        let jsfiles = files.filter(f => f.split(".").pop() === "js");
        if(jsfiles.length <= 0) {
            console.log("No commands to load!");
            return;
        }


        let result = jsfiles.forEach((f, i) => {
            let props = require(`./${f}`);
            let filesArray = [props.help.name, props.help.description, props.help.usage]
            message.author.send(`**${filesArray[0]}** \n${filesArray[1]} \n${filesArray[2]}`);
        });

    });
}

sorry for the late response.




回答3:


Here is my take on this.... it works for me.

const Discord = require('discord.js'); 
const fs = require("fs");

module.exports = {
    name: 'help',
    description: 'Lists available commands',

    async run(client, message, args, con) {
        fs.readdir("./commands/", (err, files) => {
            if(err) console.error(err);
    
            let jsfiles = files.filter(f => f.split(".").pop() === "js");
            if(jsfiles.length <= 0) {
                console.log("No commands to load!");
                return;
            }
            
            var namelist = "";
            var desclist = "";
            
            let result = jsfiles.forEach((f, i) => {
                let props = require(`./${f}`);
                namelist = props.name;
                desclist = props.description;
                message.author.send(`**${namelist}** \n${desclist} \n`);
            });
     

     
        });
    }


来源:https://stackoverflow.com/questions/47875397/discord-js-list-all-my-bot-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!