Skip to content

ShardClientUtil

classe · deprecated · Source

Helper class for sharded clients spawned as a child process/worker, such as from a ShardingManager. Utilizes IPC to send and receive data to/from the master process and other shards.

Properties

client

client: Client

Client for the shard Source

mode

mode: ShardingManagerMode

Mode the shard was spawned with Source

parentPort

nullable

parentPort: MessagePort

Message port for the master process (only when ShardClientUtil#mode is worker) Source

ids

readonly

ids: Array<number>

Array of shard ids of this client Source

count

readonly

count: number

Total number of shards Source

Methods

send

send(message: *): Promise<void>

Sends a message to the master process. Parameters

NameTypeDescription
message*Message to send

Returns: Promise<void>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
client.shard.fetchClientValues('guilds.cache.size')
  .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
  .catch(console.error);

Source

broadcastEval

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

Evaluates a script or function 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

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

Source

respawnAll

respawnAll(options?: MultipleShardRespawnOptions): Promise<void>

Requests a respawn of all shards. Parameters

NameTypeDescription
options?MultipleShardRespawnOptionsOptions for respawning shards

Returns: Promise<void> — Resolves upon the message being sent Source

_handleMessage

async · private

async _handleMessage(message: *)

Handles an IPC message. Parameters

NameTypeDescription
message*Message received
Source

_respond

private

_respond(type: string, message: *)

Sends a message to the master process, emitting an error from the client upon failure. Parameters

NameTypeDescription
typestringType of response to send
message*Message to send
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

singleton

static

singleton(client: Client, mode: ShardingManagerMode): ShardClientUtil

Creates/gets the singleton of this class. Parameters

NameTypeDescription
clientClientThe client to use
modeShardingManagerModeMode the shard was spawned with

Returns: ShardClientUtilSource

shardIdForGuildId

static

shardIdForGuildId(guildId: Snowflake, shardCount: number): number

Get the shard id for a given guild id. Parameters

NameTypeDescription
guildIdSnowflakeSnowflake guild id to get shard id for
shardCountnumberNumber of shards

Returns: numberSource

Unofficial software. Not affiliated with or supported by Discord.