Preview environment

class ShardClientUtil

export class ShardClientUtil

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

client : Client

Client for the shard

readonly
count : number

Total number of shards

readonly
ids : number[]

Array of shard ids of this client

Mode the shard was spawned with

parentPort : MessagePort | null

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

broadcastEval<

Result

>(
script: (client: Client) => Awaitable<Result>
) : Promise<Serialized<Result>[]>

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

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

Returns: Results of the script execution

fetchClientValues(
prop: string
) : Promise<unknown[]>

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

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

respawnAll() : Promise<void>

Requests a respawn of all shards.

Returns: Resolves upon the message being sent

send(
message: unknown
) : Promise<void>

Sends a message to the master process.

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

Get the shard id for a given guild id.

static
singleton(
client: Client<true>
) : ShardClientUtil

Creates/gets the singleton of this class.