【问题标题】:Discord.js check if bot, loop mentionDiscord.js 检查机器人,循环提及
【发布时间】:2021-07-08 06:21:03
【问题描述】:

所以我有这个命令,它工作得很好,但我试图让它只接收用户,因为它也接收并提到机器人。该命令的想法是随机选择用户,然后在消息中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}`)

}}

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

【问题讨论】:

    标签: javascript discord.js


    【解决方案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}`);
    

    【讨论】:

    • 感谢 while 循环知识,尽管我仍然遇到机器人被 ping 的问题。我非常喜欢使用 while 循环实现的布局
    • 等等,我想出了一个更好的方法。我将编辑代码@ViridoanZe
    • 哇,你的代码看起来棒极了,似乎 discord api 现在不想和我一起工作,并且拒绝仍然 bot 检查 lmao
    猜你喜欢
    • 2021-01-08
    • 2020-09-08
    • 2020-07-02
    • 1970-01-01
    • 2020-12-28
    • 2019-03-21
    • 2022-01-24
    • 2021-09-09
    • 2020-09-15
    相关资源
    最近更新 更多