简体   繁体   English

Discord.JS 无法识别投票中的第一个选项

[英]Discord.JS Won't Recognize the first option in a Poll

I've had this problem for quite some time now.我已经有这个问题很长一段时间了。 I've tried to resolve it, and I can't understand why it's not working.我试图解决它,但我不明白为什么它不起作用。 I've used code that has worked before and still continues to work to this day.我使用了以前有效的代码,并且至今仍在继续工作。 The main problem is that they are only counting the number of downvotes (represented by the thumbs down unicode) and not the upvotes (represented by the thumbs up unicode).主要问题是他们只计算反对票的数量(由不赞成的 unicode 表示)而不是赞成票(由赞成的 unicode 表示)。 How can I fix this?我怎样才能解决这个问题? Thanks.谢谢。

(async () => {
 if (!args[1]) return message.channel.send('You need to specify a person!'); //If the statement is missing the second word, which in this case would be the person, it will notify you.
 const mappo = ['👍', '👎'];
 message.react('👍').then(() => message.react('👎'));
 let msgArgs = args.slice(1).join(' '); //If the statement is missing the second word, which in this case would be the person, it will notify you.

 const embad = new Discord.MessageEmbed()
  .setTitle('SimPoll')
  .addField('Topic:', `${msgArgs}`)
  .addField('Rules:', 'Vote for your side.');
 message.channel.send(embad);
 setTimeout(function() {
  const filter = (reaction) => mappo.includes(reaction.emoji.name);
  message
   .awaitReactions(filter, { time: 6000 })
   .then((collection) => {
    //not the most optimal way to do it
    const upvotes = message.reactions.cache.map(
     (r) => `${'👍'} ${r.users.cache.size}`
    )[0]; //This can count the number of reactions. It's quite difficult to make.
    const downvotes = message.reactions.cache.map(
     (r) => `${'👎'} ${r.users.cache.size}`
    )[1];

    if (upvotes < downvotes) {
     message.channel.send('Majority has ruled in the favor of No.');
    } else if (upvotes == downvotes) {
     message.channel.send('The scales are balanced.');
    } else {
     message.channel.send('Majority has ruled in the favor of Yes.');
    }
   })
   .catch(console.error);
 }, 2000);
})();

You're comparing two strings against each other, not two numbers.您正在比较两个字符串,而不是两个数字。 I'm not really even sure what you're trying to do by mapping each reaction into a string displaying the emoji and then the user count.我什至不确定通过将每个反应映射到显示表情符号然后用户计数的字符串来尝试做什么。

Instead of mapping the collection into an array and then using [0] and [1] to get the first and second elements, you can just use Collection#first(2) .无需将集合映射到数组中,然后使用[0][1]获取第一个和第二个元素,您只需使用Collection#first(2)即可。 This will more efficiently get the first two elements which you can then assign to variables using array restructuring.这将更有效地获得前两个元素,然后您可以使用数组重组将它们分配给变量。

const [upvoteReaction, downvoteReaction] = message.reactions.cache.first(2);

From there you can compare MessageReaction#users#cache#size从那里你可以比较MessageReaction#users#cache#size

const upvotes = upvoteReaction.users.cache.size;
const downvotes = dovoteReaction.users.cache.size;

if (upvotes < downvotes) {
  // ...
} else if (upvotes === downvotes) {
  // ...
} else {
  // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM