ShardingManager
classe · deprecated · Source
This is a utility class that makes multi-process sharding of a bot an easy and painless experience. It works by spawning a self-contained ChildProcess or Worker for each individual shard, each containing its own instance of your bot's Client. They all have a line of communication with the master process, and there are several useful methods that utilize it in order to simplify tasks that are normally difficult with sharding. It can spawn a specific number of shards or the amount that Discord suggests for the bot, and takes a path to your main bot script to launch for each one.
Extends: EventEmitter
Constructor
Parameters
| Name | Type | Description |
|---|---|---|
file | string | Path to your shard script file |
options? | ShardingManagerOptions | Options for the sharding manager |
Properties
file
Path to the shard script file Source
shardList
List of shards this sharding manager spawns Source
totalShards
Amount of shards that all sharding managers spawn in total Source
mode
Mode for shards to spawn with Source
respawn
Whether shards should automatically respawn upon exiting Source
shardArgs
An array of arguments to pass to shards (only when ShardingManager#mode is process) Source
execArgv
An array of arguments to pass to the executable (only when ShardingManager#mode is process) Source
token
Token to use for obtaining the automatic shard count, and passing to shards Source
shards
A collection of shards that this manager has spawned Source
Methods
createShard
Creates a single shard. Using this method is usually not necessary if you use the spawn method. Parameters
| Name | Type | Description |
|---|---|---|
id? | number | Id of the shard to create This is usually not necessary to manually specify. Default: this.shards.size. |
Returns: Shard — Note that the created shard needs to be explicitly spawned using its spawn method. Source
spawn
Spawns multiple shards. Parameters
| Name | Type | Description |
|---|---|---|
options? | MultipleShardSpawnOptions | Options for spawning shards |
Returns: Promise<Collection<number, Shard>>Source
broadcast
Sends a message to all shards. Parameters
| Name | Type | Description |
|---|---|---|
message | * | Message to be sent to the shards |
Returns: Promise<Array<Shard>>Source
broadcastEval
Evaluates a script on all shards, or a given shard, in the context of the Clients. Parameters
| Name | Type | Description |
|---|---|---|
script | function | JavaScript to run on each shard |
options? | BroadcastEvalOptions | The options for the broadcast Default: {}. |
Returns: Promise<(*\|Array<*>)> — Results of the script execution Source
fetchClientValues
Fetches a client property value of each shard, or a given shard. Parameters
| Name | Type | Description |
|---|---|---|
prop | string | Name of the client property to get, using periods for nesting |
shard? | number | Shard to fetch property from, all if undefined |
Returns: Promise<(*\|Array<*>)>
manager.fetchClientValues('guilds.cache.size')
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);_performOnShards
Runs a method with given arguments on all shards, or a given shard. Parameters
| Name | Type | Description |
|---|---|---|
method | string | Method name to run on each shard |
args | Array<*> | Arguments to pass through to the method call |
shard? | number | Shard to run on, all if undefined |
Returns: Promise<(*\|Array<*>)> — Results of the method execution Source
respawnAll
Kills all running shards and respawns them. Parameters
| Name | Type | Description |
|---|---|---|
options? | MultipleShardRespawnOptions | Options for respawning shards |
Returns: Promise<Collection<number, Shard>>Source
Events
shardCreate
Emitted upon creating a shard. Parameters
| Name | Type | Description |
|---|---|---|
shard | Shard | Shard that was created |
| Source |