簡體   English   中英

如果沒有用戶對第一反應做出反應,則 Discord.js 反應收集器將失敗。 無法讀取未定義的屬性“計數”

[英]Discord.js Reaction collector failing if no user reacts with first reaction. Cannot read property 'count' of undefined

我已經為 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)

如果發生相反的情況,則不會發生這種情況(用戶的反應是贊成而不是反對)。 相關代碼:

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 如果它為空,但這並不能解決它。 非常令人困惑的是,只有在 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM