ViridianZe
ViridianZe

Reputation: 121

Discord.js check if bot, loop mention

So I have this command and it works great, but I'm trying to get it to only pick up users as it also picks up and mentions bots. The idea of the command is to randomly pick people that are users and then mention them in a message.


let number = args[0];

if(isNaN(number)){
 return message.channel.send(`Amount isnt a number or no amount listed. !randommention (number)`);
}else{

let ret = "";


for (i = 0; i < number; i++) {
    let randomName = message.guild.members.cache.random().user;
    //Failed attempt to block bots from mention
    if(randomName == message.member.user.bot){
      repeat(randomName)}
    ret += `\n${randomName}`
  }
  

  message.channel.send(`**User(s) Selected:** ${ret}`)

}}

I tried a little repeat as a work around which didn't work anyway, but I'm having problems finding how to just avoid anything bot entirely. Any ideas?

Upvotes: 0

Views: 214

Answers (1)

Akio
Akio

Reputation: 723

Trust me while loops are sometimes useful.
In your case try this

let i = 0;
while(i < number){
    let randomMember = message.guild.members.cache.random();
    //You don't need the .user
    if(randomMember.bot){  //Checks if the user is bot
        continue;
        //Note: if it's a bot the count won't increase
        //So the total count will be equal to the one you want
        //It'll be on a forever loop if there are less members in the guild than selected
    } else { //better be safe
        i ++; //It'll go forward if the user isn't a bot
        ret += `\n${randomMember}`;
    }
}
message.channel.send(`**User(s) Selected:** ${ret}`);

Upvotes: 1

Related Questions