Discord bot不会通过用户输入来响应命令

时间:2020-09-03 07:52:54

标签: javascript node.js discord.js

我想执行命令,以便显示有关用户向机器人发出的邀请的信息。 但是当我尝试r!invitecheck时,僵尸程序没有响应,或者给出了错误。 我曾尝试在编码不一致的服务器上询问有人提供的帮助,但仍然无法正常工作。

这是代码

const args = msg.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift();

switch (args[0]) {
    case 'invitecheck':
        if (!args.length) {
            return msg.channel.send(`You didn't provide any arguments, ${message.author}!`);
        }
        client.fetchInvite(args[1])
            .then(invite => {
                const test12 = new Discord.MessageEmbed()
                    .setAuthor(invite.guild.name)
                    .addField(`Inviter`, ` ${invite.inviter.username} (<@${invite.inviter.id})>`)
                    .addField(`Total members`, invite.memberCount)
                    .addField(`Owner`, ` ${invite.guild.owner} (<@${invite.guild.ownerID}>)`)
                    .addField(`Region`, ` ${invite.guild.region}`)
                    .addField(`Verification level`, ` ${invite.guild.verificationLevel}`)
                console.log(invite)
                msg.channel.send(test12)
            })
        break;
}    

1 个答案:

答案 0 :(得分:0)

这是因为您如何声明命令Array.shift()会删除数组的第一个元素,因此如果您检查命令,它将起作用:

const args = msg.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift();

switch (command) {
    case 'invitecheck':
        if (!args.length) 
            return msg.channel.send(`You didn't provide any arguments, ${message.author}!`);
        client.fetchInvite(args[0])
            .then(invite => {
                const test12 = new Discord.MessageEmbed()
                    .setAuthor(invite.guild.name)
                    .addField(`Inviter`, ` ${invite.inviter.username} (<@${invite.inviter.id})>`)
                    .addField(`Total members`, invite.memberCount)
                    .addField(`Owner`, ` ${invite.guild.owner} (<@${invite.guild.ownerID}>)`)
                    .addField(`Region`, ` ${invite.guild.region}`)
                    .addField(`Verification level`, ` ${invite.guild.verificationLevel}`)
                console.log(invite)
                msg.channel.send(test12)
            })
        break;
}