From 010a48c7f028fa808dad423f81e7aee2f4afe246 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Mon, 29 Mar 2021 09:39:37 +0530 Subject: [PATCH] debug logs --- src/gateway/handlers/index.ts | 1 + src/gateway/index.ts | 23 +++++++++++------------ src/structures/slash.ts | 21 +++++++++++---------- src/test/debug.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 src/test/debug.ts diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 9775575..ba5dfa9 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -414,4 +414,5 @@ export type ClientEvents = { commandMissingArgs: [ctx: CommandContext] commandUsed: [ctx: CommandContext] commandError: [ctx: CommandContext, err: Error] + gatewayError: [err: ErrorEvent, shards: [number, number]] } diff --git a/src/gateway/index.ts b/src/gateway/index.ts index d890457..1d672af 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -174,8 +174,8 @@ export class Gateway extends HarmonyEventEmitter { } case GatewayOpcodes.RECONNECT: { this.emit('reconnectRequired') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() + this.debug('Received OpCode RECONNECT') + await this.reconnect() break } default: @@ -191,8 +191,7 @@ export class Gateway extends HarmonyEventEmitter { switch (code) { case GatewayCloseCodes.UNKNOWN_ERROR: this.debug('API has encountered Unknown Error. Reconnecting...') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() + await this.reconnect() break case GatewayCloseCodes.UNKNOWN_OPCODE: throw new Error( @@ -206,20 +205,17 @@ export class Gateway extends HarmonyEventEmitter { throw new Error('Invalid Token provided!') case GatewayCloseCodes.INVALID_SEQ: this.debug('Invalid Seq was sent. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() + await this.reconnect() break case GatewayCloseCodes.RATE_LIMITED: throw new Error("You're ratelimited. Calm down.") case GatewayCloseCodes.SESSION_TIMED_OUT: this.debug('Session Timeout. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect(true) + await this.reconnect(true) break case GatewayCloseCodes.INVALID_SHARD: this.debug('Invalid Shard was sent. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() + await this.reconnect() break case GatewayCloseCodes.SHARDING_REQUIRED: throw new Error("Couldn't connect. Sharding is required!") @@ -257,6 +253,7 @@ export class Gateway extends HarmonyEventEmitter { error.name = 'ErrorEvent' console.log(error) this.emit('error', error, event) + this.client.emit('gatewayError', event, this.shards) } private enqueueIdentify(forceNew?: boolean): void { @@ -388,8 +385,8 @@ export class Gateway extends HarmonyEventEmitter { channel === undefined ? null : typeof channel === 'string' - ? channel - : channel?.id, + ? channel + : channel?.id, self_mute: voiceOptions.mute === undefined ? false : voiceOptions.mute, self_deaf: voiceOptions.deaf === undefined ? false : voiceOptions.deaf } @@ -402,6 +399,7 @@ export class Gateway extends HarmonyEventEmitter { async reconnect(forceNew?: boolean): Promise { this.emit('reconnecting') + this.debug('Reconnecting... (force new: ' + String(forceNew) + ')') clearInterval(this.heartbeatIntervalID) if (forceNew === true) { @@ -429,6 +427,7 @@ export class Gateway extends HarmonyEventEmitter { } close(code: number = 1000, reason?: string): void { + this.debug(`Closing with code ${code}${reason !== undefined && reason !== '' ? ` and reason ${reason}` : ''}`) return this.websocket?.close(code, reason) } diff --git a/src/structures/slash.ts b/src/structures/slash.ts index 86c48dc..d7681e5 100644 --- a/src/structures/slash.ts +++ b/src/structures/slash.ts @@ -175,15 +175,15 @@ export class Interaction extends SnowflakeBase { type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, data: data.type === undefined || - data.type === InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE || - data.type === InteractionResponseType.CHANNEL_MESSAGE + data.type === InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE || + data.type === InteractionResponseType.CHANNEL_MESSAGE ? { - content: data.content ?? '', - embeds: data.embeds, - tts: data.tts ?? false, - flags, - allowed_mentions: data.allowedMentions ?? undefined - } + content: data.content ?? '', + embeds: data.embeds, + tts: data.tts ?? false, + flags, + allowed_mentions: data.allowedMentions ?? undefined + } : undefined } @@ -313,9 +313,10 @@ export class Interaction extends SnowflakeBase { (option as WebhookMessageOptions)?.embed !== undefined ? [(option as WebhookMessageOptions).embed] : (option as WebhookMessageOptions)?.embeds !== undefined - ? (option as WebhookMessageOptions).embeds - : undefined, + ? (option as WebhookMessageOptions).embeds + : undefined, file: (option as WebhookMessageOptions)?.file, + files: (option as WebhookMessageOptions)?.files, tts: (option as WebhookMessageOptions)?.tts, allowed_mentions: (option as WebhookMessageOptions)?.allowedMentions } diff --git a/src/test/debug.ts b/src/test/debug.ts new file mode 100644 index 0000000..48bc55e --- /dev/null +++ b/src/test/debug.ts @@ -0,0 +1,28 @@ +import { Client, event } from '../../mod.ts' +import { TOKEN } from './config.ts' + +class MyClient extends Client { + constructor() { + super({ + token: TOKEN, + intents: [], + }) + } + + @event() + ready(): void { + console.log('Connected!') + } + + debug(title: string, msg: string): void { + console.log(`[${title}] ${msg}`) + if (title === 'Gateway' && msg === 'Initializing WebSocket...') { + try { throw new Error("Stack") } catch (e) { + console.log(e.stack) + } + } + } +} + +const client = new MyClient() +client.connect() \ No newline at end of file