Skip to content

Commit

Permalink
refactor(ShardClientUtil): use BigInt for shard calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Nov 8, 2020
1 parent 35d9e4d commit 7d2c438
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"plugins": ["import"],
"parserOptions": {
"ecmaVersion": 2019
"ecmaVersion": 2020
},
"env": {
"es6": true,
"es2020": true,
"node": true
},
"overrides": [{ "files": ["*.browser.js"], "env": { "browser": true } }],
Expand Down
2 changes: 2 additions & 0 deletions src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const Messages = {
SHARDING_READY_DISCONNECTED: id => `Shard ${id}'s Client disconnected before becoming ready.`,
SHARDING_READY_DIED: id => `Shard ${id}'s process exited before its Client became ready.`,
SHARDING_NO_CHILD_EXISTS: id => `Shard ${id} has no active process or worker.`,
SHARDING_SHARD_MISCALCULATION: (shard, guild, count) =>
`Calculated invalid shard ${shard} for guild ${guild} with ${count} shards.`,

COLOR_RANGE: 'Color must be within the range 0 - 16777215 (0xFFFFFF).',
COLOR_CONVERT: 'Unable to convert color to a number.',
Expand Down
7 changes: 3 additions & 4 deletions src/sharding/ShardClientUtil.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { Events } = require('../util/Constants');
const Snowflake = require('../util/Snowflake');
const Util = require('../util/Util');

/**
Expand Down Expand Up @@ -235,9 +234,9 @@ class ShardClientUtil {
* @returns {number}
*/
static shardIdForGuildId(guildId, shardCount) {
// This performs (guild_id >> 22) % num_shards, but with a string Snowflake
const timestamp = Snowflake.deconstruct(guildId).timestamp;
return (timestamp - Snowflake.epoch) % shardCount;
const shard = Number(BigInt(guildId) >> 22n) % shardCount;
if (shard < 0 || shard >= shardCount) throw new Error('SHARDING_SHARD_MISCALCULATION', guildId, shardCount, shard);
return shard;
}
}

Expand Down

0 comments on commit 7d2c438

Please sign in to comment.