diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index 4ffb8c7..43499cd 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -116,11 +116,11 @@ export interface VoiceServerUpdateData { // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type ClientEvents = { /** When Client has successfully connected to Discord */ - ready: [] + ready: [shard: number] /** When a successful reconnect has been made */ - reconnect: [] + reconnect: [shard: number] /** When a successful session resume has been done */ - resumed: [] + resumed: [shard: number] /** * When a new Channel is created * @param channel New Channel object diff --git a/src/gateway/handlers/ready.ts b/src/gateway/handlers/ready.ts index 6f5085b..9e2b727 100644 --- a/src/gateway/handlers/ready.ts +++ b/src/gateway/handlers/ready.ts @@ -20,5 +20,5 @@ export const ready: GatewayEventHandler = async ( gateway.client.guilds.set(guild.id, guild) }) - gateway.client.emit('ready') + gateway.client.emit('ready', gateway.shards?.[0] ?? 0) } diff --git a/src/gateway/handlers/resume.ts b/src/gateway/handlers/resume.ts index 50d7c6b..1c8717e 100644 --- a/src/gateway/handlers/resume.ts +++ b/src/gateway/handlers/resume.ts @@ -8,11 +8,11 @@ export const resume: GatewayEventHandler = async ( d: Resume ) => { gateway.debug(`Session Resumed!`) - gateway.client.emit('resumed') + gateway.client.emit('resumed', gateway.shards?.[0] ?? 0) if (gateway.client.user === undefined) gateway.client.user = new User( gateway.client, await gateway.client.rest.get(CLIENT_USER()) ) - gateway.client.emit('ready') + gateway.client.emit('ready', gateway.shards?.[0] ?? 0) } diff --git a/src/models/client.ts b/src/models/client.ts index 926cc2c..d39996a 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -179,7 +179,7 @@ export class Client extends HarmonyEventEmitter { Object.keys(this._decoratedEvents).length !== 0 ) { Object.entries(this._decoratedEvents).forEach((entry) => { - this.on(entry[0] as keyof ClientEvents, entry[1]) + this.on(entry[0] as keyof ClientEvents, entry[1].bind(this)) }) this._decoratedEvents = undefined } @@ -196,12 +196,6 @@ export class Client extends HarmonyEventEmitter { if (options.shard !== undefined) this.shard = options.shard if (options.shardCount !== undefined) this.shardCount = options.shardCount - this.slash = new SlashClient({ - id: () => this.getEstimatedID(), - client: this, - enabled: options.enableSlash - }) - this.fetchGatewayInfo = options.fetchGatewayInfo ?? true if (this.token === undefined) { @@ -224,6 +218,12 @@ export class Client extends HarmonyEventEmitter { if (options.restOptions !== undefined) Object.assign(restOptions, options.restOptions) this.rest = new RESTManager(restOptions) + + this.slash = new SlashClient({ + id: () => this.getEstimatedID(), + client: this, + enabled: options.enableSlash + }) } /** @@ -425,7 +425,7 @@ export function event(name?: keyof ClientEvents) { ) { const listener = ((client as unknown) as { [name in keyof ClientEvents]: (...args: ClientEvents[name]) => any - })[name ?? ((prop as unknown) as keyof ClientEvents)] + })[(prop as unknown) as keyof ClientEvents] if (typeof listener !== 'function') throw new Error('@event decorator requires a function') if (client._decoratedEvents === undefined) client._decoratedEvents = {} diff --git a/src/models/extensions.ts b/src/models/extensions.ts index 4496590..8ad0b7a 100644 --- a/src/models/extensions.ts +++ b/src/models/extensions.ts @@ -91,7 +91,7 @@ export class Extension { Object.keys(this._decoratedEvents).length !== 0 ) { Object.entries(this._decoratedEvents).forEach((entry) => { - this.listen(entry[0] as keyof ClientEvents, entry[1]) + this.listen(entry[0] as keyof ClientEvents, entry[1].bind(this)) }) this._decoratedEvents = undefined } diff --git a/src/models/slashClient.ts b/src/models/slashClient.ts index 9520025..1a6daee 100644 --- a/src/models/slashClient.ts +++ b/src/models/slashClient.ts @@ -201,13 +201,11 @@ export class SlashBuilder { export class SlashCommandsManager { slash: SlashClient - - get rest(): RESTManager { - return this.slash.rest - } + rest: RESTManager constructor(client: SlashClient) { this.slash = client + this.rest = client.rest } /** Get all Global Slash Commands */ @@ -378,7 +376,6 @@ export class SlashClient { this.id = id this.client = options.client this.token = options.token - this.commands = new SlashCommandsManager(this) this.publicKey = options.publicKey if (options !== undefined) { @@ -387,12 +384,14 @@ export class SlashClient { if (this.client?._decoratedSlash !== undefined) { this.client._decoratedSlash.forEach((e) => { + e.handler = e.handler.bind(this.client) this.handlers.push(e) }) } if (this._decoratedSlash !== undefined) { this._decoratedSlash.forEach((e) => { + e.handler = e.handler.bind(this.client) this.handlers.push(e) }) } @@ -409,6 +408,8 @@ export class SlashClient { this.client?.on('interactionCreate', (interaction) => this._process(interaction) ) + + this.commands = new SlashCommandsManager(this) } getID(): string { diff --git a/src/test/slash.ts b/src/test/slash.ts index d673a10..155a745 100644 --- a/src/test/slash.ts +++ b/src/test/slash.ts @@ -7,6 +7,7 @@ export class MyClient extends Client { @event() ready(): void { console.log(`Logged in as ${this.user?.tag}!`) + this.slash.commands.bulkEdit([{ name: 'send', description: 'idk' }]) } @event('debug')