discord.js - Random user ID from reactions under message

Viewed 229

I want to get random user ID from users with reaction under some message, but almost always when I'm trying to get all users with reaction it returns No Winner even if I reacted
Code:

    setTimeout(()=> {
        // msg.reactions.removeAll
        if(msg.reactions.cache.get("").users.cache.filter(user => !user.bot).size > 0) {
            const winner = msg.reactions.cache.get("").users.cache.filter(user => !user.client).random().id
            message.channel.send(`Winner: @<${winner}>`)
        } else {
            message.channel.send("No winner.")
        }
    }, time-Date.now())
2 Answers

I had to add

Intents.FLAGS.GUILD_MESSAGE_REACTIONS to my intents.

I modified your code and it's working. You get No winner result every time because of user.client. You should use user.bot.

Here's a code I modified:

setTimeout(()=> {

  const reaction = message.reactions.cache.get("")
  const reactionUsers = reaction.users.cache.filter(user => !user.bot)

  if(reactionUsers.size > 0){
    const winner = reactionUsers.random()
    const winnerId = winner.id
    message.channel.send(`Winner: <@${winnerId}>`)

  } else {
    message.channel.send(`No winner`)
  }
}, 10000)

Change 1: I used user.bot instead of user.client

Change 2: I assigned all variables before using them (It's more readable for me).

Change 3: @<${winnerId}> is not correct. Use <@${winnerId}> instead. (@ should be inside of <> tag)

Related