ShardingManager

export class ShardingManager extends EventEmitter
export class ShardingManager extends EventEmitter
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 or 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.

Extends

EventEmitter
constructor(file, options?)
NameTypeOptionalDescription
filestringNoNone
optionsShardingManagerOptionsYesNone
shardCreate
Emitted upon creating a shard.
NameTypeOptionalDescription
shardShardNoShard that was created
execArgv:Array<string>
An array of arguments to pass to the executable (only when mode is process)
file:string
Path to the shard script file
Mode for shards to spawn with
respawn:boolean
Whether shards should automatically respawn upon exiting
shardArgs:string[]
An array of arguments to pass to shards (only when mode is process)
shardList:number[] | 'auto'
List of shards this sharding manager spawns
A collection of shards that this manager has spawned
silent:boolean
Whether to pass the silent flag to child process (only when mode is process)
token:string | null
Token to use for obtaining the automatic shard count, and passing to shards
totalShards:number | 'auto'
Amount of shards that all sharding managers spawn in total
broadcast(message):Promise<Shard[]>
Sends a message to all shards.
NameTypeOptionalDescription
messageunknownNoMessage to be sent to the shards
broadcastEval(script):Promise<Serialized<Result>[]>
Evaluates a script on all shards, or a given shard, in the context of the Clients.
Returns
Results of the script execution
NameTypeOptionalDescription
script(client: Client) => Awaitable<Result>NoJavaScript to run on each shard
createShard(id):Shard
Creates a single shard. Using this method is usually not necessary if you use the spawn method.
Returns
Note that the created shard needs to be explicitly spawned using its spawn method.
NameTypeOptionalDescription
idnumberNoId of the shard to create This is usually not necessary to manually specify.
fetchClientValues(prop):Promise<unknown[]>
Fetches a client property value of each shard, or a given shard.
Example
manager.fetchClientValues('guilds.cache.size')
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
manager.fetchClientValues('guilds.cache.size')
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
NameTypeOptionalDescription
propstringNoName of the client property to get, using periods for nesting
on(event, listener):this
NameTypeOptionalDescription
event'shardCreate'NoNone
listener(shard: Shard) => voidNoNone
once(event, listener):this
NameTypeOptionalDescription
event'shardCreate'NoNone
listener(shard: Shard) => voidNoNone
respawnAll(options?):Promise<Collection<number, Shard>>
Kills all running shards and respawns them.
NameTypeOptionalDescription
optionsMultipleShardRespawnOptionsYesOptions for respawning shards
spawn(options?):Promise<Collection<number, Shard>>
Spawns multiple shards.
NameTypeOptionalDescription
optionsMultipleShardSpawnOptionsYesOptions for spawning shards