Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何从所有碎片中获取users.cache并将其放入集合中,就像collection client.user.cache返回的一样?_Node.js_Arrays_Collections_Discord.js_Bots - Fatal编程技术网

Node.js 如何从所有碎片中获取users.cache并将其放入集合中,就像collection client.user.cache返回的一样?

Node.js 如何从所有碎片中获取users.cache并将其放入集合中,就像collection client.user.cache返回的一样?,node.js,arrays,collections,discord.js,bots,Node.js,Arrays,Collections,Discord.js,Bots,我有一个discord机器人,它使用: client.users.cache 这将返回一个集合[Map],其中包含由bot缓存的所有用户。 我添加了切分,我也希望这样做,因此我将其替换为: client.shard.fetchClientValues('users.cache'); 但这将返回一个数组,我需要一个集合[Map],它与从以下位置返回的集合相同: client.users.cache 是否有人知道如何将集合[Map]中的数组转换为从以下位置返回的数组: client.users

我有一个discord机器人,它使用:

client.users.cache
这将返回一个集合[Map],其中包含由bot缓存的所有用户。 我添加了切分,我也希望这样做,因此我将其替换为:

client.shard.fetchClientValues('users.cache');
但这将返回一个数组,我需要一个集合[Map],它与从以下位置返回的集合相同:

client.users.cache
是否有人知道如何将集合[Map]中的数组转换为从以下位置返回的数组:

client.users.cache
或者如何从所有看起来与从中返回的碎片相同的碎片中获取集合[Map]:

client.users.cache

我认为您可以使用该方法创建一个新的数组,遍历该数组,并插入/设置新元素。只需确保密钥是用户ID,数据是用户对象:

let results=wait client.shard.fetchClientValues('users.cache');
let collection=new Discord.collection();
results.forEach((用户)=>{
users.forEach((用户)=>{
集合.set(user.id,newdiscord.user(client,user));
});
});
控制台日志(收集);
Collection
与非常相似,因此它也可能由两个元素数组组成,其中第一个元素是键,第二个是值:

let results=wait client.shard.fetchClientValues('users.cache');
let collection=new Discord.collection();
results.forEach((用户)=>{
让userCollection=newdiscord.Collection(users.map((user)=>[user.id,newdiscord.user(client,user)]);
collection=collection.concat(userCollection);
});
控制台日志(收集);

感谢answear,但我使用它对所有用户进行forEach,而您提供的代码实际上不起作用,因为集合dosnet已将每个用户标记为用户,因此我无法对集合中的每个用户进行forEach,也许您可以更新代码来实现这一点(我一生中很少使用集合和地图)我刚刚更新了它,完全忘记了您从
fetchClientValues
收到了一个数组。您好,谢谢您的更新,但是代码仍然可以使用。我的代码获取所有用户,并根据他们的id从文件和其他东西中筛选他们,如果他们是bot或更多,如果他们传递所有if语句,他们会得到一个dm,使用此代码只是说user.send不是一个函数。你可以帮我解决这个问题吗(我以前没有使用过切分和集合)你可以将
users.map((user)=>[user.id,user])
更改为
users.map((user)=>[user.id,new Discord.user(client,user)]
。我刚刚更新了上面的代码。(也许你也可以用
broadcastEval
来做这个,但我不太确定:)