mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
refactor(ShardClientUtil): logic de-duplication (#9491)
added docs to the `calculateShardId` function
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const process = require('node:process');
|
const process = require('node:process');
|
||||||
|
const { calculateShardId } = require('@discordjs/util');
|
||||||
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
|
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||||
const Events = require('../util/Events');
|
const Events = require('../util/Events');
|
||||||
const { makeError, makePlainError } = require('../util/Util');
|
const { makeError, makePlainError } = require('../util/Util');
|
||||||
@@ -251,7 +252,7 @@ class ShardClientUtil {
|
|||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
static shardIdForGuildId(guildId, shardCount) {
|
static shardIdForGuildId(guildId, shardCount) {
|
||||||
const shard = Number(BigInt(guildId) >> 22n) % shardCount;
|
const shard = calculateShardId(guildId, shardCount);
|
||||||
if (shard < 0) throw new DiscordjsError(ErrorCodes.ShardingShardMiscalculation, shard, guildId, shardCount);
|
if (shard < 0) throw new DiscordjsError(ErrorCodes.ShardingShardMiscalculation, shard, guildId, shardCount);
|
||||||
return shard;
|
return shard;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Calculates the shard id for a given guild id.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id to calculate the shard id for
|
||||||
|
* @param shardCount - The total number of shards
|
||||||
|
*/
|
||||||
export function calculateShardId(guildId: string, shardCount: number) {
|
export function calculateShardId(guildId: string, shardCount: number) {
|
||||||
return Number((BigInt(guildId) >> 22n) % BigInt(shardCount));
|
return Number(BigInt(guildId) >> 22n) % shardCount;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user