Preview environment

class Shard

extends

EventEmitter
export class Shard extends EventEmitter

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.

args : string[]

Arguments for the shard's process/worker

env : unknown

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

execArgv : string[]

Arguments for the shard's process/worker executable

id : number

The shard's id in the manager

manager : ShardingManager

Manager that created the shard

process : ChildProcess | null

Process of the shard (if mode is process)

ready : boolean

Whether the shard's Client is ready

silent : boolean

Whether to pass silent flag to the shard's process (only when mode is process)

worker : Worker | null

Worker of the shard (if mode is worker)

eval(
script: string
) : Promise<unknown>

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

Returns: Result of the script execution

fetchClientValue(
prop: string
) : Promise<unknown>

Fetches a client property value of the shard.

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

kill() : void

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

on<

Event extends keyof ShardEventTypes

>(
event: Event
listener: (...args: ShardEventTypes[Event]) => void
) : this

once<

Event extends keyof ShardEventTypes

>(
event: Event
listener: (...args: ShardEventTypes[Event]) => void
) : this

respawn(
options?: { delay?: number; timeout?: number }
) : Promise<ChildProcess>

Kills and restarts the shard's process/worker.

send(
message: unknown
) : Promise<Shard>

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

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.

death(
process: ChildProcess | Worker
)

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

disconnect()

Emitted upon the shard's shardDisconnect event.

message(
message: any
)

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

reconnecting()

Emitted upon the shard's shardReconnecting event.

resume()

Emitted upon the shard's shardResume event.

spawn(
process: ChildProcess | Worker
)

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