Skip to content

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

new ShardingManager(file: string, options?: ShardingManagerOptions)

Parameters

NameTypeDescription
filestringPath to your shard script file
options?ShardingManagerOptionsOptions for the sharding manager

Properties

file

file: string

Path to the shard script file Source

shardList

shardList: string | Array<number>

List of shards this sharding manager spawns Source

totalShards

totalShards: number

Amount of shards that all sharding managers spawn in total Source

mode

mode: ShardingManagerMode

Mode for shards to spawn with Source

respawn

respawn: boolean

Whether shards should automatically respawn upon exiting Source

shardArgs

shardArgs: Array<string>

An array of arguments to pass to shards (only when ShardingManager#mode is process) Source

execArgv

execArgv: Array<string>

An array of arguments to pass to the executable (only when ShardingManager#mode is process) Source

token

nullable

token: string

Token to use for obtaining the automatic shard count, and passing to shards Source

shards

shards: Collection<number, Shard>

A collection of shards that this manager has spawned Source

Methods

createShard

createShard(id?: number): Shard

Creates a single shard. Using this method is usually not necessary if you use the spawn method. Parameters

NameTypeDescription
id?numberId 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

async

async spawn(options?: MultipleShardSpawnOptions): Promise<Collection<number, Shard>>

Spawns multiple shards. Parameters

NameTypeDescription
options?MultipleShardSpawnOptionsOptions for spawning shards

Returns: Promise<Collection<number, Shard>>Source

broadcast

broadcast(message: *): Promise<Array<Shard>>

Sends a message to all shards. Parameters

NameTypeDescription
message*Message to be sent to the shards

Returns: Promise<Array<Shard>>Source

broadcastEval

async

async broadcastEval(script: function, options?: BroadcastEvalOptions): Promise<(*|Array<*>)>

Evaluates a script on all shards, or a given shard, in the context of the Clients. Parameters

NameTypeDescription
scriptfunctionJavaScript to run on each shard
options?BroadcastEvalOptionsThe options for the broadcast Default: {}.

Returns: Promise<(*\|Array<*>)> — Results of the script execution Source

fetchClientValues

fetchClientValues(prop: string, shard?: number): Promise<(*|Array<*>)>

Fetches a client property value of each shard, or a given shard. Parameters

NameTypeDescription
propstringName of the client property to get, using periods for nesting
shard?numberShard to fetch property from, all if undefined

Returns: Promise<(*\|Array<*>)>

js
manager.fetchClientValues('guilds.cache.size')
  .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
  .catch(console.error);

Source

_performOnShards

async · private

async _performOnShards(method: string, args: Array<*>, shard?: number): Promise<(*|Array<*>)>

Runs a method with given arguments on all shards, or a given shard. Parameters

NameTypeDescription
methodstringMethod name to run on each shard
argsArray<*>Arguments to pass through to the method call
shard?numberShard to run on, all if undefined

Returns: Promise<(*\|Array<*>)> — Results of the method execution Source

respawnAll

async

async respawnAll(options?: MultipleShardRespawnOptions): Promise<Collection<number, Shard>>

Kills all running shards and respawns them. Parameters

NameTypeDescription
options?MultipleShardRespawnOptionsOptions for respawning shards

Returns: Promise<Collection<number, Shard>>Source

Events

shardCreate

shardCreate: unknown

Emitted upon creating a shard. Parameters

NameTypeDescription
shardShardShard that was created
Source

Unofficial software. Not affiliated with or supported by Discord.