mirror of
https://github.com/discordjs/discord.js.git
synced 2024-07-08 01:41:18 +12:00
fix: Consistent debug log spacing (#10349)
* fix: consistent debug log spacing * refactor: simplify formatting * refactor: more readable ternary Co-Authored-By: Synbulat Biishev <contact@syjalo.dev> * fix: modify parameters and types --------- Co-authored-by: Synbulat Biishev <contact@syjalo.dev>
This commit is contained in:
parent
c5d40d3807
commit
38c699bc8a
|
@ -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')}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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' });
|
||||
}
|
||||
|
|
|
@ -85,11 +85,11 @@ class WebSocketShard extends EventEmitter {
|
|||
|
||||
/**
|
||||
* Emits a debug event.
|
||||
* @param {string} message The debug message
|
||||
* @param {string[]} messages The debug message
|
||||
* @private
|
||||
*/
|
||||
debug(message) {
|
||||
this.manager.debug(message, this.id);
|
||||
debug(messages) {
|
||||
this.manager.debug(messages, this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -191,12 +194,12 @@ 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([
|
||||
hasGuildsIntent
|
||||
? `Shard did not receive any guild packets in ${waitGuildTimeout} ms.`
|
||||
: 'Shard will not receive anymore guild packets.',
|
||||
`Unavailable guild count: ${this.expectedGuilds.size}`,
|
||||
]);
|
||||
|
||||
this.readyTimeout = null;
|
||||
this.status = Status.Ready;
|
||||
|
|
4
packages/discord.js/typings/index.d.ts
vendored
4
packages/discord.js/typings/index.d.ts
vendored
|
@ -3653,7 +3653,7 @@ export class WebSocketManager extends EventEmitter {
|
|||
public on(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
|
||||
public once(event: GatewayDispatchEvents, listener: (data: any, shardId: number) => void): this;
|
||||
|
||||
private debug(message: string, shardId?: number): void;
|
||||
private debug(messages: readonly string[], shardId?: number): void;
|
||||
private connect(): Promise<void>;
|
||||
private broadcast(packet: unknown): void;
|
||||
private destroy(): Promise<void>;
|
||||
|
@ -3684,7 +3684,7 @@ export class WebSocketShard extends EventEmitter {
|
|||
public status: Status;
|
||||
public ping: number;
|
||||
|
||||
private debug(message: string): void;
|
||||
private debug(messages: readonly string[]): void;
|
||||
private onReadyPacket(packet: unknown): void;
|
||||
private gotGuild(guildId: Snowflake): void;
|
||||
private checkReady(): void;
|
||||
|
|
|
@ -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') });
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue