Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shards): Support Non-Sequential Shard IDs #1389

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/sharding.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Eris = require("eris");
const bot = new Eris("Bot TOKEN", {
firstShardID: 0,
lastShardID: 15,
shardIDs: [5, 9, 1, 4], // Overrides firstShardID and lastShardID above
DonovanDMC marked this conversation as resolved.
Show resolved Hide resolved
maxShards: 16,
getAllUsers: false,
intents: ["guilds", "guildMembers", "guildPresences"]
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ declare namespace Eris {
restMode?: boolean;
seedVoiceConnections?: boolean;
shardConcurrency?: number | "auto";
shardIDs?: number[];
ws?: unknown;
}
interface CommandClientOptions {
Expand Down
20 changes: 18 additions & 2 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Client extends EventEmitter {
* @arg {Boolean} [options.restMode=false] Whether to enable getting objects over REST. Even with this option enabled, it is recommended that you check the cache first before using REST
* @arg {Boolean} [options.seedVoiceConnections=false] Whether to populate bot.voiceConnections with existing connections the bot account has during startup. Note that this will disconnect connections from other bot sessions
* @arg {Number | String} [options.shardConcurrency="auto"] The number of shards that can start simultaneously. If "auto" Eris will use Discord's recommended shard concurrency.
* @arg {Array<Number>} [options.shardIDs] An array of shard IDs to run for this client. If this is specified, the firstShardID and lastShardID options are ignored.
* @arg {Object} [options.ws] An object of WebSocket options to pass to the shard WebSocket constructors
*/
constructor(token, options) {
Expand Down Expand Up @@ -151,6 +152,7 @@ class Client extends EventEmitter {
restMode: false,
seedVoiceConnections: false,
shardConcurrency: "auto",
shardIDs: [],
ws: {},
reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000
}, options);
Expand Down Expand Up @@ -458,8 +460,22 @@ class Client extends EventEmitter {
this.shards.setConcurrency(data.session_start_limit.max_concurrency);
}

for(let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) {
this.shards.spawn(i);
// ensure we have a valid shardIDs array
let shardIDs = this.options.shardIDs;
if(!Array.isArray(shardIDs)) {
shardIDs = [];
}

// populate array via first/last shardID if no elements are present
if(shardIDs.length === 0) {
for(let i = this.options.firstShardID; i <= this.options.lastShardID; i++) {
shardIDs.push(i);
}
}

// spawn the shards
for(const id of shardIDs) {
this.shards.spawn(id);
}
} catch(err) {
if(!this.options.autoreconnect) {
Expand Down