debug logs

This commit is contained in:
DjDeveloperr 2021-03-29 09:39:37 +05:30
parent 3f7372d6a7
commit 010a48c7f0
4 changed files with 51 additions and 22 deletions

View File

@ -414,4 +414,5 @@ export type ClientEvents = {
commandMissingArgs: [ctx: CommandContext]
commandUsed: [ctx: CommandContext]
commandError: [ctx: CommandContext, err: Error]
gatewayError: [err: ErrorEvent, shards: [number, number]]
}

View File

@ -174,8 +174,8 @@ export class Gateway extends HarmonyEventEmitter<GatewayTypedEvents> {
}
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<GatewayTypedEvents> {
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<GatewayTypedEvents> {
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<GatewayTypedEvents> {
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<GatewayTypedEvents> {
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<GatewayTypedEvents> {
async reconnect(forceNew?: boolean): Promise<void> {
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<GatewayTypedEvents> {
}
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)
}

View File

@ -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
}

28
src/test/debug.ts Normal file
View File

@ -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()