Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions packages/discord.js/src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ class WebSocketManager extends EventEmitter {

/**
* Emits a debug message.
* @param {string} message The debug message
* @param {string[]} messages The debug message
* @param {?number} [shardId] The id of the shard that emitted this message, if any
* @private
*/
debug(message, shardId) {
debug(messages, shardId) {
this.client.emit(
Events.Debug,
`[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${message}`,
`[WS => ${typeof shardId === 'number' ? `Shard ${shardId}` : 'Manager'}] ${messages.join('\n\t')}`,
);
}

Expand Down Expand Up @@ -170,15 +170,8 @@ class WebSocketManager extends EventEmitter {
});

const { total, remaining } = sessionStartLimit;

this.debug(`Fetched Gateway Information
URL: ${gatewayURL}
Recommended Shards: ${recommendedShards}`);

this.debug(`Session Limit Information
Total: ${total}
Remaining: ${remaining}`);

this.debug(['Fetched Gateway Information', `URL: ${gatewayURL}`, `Recommended Shards: ${recommendedShards}`]);
this.debug(['Session Limit Information', `Total: ${total}`, `Remaining: ${remaining}`]);
this.gateway = `${gatewayURL}/`;

this.client.options.shardCount = await this._ws.getShardCount();
Expand Down Expand Up @@ -231,7 +224,7 @@ class WebSocketManager extends EventEmitter {
* @private
*/
attachEvents() {
this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug(message, shardId));
this._ws.on(WSWebSocketShardEvents.Debug, ({ message, shardId }) => this.debug([message], shardId));
this._ws.on(WSWebSocketShardEvents.Dispatch, ({ data, shardId }) => {
this.client.emit(Events.Raw, data, shardId);
this.emit(data.t, data.d, shardId);
Expand All @@ -258,7 +251,7 @@ class WebSocketManager extends EventEmitter {
* @param {number} id The shard id that disconnected
*/
this.client.emit(Events.ShardDisconnect, { code, reason: reasonIsDeprecated, wasClean: true }, shardId);
this.debug(`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`, shardId);
this.debug([`Shard not resumable: ${code} (${GatewayCloseCodes[code] ?? CloseCodes[code]})`], shardId);
return;
}

Expand Down Expand Up @@ -291,7 +284,7 @@ class WebSocketManager extends EventEmitter {
});

this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => {
this.debug(`Heartbeat acknowledged, latency of ${latency}ms.`, shardId);
this.debug([`Heartbeat acknowledged, latency of ${latency}ms.`], shardId);
const shard = this.shards.get(shardId);
shard.lastPingTimestamp = heartbeatAt;
shard.ping = latency;
Expand Down Expand Up @@ -324,7 +317,7 @@ class WebSocketManager extends EventEmitter {
async destroy() {
if (this.destroyed) return;
// TODO: Make a util for getting a stack
this.debug(Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack);
this.debug([Object.assign(new Error(), { name: 'Manager was destroyed:' }).stack]);
this.destroyed = true;
await this._ws?.destroy({ code: CloseCodes.Normal, reason: 'Manager was destroyed' });
}
Expand Down
26 changes: 14 additions & 12 deletions packages/discord.js/src/client/websocket/WebSocketShard.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ class WebSocketShard extends EventEmitter {
wasClean: false,
},
) {
this.debug(`[CLOSE]
Event Code: ${event.code}
Clean : ${event.wasClean}
Reason : ${event.reason ?? 'No reason received'}`);
this.debug([
'[CLOSE]',
`Event Code: ${event.code}`,
`Clean : ${event.wasClean}`,
`Reason : ${event.reason ?? 'No reason received'}`,
]);

/**
* Emitted when a shard's WebSocket closes.
* @private
Expand All @@ -130,7 +133,7 @@ class WebSocketShard extends EventEmitter {
*/
onReadyPacket(packet) {
if (!packet) {
this.debug(`Received broken packet: '${packet}'.`);
this.debug([`Received broken packet: '${packet}'.`]);
return;
}

Expand Down Expand Up @@ -167,7 +170,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.debug(['Shard received all its guilds. Marking as fully ready.']);
this.status = Status.Ready;

/**
Expand All @@ -191,12 +194,11 @@ class WebSocketShard extends EventEmitter {

this.readyTimeout = setTimeout(
() => {
this.debug(
`Shard ${hasGuildsIntent ? 'did' : 'will'} not receive any more guild packets` +
`${hasGuildsIntent ? ` in ${waitGuildTimeout} ms` : ''}.\nUnavailable guild count: ${
this.expectedGuilds.size
}`,
);
this.debug([
// eslint-disable-next-line max-len
`Shard ${hasGuildsIntent ? 'did' : 'will'} not receive anymore guild packets${hasGuildsIntent ? ` in ${waitGuildTimeout} ms` : ''}.`,
`Unavailable guild count: ${this.expectedGuilds.size}`,
]);

this.readyTimeout = null;
this.status = Status.Ready;
Expand Down
11 changes: 1 addition & 10 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,15 +915,6 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
}

private debug(messages: [string, ...string[]]) {
const message = `${messages[0]}${
messages.length > 1
? `\n${messages
.slice(1)
.map((message) => ` ${message}`)
.join('\n')}`
: ''
}`;

this.emit(WebSocketShardEvents.Debug, { message });
this.emit(WebSocketShardEvents.Debug, { message: messages.join('\n\t') });
}
}