javascript discord.js 列出我所有的机器人命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47875397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:39:05  来源:igfitidea点击:

discord.js list all my bot commands

javascriptnode.jsforeachbotsdiscord.js

提问by creme

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

我用 discord.js 制作了一个 discord bot,并尝试执行一个帮助命令来向用户显示所有可用的命令。

example command: avatar.js

示例命令: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
  • ...
  • 命令 1
  • 描述
  • 用法
  • 命令 2
  • 描述
  • 用法
  • ...

help.js

帮助.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.

我对 javascript 很陌生,我找不到解决方案。我试图在 foreach 地图上搜索所有不和谐的集合和东西,但我找不到结果组合在一起的例子。

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

如果有人可以帮助我或给我一个提示,我可以在哪里搜索这样的东西。会很棒。

回答by Blundering Philosopher

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 usagewith data from every file, but your .forEach(...loop just overwrites all of the data when it moves to the next files.

您的代码仅发送一个命令的原因是您的代码仅调用message.author.send('...'一次。您成功地使用每个文件中的数据设置了变量namelistdesclistusage,但是您的.forEach(...循环在移动到下一个文件时只会覆盖所有数据。

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

尝试在.forEach(...循环的每次迭代中发送数据,如下所示:

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

回答by tomjame553

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.

回复晚了非常抱歉。