javascript - 我如何检查某个机器人是否存在于公会中?不和谐.js
问题描述
嘿,所以我有一个机器人,需要两个机器人相互协作我需要知道如何检查机器人是否在公会中,如果没有,然后告诉用户邀请它。
这是我当前的代码:
if(message.guild.users.id === "ID") {
//If its in the Server
} else {
//If it isnt
}
解决方案
我建议获取公会的所有成员以确保所有成员都被缓存。这将返回一个承诺,解决获得公会成员收藏的承诺。与机器人的 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 才能进行提取工作。
推荐阅读
- java - 试图了解相互身份验证密钥对和证书如何在 gRPC 上下文中工作
- reactjs - 在表中添加序列号列
- excel - Excel公式查找特定数字
- python - 将 vici 套接字连接到特定的网络命名空间
- javascript - VS Code问题:当我保存时,行和空格会自动添加并破坏代码
- php - 角色 codeigniter 的语句
- javascript - 用时刻反应js日期格式
- html - 有没有办法使这种输入数字类型起作用?[HTML5]
- python - 我正在尝试使用 Python 中的 BeautifulSoup 从网站获取表格。但是,当我打印表格时,它没有显示
- python - 在几个熊猫数据框优化上计算列