我为discord.js建立了一个非常基本的反应收集器-我有2个表情符号,可以用来对消息做出反应。但是,如果用户仅对downVote做出反应(而不对upVote做出系统错误):

D:\tzobot\commands\poll.js:102
                if (reacts.get(downVote).count == reacts.get(upVote).count) { draw = true; }
                                                                    ^
TypeError: Cannot read property 'count' of undefined
    at ReactionCollector.<anonymous> (D:\tzobot\commands\poll.js:102:55)
    at ReactionCollector.emit (events.js:311:20)
    at ReactionCollector.stop (D:\tzobot\node_modules\discord.js\src\structures\interfaces\Collector.js:149:10)
    at D:\tzobot\node_modules\discord.js\src\structures\interfaces\Collector.js:72:73
    at Timeout.<anonymous> (D:\tzobot\node_modules\discord.js\src\client\Client.js:436:7)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)


如果发生相反的情况,则不会发生这种情况(用户以upvote而不是downvote做出反应)。
相关代码:

const Discord = require('discord.js');
const downVote = "👎";
const upVote = "👍";

//irrelevantcode here setting properties such as duration etc.

const mesEmbed = new Discord.RichEmbed()
            .setColor('#0099ff')
            .setTitle(`Poll: ${pollQuestion}`)
            //.setURL('https://discord.js.org/')
            .setAuthor(`${message.author.username}`, `${message.author.avatarURL}`, 'https://discord.js.org')
            .setDescription('Reply with 👍 or 👎 to give your opinion.')

        sendmes(message, mesEmbed);


    },

};
async function sendmes(message, mesEmbed) {
    let msg = await message.reply(mesEmbed);
    await msg.react(upVote);
    await msg.react(downVote);
    await startTimer(msg);



}
async function startTimer(mes) {
    let filter = (reaction) => reaction.emoji.name == upVote || reaction.emoji.name == downVote;

    const collector = mes.createReactionCollector(filter, { time: pollDuration });
    collector.on('end', collected => {
        console.log(`Collected ${collected.size} items`);
        if (collected.size == 0) return mes.reply(`Poll: "${pollQuestion}" has now finished. The result is a tie with no votes.`);
        var draw = Boolean(false);
        var winner = null;
        var loser = null;
        var reacts = collected;
        console.log(reacts);




        if (reacts.get(downVote).count == reacts.get(upVote).count) { draw = true; }
        else if (reacts.get(upVote).count > reacts.get(downVote).count) { winner = (upVote), loser = (downVote) }
        else { winner = (downVote), loser = (upVote) }
        //Check it wasn't a draw
        if (draw == false) return mes.reply(`Poll: "${pollQuestion}" has now finished. The final decision is: ${winner} with ${reacts.get(winner).count} votes.  ${loser} recieved ${reacts.get(loser).count} votes.`);
        //Return draw message if it was
        else return mes.reply(`Poll: "${pollQuestion}" has now finished. The result is a tie with ${reacts.get(upVote).count} votes each.`);

    });



如何更好地防止/处理当前错误。我尝试将downvote.count设置为0(如果为null),但这无法解决。这是非常令人困惑的,只有在upvote没有反应但反之亦然时才如何发生。

if(reacts.get(downVote).count == null)reacts.get(downVote).count = 0;

最佳答案

我不确定为什么当用户仅使用upvote时它不会失败,我希望它在两种情况下都会失败。失败的原因是因为当没有可用的获取时.get()返回undefined,并且undefined没有count属性。您只需要提防这种结果。

let uv = reacts.get(upVote);
let dv = reacts.get(downVote);
if (!uv && !dv) {
    draw = true; // Both were undefined, nobody voted.
} else if (uv && dv && dv.count == uv.count) {
    draw = true; // Neither was undefined and both had the same value.
}


精简版

let uv = reacts.get(upVote);
let dv = reacts.get(downVote);
let draw = (!uv && !dv) || (uv && dv && dv.count == uv.count);

10-01 09:59