discord.js列出了我所有的机器人命令 [英] discord.js list all my bot commands

查看:71
本文介绍了discord.js列出了我所有的机器人命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用discord.js制作了一个discord机器人,并尝试执行帮助命令以向用户显示所有可用命令.

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

示例命令: 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:

  • 命令1
  • 说明
  • 用法
  • 命令2
  • 说明
  • 用法
  • ...

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.

我对javascript很陌生,我找不到解决方案.我试图用Google搜索foreach地图上的不和谐集合和内容,但是找不到一个将结果组合在一起的示例.

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.

推荐答案

您的代码仅发送一个命令的原因是因为您的代码仅调用 message.author.send('...'一次.您成功设置了变量 namelist desclist usage 以及来自每个文件的数据,但您的 .forEach(... 循环只会在移至下一个文件时覆盖所有数据.

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.

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

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

这篇关于discord.js列出了我所有的机器人命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆