Skip to content

Commit

Permalink
refactor(ShardClientUtil): logic de-duplication (#9491)
Browse files Browse the repository at this point in the history
added docs to the `calculateShardId` function
  • Loading branch information
almeidx committed May 1, 2023
1 parent 54ceedf commit a9f2bff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/discord.js/src/sharding/ShardClientUtil.js
@@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/util/src/functions/calculateShardId.ts
@@ -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;
}

0 comments on commit a9f2bff

Please sign in to comment.