I want to get random user ID from users with reaction on specific message.
await msg.react("<:SenkoSad:788848057844367411>")
setTimeout(()=> {
const winner = msg.reactions.cache.get("<:SenkoSad:788848057844367411>").users.cache.random().id
message.channel.send(`Random user from emotes: <@${winner}>`)
}, time-Date.now())
Here is error:
TypeError: Cannot read properties of undefined (reading 'users')
on line const winner = msg.reactions.cache.get("<:SenkoSad:788848057844367411>").users.cache.random().id
CodePudding user response:
Try using @metro's answer but use the filter function
Like:
msg.reactions.cache.get("788848057844367411").users.cache.filter(e => !e.bot).random().id
CodePudding user response:
Try switching the line to:
const winner = msg.reactions.cache.get("788848057844367411").users.cache.random().id
I am not entirely sure, but perhaps get() expects a Snowflake for a custom emoji rather than the format you provided.
CodePudding user response:
By using reactions.cache.get("788848057844367411") you are getting a MessageReaction.
Note that to get a reaction you need to provide a unicode emoji (In case they are default emojis) or an ID (In this case it is 78884805784444367411, only the ID, not the emoji name).
MessageReaction.users returns you a ReactionsUserManager, the cache of this includes ALL users who have reacted to this message including the bot itself, so you should add a Filter: .users.cache.filter(user => !user.bot)
As for the error, what you can do to avoid this error is a conditional where you check if the number of users (not counting the bot) is greater than 0.
if(msg.reactions.cache.get("788848057844367411").users.cache.filter(user => !user.bot).size > 0) {
// If there can be a winner
} else {
// If nobody reacted
}
