From 00e3708e78aa071a68a33f118bba2138cf2c3e40 Mon Sep 17 00:00:00 2001 From: Brian Tanner Date: Tue, 31 May 2016 13:19:15 -0400 Subject: [PATCH] add guild sharding support (#393) * add guild sharding support * squash if statements --- lib/Client/Client.js | 7 +++++++ lib/Client/InternalClient.js | 11 ++++++++--- src/Client/Client.js | 7 +++++++ src/Client/InternalClient.js | 11 ++++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 53491966f..c9cbc265a 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -77,6 +77,13 @@ var Client = (function (_EventEmitter) { this.options.largeThreshold = options.largeThreshold || 250; this.options.maxCachedMessages = options.maxCachedMessages || 1000; this.options.guildCreateTimeout = options.guildCreateTimeout || 1000; + this.options.shardId = options.shardId || 0; + this.options.shardCount = options.shardCount || 0; + + if (typeof options.shardCount === "number" && typeof options.shardId === "number" && options.shardCount > 0) { + this.options.shard = [options.shardId, options.shardCount]; + } + /** * Internal Client that the Client wraps around. * @readonly diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index bfe155311..e138c7b08 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -1649,8 +1649,7 @@ var InternalClient = (function () { this.websocket = new _ws2["default"](url); this.websocket.onopen = function () { - - self.sendWS({ + var data = { op: 2, d: { token: self.token, @@ -1665,7 +1664,13 @@ var InternalClient = (function () { "$referring_domain": "discord.js" } } - }); + }; + + if (self.client.options.shard) { + data.d.shard = self.client.options.shard; + } + + self.sendWS(data); }; this.websocket.onclose = function (code) { diff --git a/src/Client/Client.js b/src/Client/Client.js index 0f19f718f..832210f2d 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -51,6 +51,13 @@ export default class Client extends EventEmitter { this.options.largeThreshold = options.largeThreshold || 250; this.options.maxCachedMessages = options.maxCachedMessages || 1000; this.options.guildCreateTimeout = options.guildCreateTimeout || 1000; + this.options.shardId = options.shardId || 0; + this.options.shardCount = options.shardCount || 0; + + if (typeof options.shardCount === "number" && typeof options.shardId === "number" && options.shardCount > 0) { + this.options.shard = [options.shardId, options.shardCount]; + } + /** * Internal Client that the Client wraps around. * @readonly diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index c2e385ec4..a30cfd68f 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -1430,8 +1430,7 @@ export default class InternalClient { this.websocket = new WebSocket(url); this.websocket.onopen = () => { - - self.sendWS({ + var data = { op: 2, d: { token: self.token, @@ -1446,7 +1445,13 @@ export default class InternalClient { "$referring_domain": "discord.js" } } - }); + }; + + if (self.client.options.shard) { + data.d.shard = self.client.options.shard; + } + + self.sendWS(data); }; this.websocket.onclose = (code) => {