From 7d2c438caadf131ebefcf60aed0606f94b8c1e41 Mon Sep 17 00:00:00 2001 From: MattIPv4 Date: Sun, 8 Nov 2020 15:37:22 +0000 Subject: [PATCH] refactor(ShardClientUtil): use BigInt for shard calculation --- .eslintrc.json | 4 ++-- src/errors/Messages.js | 2 ++ src/sharding/ShardClientUtil.js | 7 +++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index fc7436fc7fd2e..d9f2f558a6d74 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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 } }], diff --git a/src/errors/Messages.js b/src/errors/Messages.js index 50b85145885d2..a68cd2da224a0 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -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.', diff --git a/src/sharding/ShardClientUtil.js b/src/sharding/ShardClientUtil.js index 510341d6b8d7f..51d8fae307926 100644 --- a/src/sharding/ShardClientUtil.js +++ b/src/sharding/ShardClientUtil.js @@ -1,7 +1,6 @@ 'use strict'; const { Events } = require('../util/Constants'); -const Snowflake = require('../util/Snowflake'); const Util = require('../util/Util'); /** @@ -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; } }