fix: only edit when deferred (#276)

* fix: only edit when deferred

* fix: internal type
This commit is contained in:
MARCROCK22 2024-10-12 14:28:32 -04:00 committed by GitHub
parent 4dd69aeb14
commit 4a0a9ae130
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 8 deletions

View File

@ -19,7 +19,12 @@ import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from
export interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
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<When<InferWithPrefix, WebhookMessageStructure | MessageStructure, WebhookMessageStructure>> {
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;
}

View File

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