From 4a0a9ae1304ef159ffa2358da2eddd41779301c8 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:28:32 -0400 Subject: [PATCH] fix: only edit when deferred (#276) * fix: only edit when deferred * fix: internal type --- src/commands/applications/chatcontext.ts | 14 ++++++++++++-- src/websocket/discord/workermanager.ts | 9 +++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/commands/applications/chatcontext.ts b/src/commands/applications/chatcontext.ts index d8aba24..04c358e 100644 --- a/src/commands/applications/chatcontext.ts +++ b/src/commands/applications/chatcontext.ts @@ -19,7 +19,12 @@ import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from export interface CommandContext extends BaseContext, - ExtendContext {} + ExtendContext { + /**@internal */ + __edited?: true; + /**@internal */ + __deferred?: true; +} export class CommandContext< T extends OptionsRecord = {}, @@ -89,6 +94,7 @@ export class CommandContext< > { if (this.interaction) return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse); + this.__deferred = true; const options = (this.client as Client | WorkerClient).options?.commands; return (this.messageResponse = await (this.message! as Message)[options?.reply?.(this) ? 'reply' : 'write']( options?.deferReplyResponse?.(this) ?? { content: 'Thinking...' }, @@ -99,7 +105,11 @@ export class CommandContext< body: InteractionMessageUpdateBodyRequest, ): Promise> { if (this.interaction) return this.interaction.editResponse(body); - body.content ??= ''; + if (this.__deferred && !this.__edited) { + this.__edited = true; + if (this.messageResponse?.content) body.content ??= ''; + if (this.messageResponse?.embeds.length) body.embeds ??= []; + } return (this.messageResponse = await this.messageResponse!.edit(body)) as never; } diff --git a/src/websocket/discord/workermanager.ts b/src/websocket/discord/workermanager.ts index 80d8cd4..2e46845 100644 --- a/src/websocket/discord/workermanager.ts +++ b/src/websocket/discord/workermanager.ts @@ -101,10 +101,6 @@ export class WorkerManager extends Map< return this.options.shardsPerWorker; } - get workers() { - return this.options.workers; - } - async syncLatency({ shardId, workerId, @@ -130,7 +126,7 @@ export class WorkerManager extends Map< calculateWorkerId(shardId: number) { const workerId = Math.floor((shardId - this.shardStart) / this.shardsPerWorker); - if (workerId >= this.workers) { + if (workerId >= this.totalWorkers) { throw new Error('Invalid shardId'); } return workerId; @@ -268,6 +264,7 @@ export class WorkerManager extends Map< if ([...this.values()].every(w => w.disconnected)) { this.options.totalShards = this._info!.shards; this.options.shardEnd = this.options.totalShards = this._info!.shards; + this.options.workers = this.size; delete this._info; for (const [id] of this.entries()) { this.postMessage(id, { @@ -529,7 +526,7 @@ export class WorkerManager extends Map< name: '[WorkerManager]', }); } - if (this.totalShards / this.shardsPerWorker > this.workers) { + if (this.totalShards / this.shardsPerWorker > this.totalWorkers) { throw new Error( `Cannot create enough shards in the specified workers, minimum: ${Math.ceil( this.totalShards / this.shardsPerWorker,