問題描述
我正在尋找兩個不和諧 User
之間的公共服務器.目前,我的機器人能夠訪問它所屬的公會,但是給定一個向它發送消息的用戶,它無法訪問該用戶的任何公會.我知道不和諧會限制您查看共享的公會/服務器,但我什至找不到任何訪問這些的方法.
I am looking to get the common servers between two discord User
s. Currently, my bot is able to access the guilds that it is a part of, however given a user who has sent it a message, it is unable to access any of the guilds of the user. I understand that discord limits you to seeing shared guilds/servers, but I can't find any way to even access those.
任何幫助將不勝感激.
上下文:DM
guild = message.client.guilds.cache.find(clientGuild=>message.author.????)
我想要類似的東西:
guild = message.client.guilds.cache.find(clientGuild=>message.author.guilds.includes(clientGuild)
推薦答案
這不適用于分片.我還沒有分片的經驗,所以我會把它留給有經驗的人.
This will not work with sharding. I don't have experience with sharding yet, so I'll leave it for someone else with experience.
您可以嘗試使用緩存的成員(并祈禱用戶被緩存,如果用戶發送了消息但不能保證會出現這種情況)或獲取成員.
You can either try to use the cached members (and pray that the user is cached, which should be the case if the user sent a message but isn't guaranteed) or fetch the member.
公會對象有一個成員 從用戶獲取成員對象的方法.您可以簡單地檢查它是否未定義以查看用戶是否在公會中并且被緩存.
The guild object has a member method to get the member object from user. You can simply check that it's not undefined to see if the user is in the guild and is cached.
client.guilds.cache.filter(guild => !!guild.member(message.author));
使用 fetch 的更長方法
這將獲取成員,因此具有高公會計數的機器人可能會達到速率限制(查看文檔 這里).
var guilds = Promise.all(
client.guilds.cache.map(async guild => [
guild.id,
await guild.members.fetch(message.author).catch(() => null))
])
).then(guilds => guilds.filter(g => g[1]).map(guild => client.guilds.resolve(guild[0]));
它通過嘗試從機器人所在的每個公會中獲取成員來工作(fetch 可能會失敗,所以有一個只返回 null 的 catch),然后根據結果過濾公會.collection 類上沒有異步過濾器,所以我改為映射到異步謂詞,然后我使用 等待它們Promise.all.
It works by trying to fetch the member from every guild the bot is in (the fetch may fail so there's a catch which just returns null) and then filtering the guilds based on the result. There's no async filter on the collection class, so instead I map to async predicates which I then await them using Promise.all.
如果你需要分片,應該可以使用broadcastEval做同樣的事情,但是我還沒有足夠的信心來寫它.
If you need sharding, it should be possible to do the same using broadcastEval, but I'm not confident enough to write it yet.
這篇關于Discord.js 獲取兩個用戶之間的公共服務器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!