Discord.js 获取两个用户之间的公共服务器 [英] Discord.js Get common servers between two users

查看:13
本文介绍了Discord.js 获取两个用户之间的公共服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找两个不和谐 User 之间的公共服务器.目前,我的机器人能够访问它所属的公会,但是给定一个向它发送消息的用户,它无法访问该用户的任何公会.我知道不和谐会限制您查看共享的公会/服务器,但我什至找不到任何访问这些的方法.

I am looking to get the common servers between two discord Users. 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 获取两个用户之间的公共服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆