javascript - Discord 帮助命令 - args.join 不是函数

标签 javascript node.js discord discord.js

通过我的不和谐机器人,我正在处理帮助命令。 我的帮助命令访问的命令列表文件是:

{
    "Help": {
        "name":"Help",
        "group":"User",
        "desc":"Displays a list of commands",
        "usage":"help [group OR command]"
    },

    "Purge": {
        "name":"Purge",
        "group":"Admin",
        "desc":"Deletes a specified number of messages",
        "usage":"Purge <amount>"
    }
}

这些仅定义命令的组、名称和用法。到目前为止帮助命令的代码是:

const Discord = require('discord.js');
const bot = new Discord.Client();
const client = new Discord.Client();
const weather = require('weather-js');
const fs = require('fs');
const commands = JSON.parse(fs.readFileSync('Storage/commands.json', 'utf8'))
const token = "<my token>"
const prefix = 'cb!';

bot.on('message', message => {

    // Variables
    let msg = message.content.toUpperCase();
    let sender = message.author;
    let cont = message.content.slice(prefix.length).split(" ");
    let args = cont.shift().toLowerCase();

    if (message.content.startsWith(prefix+'help')) {

            console.log('ok i hate this')

            const embed = new Discord.RichEmbed()
                .setColor(0x1D82B6)



            let commandsFound = 0;

            for (var cmd in commands) {

                if (commands[cmd].group.toUpperCase() === 'USER') {
                    commandsFound++

                    embed.addField(`${commands[cmd].name}`, `**Description:** ${commands[cmd].desc}\n**Usage:** ${prefix + commands[cmd].usage}`);
                }


            }

            embed.setFooter(`Currently showing user commands. To view another group do ${prefix}help [group / command]`)
            embed.setDescription(`**${commandsFound} commands found** - <> means required, [] means optional`)

            message.author.send({embed})
            message.channel.send({embed: {
                color: 0x1D82B6,
                description: `**Check your DMs ${message.author}!**`
            }})

            } else {
                // Variables
                let groupFound = '';

                for (var cmd in commands) {

                    if (args.join(" ").trim().toUpperCase() === commands[cmd].group.toUpperCase()) {
                        groupFound = commands[cmd].group.toUpperCase();
                        break;
                    }

                }

                if (groupFound != '') {
                    for (var cmd in commands) {

                        const embed = new Discord.RichEmbed()
                        .setColor(0x1D82B6)



                        let commandsFound = 0;

                        if (commands[cmd].group.toUpperCase() === groupFound) {
                            commandsFound++

                            embed.addField(`${commands[cmd].name}`, `**Description:** ${commands[cmd].desc}\n**Usage:** ${prefix + commands[cmd].usage}`);
                        }


                    }

                    embed.setFooter(`Currently showing ${groupFound} commands. To view another group do ${prefix}help [group / command]`)
                    embed.setDescription(`**${commandsFound} commands found** - <> means required, [] means optional`)

                    message.author.send({embed})
                    message.channel.send({embed: {
                        color: 0x1D82B6,
                        description: `**Check your DMs ${message.author}!**`
                    }})

                }



    }

});

如果我输入“cb!help admin”,我会在控制台中收到此错误

                    if (args.join(" ").trim().toUpperCase() === commands[cmd].group.toUpperCase()) {
                             ^

TypeError: args.join is not a function

我可以做什么来解决这个问题?我也尝试过if (args[0].join...但这行不通。

一如既往,感谢您花时间阅读本文。我基于过时的代码,因此可能还有其他一些错误。我也在尝试修复所有这些问题。

最佳答案

args 在您的代码示例中有一个定义:

let args = cont.shift().toLowerCase();

这使得 args 成为一个 string - string 没有 join() 方法作为 join()是数组原型(prototype)链的一部分,字符串不继承自该原型(prototype)链。 shift() 将返回 cont 数组的第一个元素,因此您可能只想调用 args.toUpperCase(),尽管我会建议重命名变量以使 args 的含义更清晰。

关于javascript - Discord 帮助命令 - args.join 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904671/

相关文章:

javascript - 将事件附加到对象/数组

javascript - 使用 requirejs 并尝试将 javascript 优化为 vendor 文件和应用程序文件

javascript - Bot 未在 overwritePermissions 上设置权限

javascript - 与 Ajax 循环中的 onClick 不起作用

javascript - 是否可以解构为已声明的变量?

node.js - 带有 SSL 和 nodejs 的亚马逊 ELB

node.js - 调用nodeJS将数据保存到文件中但得到空内容

node.js - 缩进无效,可以使用制表符或空格,但不能同时使用 jade

javascript - 是否可以通过发送消息来打开 Discord.js 机器人?

python - 未找到 Discord debian 模块 python3.6 和 python2.7 安装