mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
Add client ping stuff
This commit is contained in:
@@ -148,6 +148,13 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
this.readyAt = null;
|
||||
|
||||
/**
|
||||
* The previous heartbeat pings of the websocket (most recent first, limited to three elements)
|
||||
* @type {number[]}
|
||||
*/
|
||||
this.pings = [];
|
||||
|
||||
this._pingTimestamp = 0;
|
||||
this._timeouts = new Set();
|
||||
this._intervals = new Set();
|
||||
|
||||
@@ -174,6 +181,15 @@ class Client extends EventEmitter {
|
||||
return this.readyAt ? Date.now() - this.readyAt : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The average heartbeat ping of the websocket
|
||||
* @type {number}
|
||||
* @readonly
|
||||
*/
|
||||
get ping() {
|
||||
return this.pings.reduce((prev, p) => prev + p, 0) / this.pings.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a collection, mapping guild ID to voice connections.
|
||||
* @type {Collection<string, VoiceConnection>}
|
||||
@@ -392,6 +408,11 @@ class Client extends EventEmitter {
|
||||
this._intervals.delete(interval);
|
||||
}
|
||||
|
||||
_pong(startTime) {
|
||||
this.pings.unshift(Date.now() - startTime);
|
||||
if (this.pings.length > 3) this.pings.length = 3;
|
||||
}
|
||||
|
||||
_setPresence(id, presence) {
|
||||
if (this.presences.get(id)) {
|
||||
this.presences.get(id).update(presence);
|
||||
|
||||
Reference in New Issue
Block a user