Reputation: 25
Im trying to make an @someone command that ghost pings a random person (annoying, I know) and recently the command has started breaking but only most of the time. About 1 in every 5 times it works and I dont Know what's causing it Here's the code
} else if (message.content.toLowerCase() === `${prefix}someone`) {
message.delete({
timeout: 1
})
const userList = message.guild.members.cache.array();
var randomNumber = Math.round(Math.random() * message.guild.memberCount)
var pingPerson = userList[randomNumber]
message.channel.send("<@" + pingPerson.user.id + ">")
.then(msg => {
msg.delete({
timeout: 1
})
})
Upvotes: 1
Views: 38
Reputation: 132
Could you check if your pingPerson
value returns undefined?
In your example you're getting a random number from message.guild.memberCount
using Math.round which could get you an index which is 1 more than the array length.
Also message.guild.members.cache.array()
could be different from message.build.memberCount
which could also be causing your problem.
const userList = message.guild.members.cache.array();
var randomNumber = Math.round(Math.random() * message.guild.memberCount)
var pingPerson = userList[randomNumber]
Edits
const userList = message.guild.members.cache.array();
var randomNumber = Math.floor(Math.random() * userList.length)
var pingPerson = userList[randomNumber]
Upvotes: 1