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);
NameTypeOptionalDescription
script(client: Client) => Awaitable<Result>NoJavaScript 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
propstringNoName 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
optionsMultipleShardRespawnOptionsYesOptions for respawning shards
send(message):Promise<void>
Sends a message to the master process.
NameTypeOptionalDescription
messageunknownNoMessage to send
Static
shardIdForGuildId(guildId, shardCount):number
Get the shard id for a given guild id.
NameTypeOptionalDescription
guildIdSnowflakeNoSnowflake guild id to get shard id for
shardCountnumberNoNumber of shards
Static
singleton(client, mode):ShardClientUtil
Creates/gets the singleton of this class.
NameTypeOptionalDescription
clientClient<true>NoThe client to use
modeShardingManagerModeNoMode the shard was spawned with