Discord.js 检查是否机器人,循环提及

时间:2021-04-13 07:51:14

标签: javascript discord.js

所以我有这个命令并且它工作得很好,但我试图让它只选择用户,因为它也选择并提到机器人。该命令的想法是随机选择用户,然后在消息中mention 他们。


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}`)

}}

我尝试了一些重复作为解决方法,但无论如何都不起作用,但是我在寻找如何完全避免任何机器人时遇到问题。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

相信我,while 循环有时很有用。
在你的情况下试试这个

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}`);