Skip to content

Commit fdb54c0

Browse files
author
iAmZiNc
authored
Update per York's feedback
Add: Friendly descriptions and introductions to topics. Other useful console.log() output examples. Examples of things that each function *does not* support retreiving.
1 parent 09d0bfb commit fdb54c0

File tree

1 file changed

+57
-17
lines changed

1 file changed

+57
-17
lines changed

understanding/sharding.md

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ const { ShardingManager } = require('discord.js');
3030
const manager = new ShardingManager('./YOUR_BOT_FILE_NAME.js', {
3131
// for ShardingManager options see:
3232
// https://discord.js.org/#/docs/main/stable/class/ShardingManager
33-
totalShards: 'auto',
33+
34+
// 'auto' handles shard count automatically
35+
totalShards: 'auto',
36+
37+
// your bot token
3438
token: 'YOUR_TOKEN_GOES_HERE'
3539
});
3640

3741
// Spawn your shards
3842
manager.spawn();
3943

40-
// Emitted when a shard is created
44+
// The shardCreate event is emitted when a shard is created
45+
// You can use it for something like logging shard launches
4146
manager.on('shardCreate', (shard) => console.log(`Shard ${shard.id} launched`));
4247
```
4348

@@ -47,31 +52,43 @@ manager.on('shardCreate', (shard) => console.log(`Shard ${shard.id} launched`));
4752
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()`.
4853
{% endhint %}
4954

50-
Responses from [`fetchClientValues`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=fetchClientValues) and [`broadcastEval`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=broadcastEval) return a Promise which resolves to an Array containing the values from each shard. See example data for each below.
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`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=fetchClientValues) or [`broadcastEval`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=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!
5158

5259
## FetchClientValues
5360

54-
[`fetchClientValues`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=fetchClientValues) gets Client properties from all shards. This is used when you would like to get any of the nested properties of the Client, such as `guilds.size` or `uptime`.
61+
[`fetchClientValues`](https://discord.js.org/#/docs/main/stable/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.
5562

56-
Example data from `fetchClientValues()`:
63+
Example:
5764
```javascript
5865
/*
59-
Example of result of fetchClientValues() on a bot with 4,300 guilds split across 4 shards
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.
6068
*/
61-
const res = await client.shard.fetchClientValues('guilds.size');
6269

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()
77+
const res = await client.shard.fetchClientValues('guilds.size');
6378
console.log(res);
6479
// Array: [
65-
//1075, // shard 0
66-
//1075, // shard 1
80+
//1050, // shard 0
81+
//1100, // shard 1
6782
//1075, // shard 2
6883
//1075 // shard 3
6984
//]
7085

7186
````
7287

88+
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):
7391

74-
Example `fetchClientValues()` function:
7592
```javascript
7693
/*
7794
Example by ZiNc#2032
@@ -87,22 +104,37 @@ const getServerCount = async () => {
87104
// get guild collection size from all the shards
88105
const req = await client.shard.fetchClientValues('guilds.size');
89106

90-
// return added value
107+
// return the added value
91108
return req.reduce((p, n) => p + n, 0);
92109
}
93110
```
94111

112+
{% hint style="info" %}
113+
`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+
95116
## BroacastEval
96117

97-
[`broadcastEval`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=broadcastEval) evaluates the input in the context of each shard's Client(s). This is used when you want to execute a method or process data on a shard and return the result. See examples below.
118+
[`broadcastEval`](https://discord.js.org/#/docs/main/stable/class/ShardClientUtil?scrollTo=broadcastEval) evaluates the input in the context of each shard's Client(s). 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.
98119

99-
Example data from `broadcastEval()`:
120+
Example:
100121
```javascript
101122
/*
102123
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.
103125
*/
104-
const res = await client.shard.broadcastEval('this.guilds.map((guild) => guild.members.size)');
105126

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
129+
console.log(client.guilds.map((guild) => guild.members.size));
130+
//[
131+
//30,
132+
//25
133+
//],
134+
135+
// If we would like to map our guilds' members.size from our
136+
// servers on all of our shards, we must make use of broadcastEval()
137+
const res = await client.shard.broadcastEval('this.guilds.map((guild) => guild.members.size)');
106138
console.log(res);
107139
// Array: [
108140
//[ // shard 0
@@ -117,13 +149,17 @@ console.log(res);
117149

118150
````
119151

152+
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()`.
153+
154+
Here's an example of a function that uses `broadcastEval()` to get a single guild no matter what shard it is present on:
120155

121-
Example `broadcastEval()` function:
122156
```javascript
123157
/*
124158
Example by ZiNc#2032
125159
The following code fetches a single guild from across shards
126-
NOTE: Fetched guild's properties such as "Guild.members" and "Guild.roles" will not be Managers, but arrays of snowflake IDs
160+
161+
NOTE: Fetched guild's properties such as "Guild.members" and "Guild.roles" will
162+
not be Managers; these properties will be arrays of snowflake IDs
127163
128164
discord.js version 11.x
129165
client = new discordjs.Client()
@@ -132,10 +168,14 @@ Example `broadcastEval()` function:
132168
*/
133169

134170
const getServer = async (guildID) => {
135-
// broadcast request
171+
// try to get guild from all the shards
136172
const req = await client.shard.broadcastEval(`this.guilds.get("${guildID}")`);
137173

138174
// return non-null response or false if not found
139175
return (req.find((res) => !!res) || false);
140176
}
141177
```
178+
179+
{% hint style="info" %}
180+
`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()`.
181+
{% endhint %}

0 commit comments

Comments
 (0)