首页 > 解决方案 > 我如何检查某个机器人是否存在于公会中?不和谐.js

问题描述

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

这是我当前的代码:

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

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我建议获取公会的所有成员以确保所有成员都被缓存。这将返回一个承诺,解决获得公会成员收藏的承诺。与机器人的 id 一起使用has(),如果在 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 才能进行提取工作。


推荐阅读