refactor(ShardClientUtil): logic de-duplication (#9491)

added docs to the `calculateShardId` function
This commit is contained in:
Almeida
2023-05-01 21:08:45 +01:00
committed by GitHub
parent 54ceedf6c5
commit a9f2bff82a
2 changed files with 9 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
const process = require('node:process');
const { calculateShardId } = require('@discordjs/util');
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
const Events = require('../util/Events');
const { makeError, makePlainError } = require('../util/Util');
@@ -251,7 +252,7 @@ class ShardClientUtil {
* @returns {number}
*/
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);
return shard;
}

View File

@@ -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) {
return Number((BigInt(guildId) >> 22n) % BigInt(shardCount));
return Number(BigInt(guildId) >> 22n) % shardCount;
}