You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Information is not readily available between shards. In order to get or share information across shards, you will need to make use of either `fetchClientValues()` or `broadcastEval()`.
53
+
{% endhint %}
54
+
55
+
Remember how we were talking about sharding being a method of "splitting" the bot into multiple instances of itself? Because your sharded bot is now in separate, individual instances, things like your adding your total guilds or getting a specific guild are not as simple as they were before. We must now use either [`fetchClientValues`](##FetchClientValues) or [`broadcastEval`](##BroadcastEval) to get information from across shards.
56
+
57
+
These two functions are your go-to for getting any information from other shards, so get familiar with them!
58
+
59
+
## FetchClientValues
60
+
61
+
[`fetchClientValues`](https://discord.js.org/#/docs/main/v11/class/ShardClientUtil?scrollTo=fetchClientValues) gets Client properties from all shards. This is what you should use when you would like to get any of the nested properties of the Client, such as `guilds.size` or `uptime`. It's useful for getting things like Collection sizes, basic client properties, and unprocessed information about the client.
62
+
63
+
Example:
64
+
```javascript
65
+
/*
66
+
Example result of fetchClientValues() on a bot with 4,300 guilds split across 4 shards.
67
+
Assume this is being executed on shard 0, the first shard.
68
+
*/
69
+
70
+
// If we just get our client.guilds.size, it will return
71
+
// only the number of guilds on the shard this is being run on.
72
+
console.log('client.guilds.size');
73
+
// 1050
74
+
75
+
// If we would like to get our client.guilds.size from all
76
+
// of our shards, we must make use of fetchClientValues().
Let's say you want to do something like get your total server count - In a non-sharded environment, this would be as simple as getting the `client.guilds.size`. However in a sharded environment, `client.guilds.size` will return not the total servers your bot is in. Instead it returns only the total number of servers *on this shard*, like in the first part of the example above.
89
+
90
+
Here's an example of a function that uses `fetchClientValues()` to first get, then add the total number of guilds from *all shards* (i.e. your bot's total guild count):
91
+
92
+
```javascript
93
+
/*
94
+
Example by ZiNc#2032
95
+
The following code fetches total combined shards' server counts.
`fetchClientValues()` does not allow you to make use of javascript methods or client methods to get or process information before returning it. It only allows you to get information from client properties.
114
+
{% endhint %}
115
+
116
+
## BroacastEval
117
+
118
+
[`broadcastEval`](https://discord.js.org/#/docs/main/v11/class/ShardClientUtil?scrollTo=broadcastEval) evaluates the input *in the context of each shard's Client(s)* (i.e. `this` is used to reference the `Client`). This is what you should use when you want to execute a method or process data on a shard and return the result. It's useful for getting information that isn't available through client properties and must instead be retrieved through the use of methods.
119
+
120
+
Example:
121
+
```javascript
122
+
/*
123
+
Example of result of broadcastEval() on a bot with 4 servers split across 2 shards.
124
+
Assume this is being executed on shard 0, the first shard.
125
+
*/
126
+
127
+
// If we just map our guilds' members.size, it will return
128
+
// only the mapped members.size of the shard this is being run on.
Say you want to get a guild from your client. In a non-sharded environment, you would simply use `client.guilds.get('ID')` or something of that nature and then carry on with your code. In a sharded environment however, it is possible that the guild you're trying to get *is not present on the shard*. In order to get the guild for use, you would then need to fetch it from whatever shard it is present on using `broadcastEval()`.
155
+
156
+
Here's an example of a function that uses `broadcastEval()` to get a single guild no matter what shard it is present on:
157
+
158
+
```javascript
159
+
/*
160
+
Example by ZiNc#2032
161
+
The following code fetches a single guild from across shards.
162
+
163
+
NOTE: Fetched guild's properties such as "Guild.members" and "Guild.roles" will
164
+
not be Managers; these properties will be arrays of snowflake IDs.
`broadcastEval()` will only return basic javascript objects, and will not return Discord.js structures such as Collections, Guild objects, Member object, and the likes. You will need to interact directly with the API or build these structures yourself after getting a response back from your `broadcastEval()`.
0 commit comments