如何从所有分片中获取 users.cache 并将它们放入集合中,就像集合 client.user.cache returns 一样?
How do I get users.cache from all of the shards and put them into a collection just like the collection client.user.cache returns?
我有一个不和谐的机器人,它使用:
client.users.cache
这个 returns 一个集合 [Map],其中包含机器人缓存的所有用户。
我添加了分片并且我想做同样的事情所以我将它替换为:
client.shard.fetchClientValues('users.cache');
但是这个 returns 是一个数组,我需要一个 Collection[Map] 与返回的相同:
client.users.cache
有谁知道如何将 Collection[Map] 中的数组转换为与从以下返回的相同的数组:
client.users.cache
或如何从所有碎片中获取与返回的碎片相同的集合[Map]:
client.users.cache
我想你可以创建一个新的 Collection
, iterate over the array, and insert/set new elements by using the set()
方法。只要确保键是用户ID,数据是用户对象即可:
let results = await client.shard.fetchClientValues('users.cache');
let collection = new Discord.Collection();
results.forEach((users) => {
users.forEach((user) => {
collection.set(user.id, new Discord.User(client, user));
});
});
console.log(collection);
Collection
与 Map
非常相似,因此有可能它也可以用双元素数组的可迭代构造,其中第一个元素是键,第二个是值:
let results = await client.shard.fetchClientValues('users.cache');
let collection = new Discord.Collection();
results.forEach((users) => {
let userCollection = new Discord.Collection(users.map((user) => [user.id, new Discord.User(client, user)]));
collection = collection.concat(userCollection);
});
console.log(collection);
我有一个不和谐的机器人,它使用:
client.users.cache
这个 returns 一个集合 [Map],其中包含机器人缓存的所有用户。 我添加了分片并且我想做同样的事情所以我将它替换为:
client.shard.fetchClientValues('users.cache');
但是这个 returns 是一个数组,我需要一个 Collection[Map] 与返回的相同:
client.users.cache
有谁知道如何将 Collection[Map] 中的数组转换为与从以下返回的相同的数组:
client.users.cache
或如何从所有碎片中获取与返回的碎片相同的集合[Map]:
client.users.cache
我想你可以创建一个新的 Collection
, iterate over the array, and insert/set new elements by using the set()
方法。只要确保键是用户ID,数据是用户对象即可:
let results = await client.shard.fetchClientValues('users.cache');
let collection = new Discord.Collection();
results.forEach((users) => {
users.forEach((user) => {
collection.set(user.id, new Discord.User(client, user));
});
});
console.log(collection);
Collection
与 Map
非常相似,因此有可能它也可以用双元素数组的可迭代构造,其中第一个元素是键,第二个是值:
let results = await client.shard.fetchClientValues('users.cache');
let collection = new Discord.Collection();
results.forEach((users) => {
let userCollection = new Discord.Collection(users.map((user) => [user.id, new Discord.User(client, user)]));
collection = collection.concat(userCollection);
});
console.log(collection);