From 93e2d87de702513b3749f0db200c15ff97a39bc5 Mon Sep 17 00:00:00 2001 From: Maisy Date: Sun, 24 Apr 2022 21:16:51 -0500 Subject: [PATCH] Add fix for dev guild commands with no bot scope and allow for setState without first deferring --- package.json | 2 +- src/Page.ts | 2 ++ src/SlashasaurusClient.ts | 47 ++++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 48c7e63..c7c501c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slashasaurus", - "version": "0.5.0", + "version": "0.5.1", "main": "dist/index.js", "types": "dist/index.d.ts", "license": "MIT", diff --git a/src/Page.ts b/src/Page.ts index 6041758..de3eba5 100644 --- a/src/Page.ts +++ b/src/Page.ts @@ -70,6 +70,7 @@ export abstract class Page

{ nextId: number; message: Message | PageInteractionReplyMessage | null; static pageId: string = DEFAULT_PAGE_ID; + latestInteraction: MessageComponentInteraction | null = null; constructor(props: P) { // @ts-expect-error this will say that _client doesn't exist on the constructor type, but it does and we're abusing that :^) @@ -122,6 +123,7 @@ export abstract class Page

{ async transitionTo(newPage: Page) { newPage.message = this.message; + newPage.latestInteraction = this.latestInteraction; await this.client.updatePage(newPage, newPage.state); return; } diff --git a/src/SlashasaurusClient.ts b/src/SlashasaurusClient.ts index f1e21bc..f770dc3 100644 --- a/src/SlashasaurusClient.ts +++ b/src/SlashasaurusClient.ts @@ -1,6 +1,5 @@ import { ApplicationCommandData, - ApplicationCommandManager, ApplicationCommandOptionData, AutocompleteInteraction, Awaitable, @@ -13,7 +12,6 @@ import { CommandInteraction, ContextMenuInteraction, DMChannel, - GuildApplicationCommandManager, Interaction, InteractionWebhook, Message, @@ -239,12 +237,10 @@ export class SlashasaurusClient extends Client { this.logger?.debug(commandData); - let manager: ApplicationCommandManager | GuildApplicationCommandManager; + let manager = this.application!.commands; if (registerTo === 'dev') { - manager = this.guilds.cache.get(this.devServerId)!.commands; - manager.set(commandData); + manager.set(commandData, this.devServerId); } else if (registerTo === 'global') { - manager = this.application!.commands; manager.set(commandData); } @@ -868,6 +864,7 @@ export class SlashasaurusClient extends Client { messageToMessageData(page.message) ); } + page.latestInteraction = interaction; page.handleId(interaction.customId.split(';')[1], interaction); } @@ -912,6 +909,7 @@ export class SlashasaurusClient extends Client { messageToMessageData(page.message) ); } + page.latestInteraction = interaction; page.handleId(interaction.customId.split(';')[1], interaction); } @@ -952,14 +950,10 @@ export class SlashasaurusClient extends Client { : undefined, fetchReply: true, }); - if (message instanceof Message) { - page.message = message; - } else { - page.message = new PageInteractionReplyMessage( - interaction.webhook, - message.id - ); - } + page.message = new PageInteractionReplyMessage( + interaction.webhook, + message.id + ); const state = await page.serializeState(); this.storePageState( message.id, @@ -998,12 +992,25 @@ export class SlashasaurusClient extends Client { page.state = newState; const messageOptions = await page.render(); const { message } = page; - await message.edit({ - ...messageOptions, - components: messageOptions.components - ? pageComponentRowsToComponents(messageOptions.components, page) - : undefined, - }); + if ( + message instanceof PageInteractionReplyMessage && + page.latestInteraction && + !page.latestInteraction.deferred + ) { + await page.latestInteraction.update({ + ...messageOptions, + components: messageOptions.components + ? pageComponentRowsToComponents(messageOptions.components, page) + : undefined, + }); + } else { + await message.edit({ + ...messageOptions, + components: messageOptions.components + ? pageComponentRowsToComponents(messageOptions.components, page) + : undefined, + }); + } const state = await page.serializeState(); this.activePages.set(message.id, page); this.storePageState(