discord.js bot用于决斗其他玩家

时间:2018-02-18 23:34:07

标签: javascript discord.js

我正在制作一个机器人,允许用户挑战别人进行决斗。目前我仍然坚持让僵尸程序只显示已经被接受的决斗,如果它来自受到挑战的用户。原样,它只允许来自原始邮件发件人的响应。任何帮助将不胜感激。

if(command === `${prefix}fight`) {
    //checks if the username to fight is in the message
    let author1 = message.author.username;
    let user = message.mentions.users.first();
    if(!user) return message.reply("you did not specify who you would like to fight!");

    //checks if the users is trying to fight themselves
    if(user.id == message.author.id) return message.reply('you cannot fight yourself!');

    //checks if the user is trying to fight the bot
    if(user.bot ==  true)
        return message.reply('you cannot fight a bot!');

    //saves the two user ids to variables
    var fighter1 = message.author.id;
    var fighter2 = user.id;

    //announces challenge and awaits response
    var challenged = user.toString();
    message.channel.send(`${challenged}, ${author1} has challenged you to a duel. Do you accept the challenge, yes or no?`)
        .then(() => {
            message.channel.awaitMessages(response => response.content == 'yes' && message.author.id == fighter2 || response.content == 'no' && message.author.id == fighter2, {
                max: 1,
                time: 60000,
                errors: ['time'],
            })
            .then((collected) => {
                if (collected.first().content == 'yes') {
                    message.channel.send(`${challenged} has accepted the challenge!`);
                }
                else if(collected.first().content == 'no') {
                    message.channel.send(`nope`);
                }
            })
            .catch(() => {
                message.channel.send(`No response. Fight has been cancelled.`);
            });
        });       
}

2 个答案:

答案 0 :(得分:0)

那是因为您使用的是message.author而不是response.author

message.channel.awaitMessages(response => return (response.content == 'yes' && message.author.id == fighter2)
|| (response.content == 'no' && message.author.id == fighter2), 
{
    max: 1,
    time: 60000,
    errors: ['time'],
})

答案 1 :(得分:0)

看起来您的过滤器功能仅在查看response时查看content消息,但是当您重新检查.author.id时,您会从原始message获取该消息{1}}开始了战斗(来自挑战者,而不是受到挑战的人)。

尝试更改过滤功能,使用response.author.id代替message.author.id,如下所示:

message.channel.awaitMessages(response => response.content == 'yes' && response.author.id == fighter2 || response.content == 'no' && response.author.id == fighter2, {
    ...
})