Discord.js 获取两个用户之间的公共服务器

Discord.js Get common servers between two users(Discord.js 获取两个用户之间的公共服务器)
本文介绍了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 获取两个用户之间的公共服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)