Downgrade to v13
[vi] cảm giác đau khổ
This commit is contained in:
@@ -4,32 +4,28 @@ const EventEmitter = require('node:events');
|
||||
const { setImmediate } = require('node:timers');
|
||||
const { setTimeout: sleep } = require('node:timers/promises');
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const { GatewayCloseCodes, GatewayDispatchEvents, Routes } = require('discord-api-types/v9');
|
||||
const { RPCErrorCodes } = require('discord-api-types/v9');
|
||||
const WebSocketShard = require('./WebSocketShard');
|
||||
const PacketHandlers = require('./handlers');
|
||||
const { Error } = require('../../errors');
|
||||
const Events = require('../../util/Events');
|
||||
const ShardEvents = require('../../util/ShardEvents');
|
||||
const Status = require('../../util/Status');
|
||||
const { Events, ShardEvents, Status, WSCodes, WSEvents } = require('../../util/Constants');
|
||||
|
||||
const BeforeReadyWhitelist = [
|
||||
GatewayDispatchEvents.Ready,
|
||||
GatewayDispatchEvents.Resumed,
|
||||
GatewayDispatchEvents.GuildCreate,
|
||||
GatewayDispatchEvents.GuildDelete,
|
||||
GatewayDispatchEvents.GuildMembersChunk,
|
||||
GatewayDispatchEvents.GuildMemberAdd,
|
||||
GatewayDispatchEvents.GuildMemberRemove,
|
||||
WSEvents.READY,
|
||||
WSEvents.RESUMED,
|
||||
WSEvents.GUILD_CREATE,
|
||||
WSEvents.GUILD_DELETE,
|
||||
WSEvents.GUILD_MEMBERS_CHUNK,
|
||||
WSEvents.GUILD_MEMBER_ADD,
|
||||
WSEvents.GUILD_MEMBER_REMOVE,
|
||||
];
|
||||
|
||||
const UNRECOVERABLE_CLOSE_CODES = [
|
||||
GatewayCloseCodes.AuthenticationFailed,
|
||||
GatewayCloseCodes.InvalidShard,
|
||||
GatewayCloseCodes.ShardingRequired,
|
||||
GatewayCloseCodes.InvalidIntents,
|
||||
GatewayCloseCodes.DisallowedIntents,
|
||||
const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1).map(Number);
|
||||
const UNRESUMABLE_CLOSE_CODES = [
|
||||
RPCErrorCodes.UnknownError,
|
||||
RPCErrorCodes.InvalidPermissions,
|
||||
RPCErrorCodes.InvalidClientId,
|
||||
];
|
||||
const UNRESUMABLE_CLOSE_CODES = [1000, GatewayCloseCodes.AlreadyAuthenticated, GatewayCloseCodes.InvalidSeq];
|
||||
|
||||
/**
|
||||
* The WebSocket manager for this client.
|
||||
@@ -88,7 +84,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* The current status of this WebSocketManager
|
||||
* @type {Status}
|
||||
*/
|
||||
this.status = Status.Idle;
|
||||
this.status = Status.IDLE;
|
||||
|
||||
/**
|
||||
* If this manager was destroyed. It will prevent shards from reconnecting
|
||||
@@ -122,7 +118,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
debug(message, shard) {
|
||||
this.client.emit(Events.Debug, `[WS => ${shard ? `Shard ${shard.id}` : 'Manager'}] ${message}`);
|
||||
this.client.emit(Events.DEBUG, `[WS => ${shard ? `Shard ${shard.id}` : 'Manager'}] ${message}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +126,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
async connect() {
|
||||
const invalidToken = new Error(GatewayCloseCodes[GatewayCloseCodes.AuthenticationFailed]);
|
||||
const invalidToken = new Error(WSCodes[4004]);
|
||||
const {
|
||||
url: gatewayURL,
|
||||
shards: recommendedShards,
|
||||
@@ -180,20 +176,20 @@ class WebSocketManager extends EventEmitter {
|
||||
this.shardQueue.delete(shard);
|
||||
|
||||
if (!shard.eventsAttached) {
|
||||
shard.on(ShardEvents.AllReady, unavailableGuilds => {
|
||||
shard.on(ShardEvents.ALL_READY, unavailableGuilds => {
|
||||
/**
|
||||
* Emitted when a shard turns ready.
|
||||
* @event Client#shardReady
|
||||
* @param {number} id The shard id that turned ready
|
||||
* @param {?Set<Snowflake>} unavailableGuilds Set of unavailable guild ids, if any
|
||||
*/
|
||||
this.client.emit(Events.ShardReady, shard.id, unavailableGuilds);
|
||||
this.client.emit(Events.SHARD_READY, shard.id, unavailableGuilds);
|
||||
|
||||
if (!this.shardQueue.size) this.reconnecting = false;
|
||||
this.checkShardsReady();
|
||||
});
|
||||
|
||||
shard.on(ShardEvents.Close, event => {
|
||||
shard.on(ShardEvents.CLOSE, event => {
|
||||
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.
|
||||
@@ -201,8 +197,8 @@ class WebSocketManager extends EventEmitter {
|
||||
* @param {CloseEvent} event The WebSocket close event
|
||||
* @param {number} id The shard id that disconnected
|
||||
*/
|
||||
this.client.emit(Events.ShardDisconnect, event, shard.id);
|
||||
this.debug(GatewayCloseCodes[event.code], shard);
|
||||
this.client.emit(Events.SHARD_DISCONNECT, event, shard.id);
|
||||
this.debug(WSCodes[event.code], shard);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -216,7 +212,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @event Client#shardReconnecting
|
||||
* @param {number} id The shard id that is attempting to reconnect
|
||||
*/
|
||||
this.client.emit(Events.ShardReconnecting, shard.id);
|
||||
this.client.emit(Events.SHARD_RECONNECTING, shard.id);
|
||||
|
||||
this.shardQueue.add(shard);
|
||||
|
||||
@@ -229,14 +225,14 @@ class WebSocketManager extends EventEmitter {
|
||||
}
|
||||
});
|
||||
|
||||
shard.on(ShardEvents.InvalidSession, () => {
|
||||
this.client.emit(Events.ShardReconnecting, shard.id);
|
||||
shard.on(ShardEvents.INVALID_SESSION, () => {
|
||||
this.client.emit(Events.SHARD_RECONNECTING, shard.id);
|
||||
});
|
||||
|
||||
shard.on(ShardEvents.Destroyed, () => {
|
||||
shard.on(ShardEvents.DESTROYED, () => {
|
||||
this.debug('Shard was destroyed but no WebSocket connection was present! Reconnecting...', shard);
|
||||
|
||||
this.client.emit(Events.ShardReconnecting, shard.id);
|
||||
this.client.emit(Events.SHARD_RECONNECTING, shard.id);
|
||||
|
||||
this.shardQueue.add(shard);
|
||||
this.reconnect();
|
||||
@@ -251,7 +247,7 @@ class WebSocketManager extends EventEmitter {
|
||||
await shard.connect();
|
||||
} catch (error) {
|
||||
if (error?.code && UNRECOVERABLE_CLOSE_CODES.includes(error.code)) {
|
||||
throw new Error(GatewayCloseCodes[error.code]);
|
||||
throw new Error(WSCodes[error.code]);
|
||||
// Undefined if session is invalid, error event for regular closes
|
||||
} else if (!error || error.code) {
|
||||
this.debug('Failed to connect to the gateway, requeueing...', shard);
|
||||
@@ -276,7 +272,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async reconnect() {
|
||||
if (this.reconnecting || this.status !== Status.Ready) return false;
|
||||
if (this.reconnecting || this.status !== Status.READY) return false;
|
||||
this.reconnecting = true;
|
||||
try {
|
||||
await this.createShards();
|
||||
@@ -289,14 +285,14 @@ class WebSocketManager extends EventEmitter {
|
||||
return this.reconnect();
|
||||
}
|
||||
// If we get an error at this point, it means we cannot reconnect anymore
|
||||
if (this.client.listenerCount(Events.Invalidated)) {
|
||||
if (this.client.listenerCount(Events.INVALIDATED)) {
|
||||
/**
|
||||
* Emitted when the client's session becomes invalidated.
|
||||
* You are expected to handle closing the process gracefully and preventing a boot loop
|
||||
* if you are listening to this event.
|
||||
* @event Client#invalidated
|
||||
*/
|
||||
this.client.emit(Events.Invalidated);
|
||||
this.client.emit(Events.INVALIDATED);
|
||||
// Destroy just the shards. This means you have to handle the cleanup yourself
|
||||
this.destroy();
|
||||
} else {
|
||||
@@ -337,7 +333,7 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
handlePacket(packet, shard) {
|
||||
if (packet && this.status !== Status.Ready) {
|
||||
if (packet && this.status !== Status.READY) {
|
||||
if (!BeforeReadyWhitelist.includes(packet.t)) {
|
||||
this.packetQueue.push({ packet, shard });
|
||||
return false;
|
||||
@@ -363,8 +359,8 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
checkShardsReady() {
|
||||
if (this.status === Status.Ready) return;
|
||||
if (this.shards.size !== this.totalShards || this.shards.some(s => s.status !== Status.Ready)) {
|
||||
if (this.status === Status.READY) return;
|
||||
if (this.shards.size !== this.totalShards || this.shards.some(s => s.status !== Status.READY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -376,16 +372,16 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
triggerClientReady() {
|
||||
this.status = Status.Ready;
|
||||
this.status = Status.READY;
|
||||
|
||||
this.client.readyTimestamp = Date.now();
|
||||
this.client.readyAt = new Date();
|
||||
|
||||
/**
|
||||
* Emitted when the client becomes ready to start working.
|
||||
* @event Client#ready
|
||||
* @param {Client} client The client
|
||||
*/
|
||||
this.client.emit(Events.ClientReady, this.client);
|
||||
this.client.emit(Events.CLIENT_READY, this.client);
|
||||
|
||||
this.handlePacket();
|
||||
}
|
||||
|
@@ -1,13 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const EventEmitter = require('node:events');
|
||||
const { setTimeout, setInterval, clearTimeout, clearInterval } = require('node:timers');
|
||||
const { GatewayDispatchEvents, GatewayIntentBits, GatewayOpcodes } = require('discord-api-types/v9');
|
||||
const { setTimeout, setInterval } = require('node:timers');
|
||||
const WebSocket = require('../../WebSocket');
|
||||
const Events = require('../../util/Events');
|
||||
const IntentsBitField = require('../../util/IntentsBitField');
|
||||
const ShardEvents = require('../../util/ShardEvents');
|
||||
const Status = require('../../util/Status');
|
||||
const { Status, Events, ShardEvents, Opcodes, WSEvents } = require('../../util/Constants');
|
||||
const Intents = require('../../util/Intents');
|
||||
|
||||
const STATUS_KEYS = Object.keys(Status);
|
||||
const CONNECTION_STATE = Object.keys(WebSocket.WebSocket);
|
||||
@@ -41,7 +38,7 @@ class WebSocketShard extends EventEmitter {
|
||||
* The current status of the shard
|
||||
* @type {Status}
|
||||
*/
|
||||
this.status = Status.Idle;
|
||||
this.status = Status.IDLE;
|
||||
|
||||
/**
|
||||
* The current sequence of the shard
|
||||
@@ -180,17 +177,17 @@ class WebSocketShard extends EventEmitter {
|
||||
connect() {
|
||||
const { gateway, client } = this.manager;
|
||||
|
||||
if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.Ready) {
|
||||
if (this.connection?.readyState === WebSocket.OPEN && this.status === Status.READY) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const cleanup = () => {
|
||||
this.removeListener(ShardEvents.Close, onClose);
|
||||
this.removeListener(ShardEvents.Ready, onReady);
|
||||
this.removeListener(ShardEvents.Resumed, onResumed);
|
||||
this.removeListener(ShardEvents.InvalidSession, onInvalidOrDestroyed);
|
||||
this.removeListener(ShardEvents.Destroyed, onInvalidOrDestroyed);
|
||||
this.removeListener(ShardEvents.CLOSE, onClose);
|
||||
this.removeListener(ShardEvents.READY, onReady);
|
||||
this.removeListener(ShardEvents.RESUMED, onResumed);
|
||||
this.removeListener(ShardEvents.INVALID_SESSION, onInvalidOrDestroyed);
|
||||
this.removeListener(ShardEvents.DESTROYED, onInvalidOrDestroyed);
|
||||
};
|
||||
|
||||
const onReady = () => {
|
||||
@@ -214,11 +211,11 @@ class WebSocketShard extends EventEmitter {
|
||||
reject();
|
||||
};
|
||||
|
||||
this.once(ShardEvents.Ready, onReady);
|
||||
this.once(ShardEvents.Resumed, onResumed);
|
||||
this.once(ShardEvents.Close, onClose);
|
||||
this.once(ShardEvents.InvalidSession, onInvalidOrDestroyed);
|
||||
this.once(ShardEvents.Destroyed, onInvalidOrDestroyed);
|
||||
this.once(ShardEvents.READY, onReady);
|
||||
this.once(ShardEvents.RESUMED, onResumed);
|
||||
this.once(ShardEvents.CLOSE, onClose);
|
||||
this.once(ShardEvents.INVALID_SESSION, onInvalidOrDestroyed);
|
||||
this.once(ShardEvents.DESTROYED, onInvalidOrDestroyed);
|
||||
|
||||
if (this.connection?.readyState === WebSocket.OPEN) {
|
||||
this.debug('An open connection was found, attempting an immediate identify.');
|
||||
@@ -251,7 +248,7 @@ class WebSocketShard extends EventEmitter {
|
||||
Compression: ${zlib ? 'zlib-stream' : 'none'}`,
|
||||
);
|
||||
|
||||
this.status = this.status === Status.Disconnected ? Status.Reconnecting : Status.Connecting;
|
||||
this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING;
|
||||
this.setHelloTimeout();
|
||||
|
||||
this.connectedAt = Date.now();
|
||||
@@ -270,7 +267,7 @@ class WebSocketShard extends EventEmitter {
|
||||
*/
|
||||
onOpen() {
|
||||
this.debug(`[CONNECTED] Took ${Date.now() - this.connectedAt}ms`);
|
||||
this.status = Status.Nearly;
|
||||
this.status = Status.NEARLY;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,11 +293,11 @@ class WebSocketShard extends EventEmitter {
|
||||
try {
|
||||
packet = WebSocket.unpack(raw);
|
||||
} catch (err) {
|
||||
this.manager.client.emit(Events.ShardError, err, this.id);
|
||||
this.manager.client.emit(Events.SHARD_ERROR, err, this.id);
|
||||
return;
|
||||
}
|
||||
this.manager.client.emit(Events.Raw, packet, this.id);
|
||||
if (packet.op === GatewayOpcodes.Dispatch) this.manager.emit(packet.t, packet.d, this.id);
|
||||
this.manager.client.emit(Events.RAW, packet, this.id);
|
||||
if (packet.op === Opcodes.DISPATCH) this.manager.emit(packet.t, packet.d, this.id);
|
||||
this.onPacket(packet);
|
||||
}
|
||||
|
||||
@@ -319,7 +316,7 @@ class WebSocketShard extends EventEmitter {
|
||||
* @param {Error} error The encountered error
|
||||
* @param {number} shardId The shard that encountered this error
|
||||
*/
|
||||
this.manager.client.emit(Events.ShardError, error, this.id);
|
||||
this.manager.client.emit(Events.SHARD_ERROR, error, this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -356,7 +353,7 @@ class WebSocketShard extends EventEmitter {
|
||||
// If we still have a connection object, clean up its listeners
|
||||
if (this.connection) this._cleanupConnection();
|
||||
|
||||
this.status = Status.Disconnected;
|
||||
this.status = Status.DISCONNECTED;
|
||||
|
||||
/**
|
||||
* Emitted when a shard's WebSocket closes.
|
||||
@@ -364,7 +361,7 @@ class WebSocketShard extends EventEmitter {
|
||||
* @event WebSocketShard#close
|
||||
* @param {CloseEvent} event The received event
|
||||
*/
|
||||
this.emit(ShardEvents.Close, event);
|
||||
this.emit(ShardEvents.CLOSE, event);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,28 +376,28 @@ class WebSocketShard extends EventEmitter {
|
||||
}
|
||||
|
||||
switch (packet.t) {
|
||||
case GatewayDispatchEvents.Ready:
|
||||
case WSEvents.READY:
|
||||
/**
|
||||
* Emitted when the shard receives the READY payload and is now waiting for guilds
|
||||
* @event WebSocketShard#ready
|
||||
*/
|
||||
this.emit(ShardEvents.Ready);
|
||||
this.emit(ShardEvents.READY);
|
||||
|
||||
this.sessionId = packet.d.session_id;
|
||||
this.expectedGuilds = new Set(packet.d.guilds.map(d => d.id));
|
||||
this.status = Status.WaitingForGuilds;
|
||||
this.status = Status.WAITING_FOR_GUILDS;
|
||||
this.debug(`[READY] Session ${this.sessionId}.`);
|
||||
this.lastHeartbeatAcked = true;
|
||||
this.sendHeartbeat('ReadyHeartbeat');
|
||||
break;
|
||||
case GatewayDispatchEvents.Resumed: {
|
||||
case WSEvents.RESUMED: {
|
||||
/**
|
||||
* Emitted when the shard resumes successfully
|
||||
* @event WebSocketShard#resumed
|
||||
*/
|
||||
this.emit(ShardEvents.Resumed);
|
||||
this.emit(ShardEvents.RESUMED);
|
||||
|
||||
this.status = Status.Ready;
|
||||
this.status = Status.READY;
|
||||
const replayed = packet.s - this.closeSequence;
|
||||
this.debug(`[RESUMED] Session ${this.sessionId} | Replayed ${replayed} events.`);
|
||||
this.lastHeartbeatAcked = true;
|
||||
@@ -412,16 +409,16 @@ class WebSocketShard extends EventEmitter {
|
||||
if (packet.s > this.sequence) this.sequence = packet.s;
|
||||
|
||||
switch (packet.op) {
|
||||
case GatewayOpcodes.Hello:
|
||||
case Opcodes.HELLO:
|
||||
this.setHelloTimeout(-1);
|
||||
this.setHeartbeatTimer(packet.d.heartbeat_interval);
|
||||
this.identify();
|
||||
break;
|
||||
case GatewayOpcodes.Reconnect:
|
||||
case Opcodes.RECONNECT:
|
||||
this.debug('[RECONNECT] Discord asked us to reconnect');
|
||||
this.destroy({ closeCode: 4_000 });
|
||||
break;
|
||||
case GatewayOpcodes.InvalidSession:
|
||||
case Opcodes.INVALID_SESSION:
|
||||
this.debug(`[INVALID SESSION] Resumable: ${packet.d}.`);
|
||||
// If we can resume the session, do so immediately
|
||||
if (packet.d) {
|
||||
@@ -433,19 +430,19 @@ class WebSocketShard extends EventEmitter {
|
||||
// Reset the session id as it's invalid
|
||||
this.sessionId = null;
|
||||
// Set the status to reconnecting
|
||||
this.status = Status.Reconnecting;
|
||||
this.status = Status.RECONNECTING;
|
||||
// Finally, emit the INVALID_SESSION event
|
||||
this.emit(ShardEvents.InvalidSession);
|
||||
this.emit(ShardEvents.INVALID_SESSION);
|
||||
break;
|
||||
case GatewayOpcodes.HeartbeatAck:
|
||||
case Opcodes.HEARTBEAT_ACK:
|
||||
this.ackHeartbeat();
|
||||
break;
|
||||
case GatewayOpcodes.Heartbeat:
|
||||
case Opcodes.HEARTBEAT:
|
||||
this.sendHeartbeat('HeartbeatRequest', true);
|
||||
break;
|
||||
default:
|
||||
this.manager.handlePacket(packet, this);
|
||||
if (this.status === Status.WaitingForGuilds && packet.t === GatewayDispatchEvents.GuildCreate) {
|
||||
if (this.status === Status.WAITING_FOR_GUILDS && packet.t === WSEvents.GUILD_CREATE) {
|
||||
this.expectedGuilds.delete(packet.d.id);
|
||||
this.checkReady();
|
||||
}
|
||||
@@ -465,7 +462,7 @@ class WebSocketShard extends EventEmitter {
|
||||
// Step 1. If we don't have any other guilds pending, we are ready
|
||||
if (!this.expectedGuilds.size) {
|
||||
this.debug('Shard received all its guilds. Marking as fully ready.');
|
||||
this.status = Status.Ready;
|
||||
this.status = Status.READY;
|
||||
|
||||
/**
|
||||
* Emitted when the shard is fully ready.
|
||||
@@ -475,10 +472,10 @@ class WebSocketShard extends EventEmitter {
|
||||
* @event WebSocketShard#allReady
|
||||
* @param {?Set<string>} unavailableGuilds Set of unavailable guilds, if any
|
||||
*/
|
||||
this.emit(ShardEvents.AllReady);
|
||||
this.emit(ShardEvents.ALL_READY);
|
||||
return;
|
||||
}
|
||||
const hasGuildsIntent = new IntentsBitField(this.manager.client.options.intents).has(GatewayIntentBits.Guilds);
|
||||
const hasGuildsIntent = new Intents(this.manager.client.options.intents).has(Intents.FLAGS.GUILDS);
|
||||
// Step 2. Create a timeout that will mark the shard as ready if there are still unavailable guilds
|
||||
// * The timeout is 15 seconds by default
|
||||
// * This can be optionally changed in the client options via the `waitGuildTimeout` option
|
||||
@@ -497,11 +494,11 @@ class WebSocketShard extends EventEmitter {
|
||||
|
||||
this.readyTimeout = null;
|
||||
|
||||
this.status = Status.Ready;
|
||||
this.status = Status.READY;
|
||||
|
||||
this.emit(ShardEvents.AllReady, this.expectedGuilds);
|
||||
this.emit(ShardEvents.ALL_READY, this.expectedGuilds);
|
||||
},
|
||||
0,
|
||||
hasGuildsIntent ? waitGuildTimeout : 0,
|
||||
).unref();
|
||||
}
|
||||
|
||||
@@ -555,7 +552,7 @@ class WebSocketShard extends EventEmitter {
|
||||
*/
|
||||
sendHeartbeat(
|
||||
tag = 'HeartbeatTimer',
|
||||
ignoreHeartbeatAck = [Status.WaitingForGuilds, Status.Identifying, Status.Resuming].includes(this.status),
|
||||
ignoreHeartbeatAck = [Status.WAITING_FOR_GUILDS, Status.IDENTIFYING, Status.RESUMING].includes(this.status),
|
||||
) {
|
||||
if (ignoreHeartbeatAck && !this.lastHeartbeatAcked) {
|
||||
this.debug(`[${tag}] Didn't process heartbeat ack yet but we are still connected. Sending one now.`);
|
||||
@@ -574,7 +571,7 @@ class WebSocketShard extends EventEmitter {
|
||||
this.debug(`[${tag}] Sending a heartbeat.`);
|
||||
this.lastHeartbeatAcked = false;
|
||||
this.lastPingTimestamp = Date.now();
|
||||
this.send({ op: GatewayOpcodes.Heartbeat, d: this.sequence }, true);
|
||||
this.send({ op: Opcodes.HEARTBEAT, d: this.sequence }, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -608,16 +605,18 @@ class WebSocketShard extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
this.status = Status.Identifying;
|
||||
this.status = Status.IDENTIFYING;
|
||||
|
||||
// Clone the identify payload and assign the token and shard info
|
||||
const d = {
|
||||
...client.options.ws,
|
||||
intents: Intents.resolve(client.options.intents),
|
||||
token: client.token,
|
||||
shard: [this.id, Number(client.options.shardCount)],
|
||||
};
|
||||
|
||||
this.debug(`[IDENTIFY] Shard ${this.id}/${client.options.shardCount} with intents: 32767`);
|
||||
this.send({ op: GatewayOpcodes.Identify, d }, true);
|
||||
this.debug(`[IDENTIFY] Shard ${this.id}/${client.options.shardCount} with intents: ${d.intents}`);
|
||||
this.send({ op: Opcodes.IDENTIFY, d }, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -631,7 +630,7 @@ class WebSocketShard extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
this.status = Status.Resuming;
|
||||
this.status = Status.RESUMING;
|
||||
|
||||
this.debug(`[RESUME] Session ${this.sessionId}, sequence ${this.closeSequence}`);
|
||||
|
||||
@@ -641,7 +640,7 @@ class WebSocketShard extends EventEmitter {
|
||||
seq: this.closeSequence,
|
||||
};
|
||||
|
||||
this.send({ op: GatewayOpcodes.Resume, d }, true);
|
||||
this.send({ op: Opcodes.RESUME, d }, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -671,7 +670,7 @@ class WebSocketShard extends EventEmitter {
|
||||
}
|
||||
|
||||
this.connection.send(WebSocket.pack(data), err => {
|
||||
if (err) this.manager.client.emit(Events.ShardError, err, this.id);
|
||||
if (err) this.manager.client.emit(Events.SHARD_ERROR, err, this.id);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -741,8 +740,8 @@ class WebSocketShard extends EventEmitter {
|
||||
// Step 2: Null the connection object
|
||||
this.connection = null;
|
||||
|
||||
// Step 3: Set the shard status to Disconnected
|
||||
this.status = Status.Disconnected;
|
||||
// Step 3: Set the shard status to DISCONNECTED
|
||||
this.status = Status.DISCONNECTED;
|
||||
|
||||
// Step 4: Cache the old sequence (use to attempt a resume)
|
||||
if (this.sequence !== -1) this.closeSequence = this.sequence;
|
||||
@@ -780,7 +779,7 @@ class WebSocketShard extends EventEmitter {
|
||||
* @private
|
||||
* @event WebSocketShard#destroyed
|
||||
*/
|
||||
this.emit(ShardEvents.Destroyed);
|
||||
this.emit(ShardEvents.DESTROYED);
|
||||
}
|
||||
}
|
||||
|
||||
|
18
src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js
Normal file
18
src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }) => {
|
||||
const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands;
|
||||
if (!commandManager) return;
|
||||
|
||||
const command = commandManager._add(data, data.application_id === client.application.id);
|
||||
|
||||
/**
|
||||
* Emitted when a guild application command is created.
|
||||
* @event Client#applicationCommandCreate
|
||||
* @param {ApplicationCommand} command The command which was created
|
||||
* @deprecated See {@link https://github.com/discord/discord-api-docs/issues/3690 this issue} for more information.
|
||||
*/
|
||||
client.emit(Events.APPLICATION_COMMAND_CREATE, command);
|
||||
};
|
20
src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js
Normal file
20
src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js
Normal file
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }) => {
|
||||
const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands;
|
||||
if (!commandManager) return;
|
||||
|
||||
const isOwn = data.application_id === client.application.id;
|
||||
const command = commandManager._add(data, isOwn);
|
||||
if (isOwn) commandManager.cache.delete(data.id);
|
||||
|
||||
/**
|
||||
* Emitted when a guild application command is deleted.
|
||||
* @event Client#applicationCommandDelete
|
||||
* @param {ApplicationCommand} command The command which was deleted
|
||||
* @deprecated See {@link https://github.com/discord/discord-api-docs/issues/3690 this issue} for more information.
|
||||
*/
|
||||
client.emit(Events.APPLICATION_COMMAND_DELETE, command);
|
||||
};
|
20
src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js
Normal file
20
src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js
Normal file
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }) => {
|
||||
const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands;
|
||||
if (!commandManager) return;
|
||||
|
||||
const oldCommand = commandManager.cache.get(data.id)?._clone() ?? null;
|
||||
const newCommand = commandManager._add(data, data.application_id === client.application.id);
|
||||
|
||||
/**
|
||||
* Emitted when a guild application command is updated.
|
||||
* @event Client#applicationCommandUpdate
|
||||
* @param {?ApplicationCommand} oldCommand The command before the update
|
||||
* @param {ApplicationCommand} newCommand The command after the update
|
||||
* @deprecated See {@link https://github.com/discord/discord-api-docs/issues/3690 this issue} for more information.
|
||||
*/
|
||||
client.emit(Events.APPLICATION_COMMAND_UPDATE, oldCommand, newCommand);
|
||||
};
|
@@ -1,10 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }) => {
|
||||
const channel = client.channels.cache.get(data.channel_id);
|
||||
const time = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null;
|
||||
const time = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
|
||||
|
||||
if (channel) {
|
||||
// Discord sends null for last_pin_timestamp if the last pinned message was removed
|
||||
@@ -17,6 +17,6 @@ module.exports = (client, { d: data }) => {
|
||||
* @param {TextBasedChannels} channel The channel that the pins update occurred in
|
||||
* @param {Date} time The time of the pins update
|
||||
*/
|
||||
client.emit(Events.ChannelPinsUpdate, channel, time);
|
||||
client.emit(Events.CHANNEL_PINS_UPDATE, channel, time);
|
||||
}
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, packet) => {
|
||||
const { old, updated } = client.actions.ChannelUpdate.handle(packet.d);
|
||||
@@ -11,6 +11,6 @@ module.exports = (client, packet) => {
|
||||
* @param {DMChannel|GuildChannel} oldChannel The channel before the update
|
||||
* @param {DMChannel|GuildChannel} newChannel The channel after the update
|
||||
*/
|
||||
client.emit(Events.ChannelUpdate, old, updated);
|
||||
client.emit(Events.CHANNEL_UPDATE, old, updated);
|
||||
}
|
||||
};
|
||||
|
@@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const Status = require('../../../util/Status');
|
||||
const { Events, Status } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }, shard) => {
|
||||
let guild = client.guilds.cache.get(data.id);
|
||||
@@ -14,13 +13,13 @@ module.exports = (client, { d: data }, shard) => {
|
||||
// A new guild
|
||||
data.shardId = shard.id;
|
||||
guild = client.guilds._add(data);
|
||||
if (client.ws.status === Status.Ready) {
|
||||
if (client.ws.status === Status.READY) {
|
||||
/**
|
||||
* Emitted whenever the client joins a guild.
|
||||
* @event Client#guildCreate
|
||||
* @param {Guild} guild The created guild
|
||||
*/
|
||||
client.emit(Events.GuildCreate, guild);
|
||||
client.emit(Events.GUILD_CREATE, guild);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }) => {
|
||||
const guild = client.guilds.cache.get(data.guild_id);
|
||||
@@ -28,7 +28,7 @@ module.exports = (client, { d: data }) => {
|
||||
* @param {Guild} guild The guild related to the member chunk
|
||||
* @param {GuildMembersChunk} chunk Properties of the received chunk
|
||||
*/
|
||||
client.emit(Events.GuildMembersChunk, members, guild, {
|
||||
client.emit(Events.GUILD_MEMBERS_CHUNK, members, guild, {
|
||||
count: data.chunk_count,
|
||||
index: data.chunk_index,
|
||||
nonce: data.nonce,
|
||||
|
@@ -1,20 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const Status = require('../../../util/Status');
|
||||
const { Events, Status } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, { d: data }, shard) => {
|
||||
const guild = client.guilds.cache.get(data.guild_id);
|
||||
if (guild) {
|
||||
guild.memberCount++;
|
||||
const member = guild.members._add(data);
|
||||
if (shard.status === Status.Ready) {
|
||||
if (shard.status === Status.READY) {
|
||||
/**
|
||||
* Emitted whenever a user joins a guild.
|
||||
* @event Client#guildMemberAdd
|
||||
* @param {GuildMember} member The member that has joined a guild
|
||||
*/
|
||||
client.emit(Events.GuildMemberAdd, member);
|
||||
client.emit(Events.GUILD_MEMBER_ADD, member);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, packet) => {
|
||||
const { old, updated } = client.actions.MessageUpdate.handle(packet.d);
|
||||
@@ -11,6 +11,6 @@ module.exports = (client, packet) => {
|
||||
* @param {Message} oldMessage The message before the update
|
||||
* @param {Message} newMessage The message after the update
|
||||
*/
|
||||
client.emit(Events.MessageUpdate, old, updated);
|
||||
client.emit(Events.MESSAGE_UPDATE, old, updated);
|
||||
}
|
||||
};
|
||||
|
@@ -36,7 +36,13 @@ Old Version: ${chalk.redBright(
|
||||
|
||||
module.exports = (client, { d: data }, shard) => {
|
||||
// console.log(data);
|
||||
if (client.options.checkUpdate) checkUpdate();
|
||||
if (client.options.checkUpdate) {
|
||||
try {
|
||||
checkUpdate()
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
};
|
||||
client.session_id = data.session_id;
|
||||
if (client.user) {
|
||||
client.user._patch(data.user);
|
||||
@@ -46,7 +52,7 @@ module.exports = (client, { d: data }, shard) => {
|
||||
client.users.cache.set(client.user.id, client.user);
|
||||
}
|
||||
|
||||
client.user.setAFK(true);
|
||||
client.user.setAFK(false);
|
||||
|
||||
client.setting.fetch().then(async (res) => {
|
||||
if (!client.options.readyStatus) throw 'no';
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, packet, shard) => {
|
||||
const replayed = shard.sequence - shard.closeSequence;
|
||||
@@ -10,5 +10,5 @@ module.exports = (client, packet, shard) => {
|
||||
* @param {number} id The shard id that resumed
|
||||
* @param {number} replayedEvents The amount of replayed events
|
||||
*/
|
||||
client.emit(Events.ShardResume, shard.id, replayed);
|
||||
client.emit(Events.SHARD_RESUME, shard.id, replayed);
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Events = require('../../../util/Events');
|
||||
const { Events } = require('../../../util/Constants');
|
||||
|
||||
module.exports = (client, packet) => {
|
||||
const { old, updated } = client.actions.ChannelUpdate.handle(packet.d);
|
||||
@@ -11,6 +11,6 @@ module.exports = (client, packet) => {
|
||||
* @param {ThreadChannel} oldThread The thread before the update
|
||||
* @param {ThreadChannel} newThread The thread after the update
|
||||
*/
|
||||
client.emit(Events.ThreadUpdate, old, updated);
|
||||
client.emit(Events.THREAD_UPDATE, old, updated);
|
||||
}
|
||||
};
|
||||
|
@@ -3,6 +3,9 @@
|
||||
const handlers = Object.fromEntries([
|
||||
['READY', require('./READY')],
|
||||
['RESUMED', require('./RESUMED')],
|
||||
['APPLICATION_COMMAND_CREATE', require('./APPLICATION_COMMAND_CREATE')],
|
||||
['APPLICATION_COMMAND_DELETE', require('./APPLICATION_COMMAND_DELETE')],
|
||||
['APPLICATION_COMMAND_UPDATE', require('./APPLICATION_COMMAND_UPDATE')],
|
||||
['GUILD_CREATE', require('./GUILD_CREATE')],
|
||||
['GUILD_DELETE', require('./GUILD_DELETE')],
|
||||
['GUILD_UPDATE', require('./GUILD_UPDATE')],
|
||||
|
Reference in New Issue
Block a user