Class: ShardingManager

ShardingManager

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 utilise 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.

Constructor

new ShardingManager(file, optionsopt)

Parameters:
Name Type Attributes Description
file string Path to your shard script file
options Object <optional>
Options for the sharding manager
Properties
Name Type Attributes Default Description
totalShards string | number <optional>
'auto' Number of total shards of all shard managers or "auto"
shardList string | Array.<number> <optional>
'auto' List of shards to spawn or "auto"
mode ShardingManagerMode <optional>
'process' Which mode to use for shards
respawn boolean <optional>
true Whether shards should automatically respawn upon exiting
shardArgs Array.<string> <optional>
[] Arguments to pass to the shard script when spawning (only available when using the `process` mode)
execArgv Array.<string> <optional>
[] Arguments to pass to the shard script executable when spawning (only available when using the `process` mode)
token string <optional>
Token to use for automatic shard count and passing to shards
Source:

Extends

  • EventEmitter

Members

execArgv :Array.<string>

An array of arguments to pass to the executable (only when ShardingManager#mode is `process`)
Type:
  • Array.<string>
Source:

file :string

Path to the shard script file
Type:
  • string
Source:

mode :ShardingManagerMode

Mode for shards to spawn with
Type:
Source:

respawn :boolean

Whether shards should automatically respawn upon exiting
Type:
  • boolean
Source:

shardArgs :Array.<string>

An array of arguments to pass to shards (only when ShardingManager#mode is `process`)
Type:
  • Array.<string>
Source:

shardList :string|Array.<number>

List of shards this sharding manager spawns
Type:
  • string | Array.<number>
Source:

shards :Collection.<number, Shard>

A collection of shards that this manager has spawned
Type:
  • Collection.<number, Shard>
Source:

(nullable) token :string

Token to use for obtaining the automatic shard count, and passing to shards
Type:
  • string
Source:

totalShards :number

Amount of shards that all sharding managers spawn in total
Type:
  • number
Source:

Methods

broadcast(message) → {Promise.<Array.<Shard>>}

Sends a message to all shards.
Parameters:
Name Type Description
message * Message to be sent to the shards
Source:
Returns:
Type
Promise.<Array.<Shard>>

broadcastEval(script) → {Promise.<Array.<*>>}

Evaluates a script on all shards, in the context of the Clients.
Parameters:
Name Type Description
script string JavaScript to run on each shard
Source:
Returns:
Results of the script execution
Type
Promise.<Array.<*>>

createShard(idopt) → {Shard}

Creates a single shard. Using this method is usually not necessary if you use the spawn method.
Parameters:
Name Type Attributes Default Description
id number <optional>
this.shards.size ID of the shard to create This is usually not necessary to manually specify.
Source:
Returns:
Note that the created shard needs to be explicitly spawned using its spawn method.
Type
Shard

fetchClientValues(prop) → {Promise.<Array.<*>>}

Fetches a client property value of each shard.
Parameters:
Name Type Description
prop string Name of the client property to get, using periods for nesting
Source:
Returns:
Type
Promise.<Array.<*>>
Example
manager.fetchClientValues('guilds.cache.size')
  .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
  .catch(console.error);

respawnAll(shardDelayopt, respawnDelayopt, spawnTimeoutopt) → {Promise.<Collection.<string, Shard>>}

Kills all running shards and respawns them.
Parameters:
Name Type Attributes Default Description
shardDelay number <optional>
5000 How long to wait between shards (in milliseconds)
respawnDelay number <optional>
500 How long to wait between killing a shard's process and restarting it (in milliseconds)
spawnTimeout number <optional>
30000 The amount in milliseconds to wait for a shard to become ready before continuing to another. (-1 or Infinity for no wait)
Source:
Returns:
Type
Promise.<Collection.<string, Shard>>

spawn(amountopt, delayopt, spawnTimeoutopt) → {Promise.<Collection.<number, Shard>>}

Spawns multiple shards.
Parameters:
Name Type Attributes Default Description
amount number | string <optional>
this.totalShards Number of shards to spawn
delay number <optional>
5500 How long to wait in between spawning each shard (in milliseconds)
spawnTimeout number <optional>
30000 The amount in milliseconds to wait until the Client has become ready before resolving. (-1 or Infinity for no wait)
Source:
Returns:
Type
Promise.<Collection.<number, Shard>>

Events

shardCreate

Emitted upon creating a shard.
Parameters:
Name Type Description
shard Shard Shard that was created
Source: