ShardClientUtil

export 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(script):Promise<Serialized<Result>[]>
Evaluates a script or function on all shards, or a given shard, in the context of the Clients.
Returns
Results of the script execution
Example
client.shard.broadcastEval(client => client.guilds.cache.size)
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
client.shard.broadcastEval(client => client.guilds.cache.size)
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
NameConstraintsOptionalDefaultDescription
ResultNoNone
NameTypeOptionalDescription
script(client: Client) => Awaitable<Result>No
JavaScript to run on each shard
fetchClientValues(prop):Promise<unknown[]>
Fetches a client property value of each shard, or a given shard.
Example
client.shard.fetchClientValues('guilds.cache.size')
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
client.shard.fetchClientValues('guilds.cache.size')
.then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
.catch(console.error);
NameTypeOptionalDescription
propstringNo
Name of the client property to get, using periods for nesting
respawnAll(options?):Promise<void>
Requests a respawn of all shards.
Returns
Resolves upon the message being sent
NameTypeOptionalDescription
optionsMultipleShardRespawnOptionsYes
Options for respawning shards
send(message):Promise<void>
Sends a message to the master process.
NameTypeOptionalDescription
messageunknownNo
Message to send
Static
shardIdForGuildId(guildId, shardCount):number
Get the shard id for a given guild id.
NameTypeOptionalDescription
guildIdSnowflakeNo
Snowflake guild id to get shard id for
shardCountnumberNo
Number of shards
Static
singleton(client, mode):ShardClientUtil
Creates/gets the singleton of this class.
NameTypeOptionalDescription
clientClient<true>No
The client to use
modeShardingManagerModeNo
Mode the shard was spawned with