refactor: ES2021 features (#6540)

Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
This commit is contained in:
Rodry
2021-09-03 12:58:01 +01:00
committed by GitHub
parent d81590d566
commit 00bd92a451
35 changed files with 145 additions and 156 deletions

View File

@@ -2,6 +2,7 @@
const EventEmitter = require('events');
const { Collection } = require('@discordjs/collection');
const { RPCErrorCodes } = require('discord-api-types/v9');
const WebSocketShard = require('./WebSocketShard');
const PacketHandlers = require('./handlers');
const { Error } = require('../../errors');
@@ -19,7 +20,11 @@ const BeforeReadyWhitelist = [
];
const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1).map(Number);
const UNRESUMABLE_CLOSE_CODES = [1000, 4006, 4007];
const UNRESUMABLE_CLOSE_CODES = [
RPCErrorCodes.UnknownError,
RPCErrorCodes.InvalidPermissions,
RPCErrorCodes.InvalidClientId,
];
/**
* The WebSocket manager for this client.
@@ -184,7 +189,7 @@ class WebSocketManager extends EventEmitter {
});
shard.on(ShardEvents.CLOSE, event => {
if (event.code === 1000 ? this.destroyed : UNRECOVERABLE_CLOSE_CODES.includes(event.code)) {
if (event.code === 1_000 ? this.destroyed : UNRECOVERABLE_CLOSE_CODES.includes(event.code)) {
/**
* Emitted when a shard's WebSocket disconnects and will no longer reconnect.
* @event Client#shardDisconnect
@@ -253,7 +258,7 @@ class WebSocketManager extends EventEmitter {
// If we have more shards, add a 5s delay
if (this.shardQueue.size) {
this.debug(`Shard Queue Size: ${this.shardQueue.size}; continuing in 5 seconds...`);
await Util.delayFor(5000);
await Util.delayFor(5_000);
return this.createShards();
}
@@ -274,7 +279,7 @@ class WebSocketManager extends EventEmitter {
this.debug(`Couldn't reconnect or fetch information about the gateway. ${error}`);
if (error.httpStatus !== 401) {
this.debug(`Possible network error occurred. Retrying in 5s...`);
await Util.delayFor(5000);
await Util.delayFor(5_000);
this.reconnecting = false;
return this.reconnect();
}
@@ -316,7 +321,7 @@ class WebSocketManager extends EventEmitter {
this.debug(`Manager was destroyed. Called by:\n${new Error('MANAGER_DESTROYED').stack}`);
this.destroyed = true;
this.shardQueue.clear();
for (const shard of this.shards.values()) shard.destroy({ closeCode: 1000, reset: true, emit: false, log: false });
for (const shard of this.shards.values()) shard.destroy({ closeCode: 1_000, reset: true, emit: false, log: false });
}
/**

View File

@@ -415,7 +415,7 @@ class WebSocketShard extends EventEmitter {
break;
case Opcodes.RECONNECT:
this.debug('[RECONNECT] Discord asked us to reconnect');
this.destroy({ closeCode: 4000 });
this.destroy({ closeCode: 4_000 });
break;
case Opcodes.INVALID_SESSION:
this.debug(`[INVALID SESSION] Resumable: ${packet.d}.`);
@@ -489,7 +489,7 @@ class WebSocketShard extends EventEmitter {
this.emit(ShardEvents.ALL_READY, this.expectedGuilds);
},
hasGuildsIntent ? 15000 : 0,
hasGuildsIntent ? 15_000 : 0,
).unref();
}
@@ -511,7 +511,7 @@ class WebSocketShard extends EventEmitter {
this.helloTimeout = setTimeout(() => {
this.debug('Did not receive HELLO in time. Destroying and connecting again.');
this.destroy({ reset: true, closeCode: 4009 });
}, 20000).unref();
}, 20_000).unref();
}
/**
@@ -656,7 +656,7 @@ class WebSocketShard extends EventEmitter {
_send(data) {
if (this.connection?.readyState !== WebSocket.OPEN) {
this.debug(`Tried to send packet '${JSON.stringify(data)}' but no WebSocket is available!`);
this.destroy({ closeCode: 4000 });
this.destroy({ closeCode: 4_000 });
return;
}
@@ -692,7 +692,7 @@ class WebSocketShard extends EventEmitter {
* @param {Object} [options={ closeCode: 1000, reset: false, emit: true, log: true }] Options for destroying the shard
* @private
*/
destroy({ closeCode = 1000, reset = false, emit = true, log = true } = {}) {
destroy({ closeCode = 1_000, reset = false, emit = true, log = true } = {}) {
if (log) {
this.debug(`[DESTROY]
Close Code : ${closeCode}