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

Custom reconnect #547

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ declare namespace Eris {
}
type PossiblyUncachedMessage = Message | { id: string, channel: TextableChannel };
interface RawPacket { op: number; t?: string; d?: any; s?: number; }
type ReconnectDelayFunction = (lastDelay: number, attempts: number) => number;
interface ClientOptions {
autoreconnect?: boolean;
compress?: boolean;
Expand All @@ -465,7 +466,9 @@ declare namespace Eris {
defaultImageSize?: number;
ws?: any;
latencyThreshold?: number;
agent?: HTTPSAgent
agent?: HTTPSAgent;
reconnectAttempts: number;
reconnectDelay: ReconnectDelayFunction;
}
interface CommandClientOptions {
defaultHelpCommand?: boolean;
Expand Down
14 changes: 11 additions & 3 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Client extends EventEmitter {
* @arg {Number} [options.defaultImageSize=128] The default size to return user avatars, guild icons, banners, splashes, and group icons. Can be any power of two between 16 and 2048. If the height and width are different, the width will be the value specified, and the height relative to that
* @arg {Object} [options.ws] An object of WebSocket options to pass to the shard WebSocket constructors
* @arg {Object} [options.agent] A HTTP Agent used to proxy requests
* @arg {Number} [options.maxReconnectAttempts=Infinity] The maximum amount of times that the client is allowed to try to reconnect to Discord.
* @arg {Function} [options.reconnectDelay] A function which returns how long the bot should wait until reconnecting to Discord.
*/
constructor(token, options) {
super();
Expand All @@ -124,7 +126,9 @@ class Client extends EventEmitter {
restMode: false,
seedVoiceConnections: false,
ws: {},
agent: null
agent: null,
maxReconnectAttempts: Infinity,
reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000
}, options);
if(this.options.lastShardID === undefined && this.options.maxShards !== "auto") {
this.options.lastShardID = this.options.maxShards - 1;
Expand Down Expand Up @@ -177,6 +181,8 @@ class Client extends EventEmitter {
this.voiceConnections = new VoiceConnectionManager();

this.connect = this.connect.bind(this);
this.lastReconnectDelay = 0;
this.reconnectAttempts = 0;
}

get uptime() {
Expand Down Expand Up @@ -222,8 +228,10 @@ class Client extends EventEmitter {
if(!this.options.autoreconnect) {
throw err;
}
this.emit("error", err);
await sleep(2000);
const reconnectDelay = this.options.reconnectDelay(this.lastReconnectDelay, this.reconnectAttempts);
await sleep(reconnectDelay);
this.lastReconnectDelay = reconnectDelay;
this.reconnectAttempts = this.reconnectAttempts + 1;
return this.connect();
}
}
Expand Down