Skip to content

Shard ​

classe · deprecated · Source

A self-contained shard created by the ShardingManager. Each one has a ChildProcess that contains an instance of the bot and its Client. When its child process/worker exits for any reason, the shard will spawn a new one to replace it as necessary.

Extends: EventEmitter

Properties ​

manager ​

manager: ShardingManager

Manager that created the shard Source

id ​

id: number

The shard's id in the manager Source

args ​

args: Array<string>

Arguments for the shard's process (only when ShardingManager#mode is process) Source

execArgv ​

execArgv: Array<string>

Arguments for the shard's process executable (only when ShardingManager#mode is process) Source

env ​

env: Object

Environment variables for the shard's process, or workerData for the shard's worker Source

ready ​

ready: boolean

Whether the shard's Client is ready Source

process ​

nullable

process: ChildProcess

Process of the shard (if ShardingManager#mode is process) Source

worker ​

nullable

worker: Worker

Worker of the shard (if ShardingManager#mode is worker) Source

_evals ​

private

_evals: Map<string, Object>

Ongoing operations for calls to Shard#eval, mapped by the script they were called with Source

_fetches ​

private

_fetches: Map<string, Object>

Ongoing operations for calls to Shard#fetchClientValue, mapped by the prop they were called with Source

_exitListener ​

private

_exitListener: function

Listener function for the ChildProcess' exit event Source

Methods ​

spawn ​

spawn(timeout?: number): Promise<ChildProcess>

Forks a child process or creates a worker thread for the shard. You should not need to call this manually. Parameters

NameTypeDescription
timeout?numberThe amount in milliseconds to wait until the Client has become ready
before resolving (-1 or Infinity for no wait) Default: 30000.

Returns: Promise<ChildProcess>Source

kill ​

kill()

Immediately kills the shard's process/worker and does not restart it. Source

respawn ​

async

async respawn(options?: ShardRespawnOptions): Promise<ChildProcess>

Kills and restarts the shard's process/worker. Parameters

NameTypeDescription
options?ShardRespawnOptionsOptions for respawning the shard

Returns: Promise<ChildProcess>Source

send ​

send(message: *): Promise<Shard>

Sends a message to the shard's process/worker. Parameters

NameTypeDescription
message*Message to send to the shard

Returns: Promise<Shard>Source

fetchClientValue ​

async

async fetchClientValue(prop: string): Promise<*>

Fetches a client property value of the shard. Parameters

NameTypeDescription
propstringName of the client property to get, using periods for nesting

Returns: Promise<*>

js
shard.fetchClientValue('guilds.cache.size')
  .then(count => console.log(`${count} guilds in shard ${shard.id}`))
  .catch(console.error);

Source

eval ​

async

async eval(script: string | function, context?: *): Promise<*>

Evaluates a script or function on the shard, in the context of the Client. Parameters

NameTypeDescription
scriptstring | functionJavaScript to run on the shard
context?*The context for the eval

Returns: Promise<*> — Result of the script execution Source

_createPendingOperation ​

private

_createPendingOperation(operations: Map<string, Object>, key: string, messageKey: string): Promise<*>

Creates an IPC operation that can be cleaned up and rejected if the child exits. Parameters

NameTypeDescription
operationsMap<string, Object>Collection that owns the operation
keystringOperation identifier
messageKeystringIPC message property used for the identifier

Returns: Promise<*>Source

_handleMessage ​

private

_handleMessage(message: *)

Handles a message received from the child process/worker. Parameters

NameTypeDescription
message*Message received
Source

_handleExit ​

private

_handleExit(respawn?: boolean, timeout?: number)

Handles the shard's process/worker exiting. Parameters

NameTypeDescription
respawn?booleanWhether to spawn the shard again Default: this.manager.respawn.
timeout?numberThe amount in milliseconds to wait until the Client
has become ready (-1 or Infinity for no wait)
Source

incrementMaxListeners ​

private

incrementMaxListeners(emitter: EventEmitter | process)

Increments max listeners by one for a given emitter, if they are not zero. Parameters

NameTypeDescription
emitterEventEmitter | processThe emitter that emits the events.
Source

decrementMaxListeners ​

private

decrementMaxListeners(emitter: EventEmitter | process)

Decrements max listeners by one for a given emitter, if they are not zero. Parameters

NameTypeDescription
emitterEventEmitter | processThe emitter that emits the events.
Source

Events ​

spawn ​

spawn: unknown

Emitted upon the creation of the shard's child process/worker. Parameters

NameTypeDescription
processChildProcess | WorkerChild process/worker that was created
Source

ready ​

ready: unknown

Emitted upon the shard's Client#event:shardReady event. Source

disconnect ​

disconnect: unknown

Emitted upon the shard's Client#event:shardDisconnect event. Source

reconnecting ​

reconnecting: unknown

Emitted upon the shard's Client#event:shardReconnecting event. Source

message ​

message: unknown

Emitted upon receiving a message from the child process/worker. Parameters

NameTypeDescription
message*Message that was received
Source

death ​

death: unknown

Emitted upon the shard's child process/worker exiting. Parameters

NameTypeDescription
processChildProcess | WorkerChild process/worker that exited
Source

Unofficial software. Not affiliated with or supported by Discord.