Reputation: 81
I've set up a pretty basic reaction collector for discord.js - i've got 2 emojis that get used to react to a message. However if users only react with downVote (not upvote, the system errors):
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)
This doesn't occur if the opposite happens (users react with upvote not downvote). Relevant code:
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.`);
});
How can I prevent/handle the current error I am receiving better. I've tried setting downvote.count to 0 if it is null however this doesn't resolve it. It is very confusing how this only occurs when upvote isnt reacted but not visa-versa.
if (reacts.get(downVote).count == null) reacts.get(downVote).count = 0;
Upvotes: 0
Views: 739
Reputation: 2665
I'm not certain as to why it doesn't fail when users react with upvote only, I would expect it to fail in both scenarios. The reason it fails is because .get() returns undefined when nothing is available to get, and undefined does not have a count property. You just have to guard against such an outcome.
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.
}
Compact version
let uv = reacts.get(upVote);
let dv = reacts.get(downVote);
let draw = (!uv && !dv) || (uv && dv && dv.count == uv.count);
Upvotes: 1