在特定服务器中通过ID查找 Discord.js 中的用户。

huangapple go评论68阅读模式
英文:

discord.js shading find a user in a specific guild by id

问题

我的机器人使用了分片。我尝试通过它们的ID来获取特定服务器中的用户角色。

__在没有分片之前__:

```js
const serverSupportGuild = client.guilds.cache.get('myGuildId');
const user = serverSupportGuild.members.cache.get(id);

console.log(user._roles);

现在我尝试这样做

const getServer = async (guildID) => {
    const req = await client.shard.broadcastEval((c, id) => c.guilds.cache.get(id), { 
        context: guildID
    });
    return req.find(res => !!res) || null;
}
const serverSupportGuild = await getServer('myGuildId');
console.log("成员: ", serverSupportGuild.members);
const user = serverSupportGuild.members.cache.get(id);  ←← 记录错误
console.log("用户: ", user);
成员: [
  '829707635787825152',
  '660435627757666311'
]
TypeError: Cannot read properties of undefined (reading 'get')

在我使用我的方法之前,没有分片的情况下,我能够看到所有来自我的服务器的成员及其相关信息。

但现在,我只有一个包含机器人ID和我的ID的数组。如何使用分片获取服务器中的所有成员

谢谢。


<details>
<summary>英文:</summary>

My bot use shards. And I try to get user roles in a specific guild by they ids.

__Before sharding__:

```js
const serverSupportGuild = client.guilds.cache.get(&#39;myGuildId&#39;);
const user = serverSupportGuild.members.cache.get(id);

console.log(user._roles);

Now I try to do this:

const getServer = async (guildID) =&gt; {
    const req = await client.shard.broadcastEval((c, id) =&gt; c.guilds.cache.get(id), { 
        context: guildID
    });
    return req.find(res =&gt; !!res) || null;
}
const serverSupportGuild = await getServer(&#39;myGuildId&#39;);
console.log(&quot;members: &quot;, serverSupportGuild.members);
const user = serverSupportGuild.members.cache.get(id);  ←← log ERROR
console.log(&quot;user: &quot;, user);
members: [
  &#39;829707635787825152&#39;,
  &#39;660435627757666311&#39;
]
TypeError: Cannot read properties of undefined (reading &#39;get&#39;)

Before sharding with my method i was able to see all members from my guild with they respective informations.

But now, I just have an array with bot id and my id. How can I get all members from a guild with shards ?

Thanks.

答案1

得分: 1

分片不会返回复杂的对象,它返回简单的东西,比如字符串或字符串数组,就像你在结果中看到的那样。如果你需要执行更复杂的操作,你需要在broadcastEval上下文中执行它们,因为那里可以看到服务器/频道/缓存/成员等对象。

执行你的任务,然后从getServer返回(考虑到这个见解,你可能想现在重命名它),然后继续在完成所需任务后继续代码。

英文:

Sharding doesn't return complex objects, it returns simple things, like strings, or arrays of strings, as you see in your results. If you have more complex things to do, you need to do them inside of the broadcastEval context, as that is where the guild/channel/cache/member etc objects are visible.

Perform your tasks, then return from the getServer (which you might want to rename now, given this insight) and continue on with the code after having done the task(s) you wanted on the member(s) etc.

huangapple
  • 本文由 发表于 2023年1月5日 01:29:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75009117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定