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

时间:2017-12-18 19:46:38

标签: javascript node.js foreach bots discord.js

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

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

然后我尝试发送一条消息,其中包含以下命令的完整列表:

  • 命令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: ""
}

我的代码有点工作,但它只发送第一个命令。

我是javascript的新手,我无法找到解决方案。 我试图谷歌foreach地图discord集合和东西上的一切,但我不能找到一个结果合并在一起的例子。

如果有人可以帮助我或给我一个提示,我可以搜索这样的东西。太棒了。

5 个答案:

答案 0 :(得分:1)

您的代码只发送一个命令的原因是因为您的代码只调用// server const app = require('express')(); const http = require('http').createServer(app); // ^^^^^^^^^^^^ const io = require('socket.io')(http); // here you should serve your index.html file // and the socket io will know which socket to track app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); io.on('connection', function(socket){ console.log('a user connected'); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); // client/index.html <!doctype html> <html> <head> <title>Socket.IO chat</title> <script src="/socket.io/socket.io.js"></script> <script> // here it automatically track localhost:3000 var socket = io(); </script> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> </body> </html> 一次。您使用每个文件中的数据成功设置了变量message.author.send('...'namelistdesclist,但是当usage循环移动到下一个文件时,它会覆盖所有数据

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

.forEach(...

答案 1 :(得分:1)

您应该在Array中执行此操作,它将解决该问题,因此它应该看起来像这样。

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

    });
}

很抱歉,您的回复很晚。

答案 2 :(得分:0)

这是我的看法。。。它对我有用。

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

     
        });
    }

答案 3 :(得分:0)

我使用的是 DiscordJS 12,所以这可能不适用于 11。

const discord = require('discord.js')
module.exports = {
name: 'cmd-list',
async run(client, message, args) {
const commandFiles = readdirSync(join(__dirname, "<your commands folder name>")).filter(file => file.endsWith(".js")); // Get files
const cmdmap = commandFiles.map(files => `${files}`).join(' | Working\n')
const embed = new discord.MessageEmbed()
.setDescription(cmdmap)
message.channel.send(embed)
}
}

答案 4 :(得分:0)

你可以像这样使用 discord 的普通嵌入系统:

const { Message, MessageEmbed } = require('discord.js');

将此添加到您的代码的顶部,然后键入以下所有内容以制作像这样的普通嵌入:

const embed = new MessageEmbed()
     .setTitle(`Your title!`)
     .setColor(5814783)
     .setTimestamp()
     .setThumbnail('')
     .setDescription(`Your description`)
     .addFields(
     { name: 'Field 1', value: `Filed 1 Value`},
     { name: 'Field 2', value: `Filed 2 Value` },
     { name: 'Field 3', value: `Filed 3 Value`, inline: true },
     { name: 'Field 4', value: `Filed 4 Value`, inline: true },
     { name: 'Field 5', value: `Field 5 Value`, inline: true },
     )
message.channel.send(embed2)