【问题标题】:How Do i Check if a Certain Bot Exists in a Guild | discord.js我如何检查某个机器人是否存在于公会中 |不和谐.js
【发布时间】:2021-06-03 05:12:20
【问题描述】:

嘿,所以我有一个机器人需要两个机器人相互合作我需要知道如何检查一个机器人是否在公会中,如果不是,然后告诉用户邀请它。

这是我当前的代码:

if(message.guild.users.id === "ID") {
    //If its in the Server
} else {
    //If it isnt
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    我建议获取公会的所有成员以确保 所有 成员都被缓存。这将返回一个承诺,解决获得公会成员收藏的承诺。使用 has() 和机器人的 id,如果在 Collection 中找到 id,则返回 true,否则返回 false。

    message.guild.members.fetch().then(memberList => {
       if (memberList.has("ID")) {
          // If it's in the server
       } else {
          // if it's not
       }
    }).catch(console.error);
    

    您也可以尝试直接获取机器人并检查是否返回了 GuildMember 对象。

    message.guild.members.fetch("ID")
       .then(botObject => {
           // If found
        })
       .catch(err => {
          // If not found
       });
    

    您需要启用 GuildMember 的 Intent 才能工作。

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2018-04-02
      • 2020-07-21
      • 1970-01-01
      相关资源
      最近更新 更多