Skip to content

Commit

Permalink
Add fix for dev guild commands with no bot scope and allow for setSta…
Browse files Browse the repository at this point in the history
…te without first deferring
  • Loading branch information
Rodentman87 committed Apr 25, 2022
1 parent 1b8b0b6 commit 93e2d87
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export abstract class Page<P = {}, S = {}> {
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 :^)
Expand Down Expand Up @@ -122,6 +123,7 @@ export abstract class Page<P = {}, S = {}> {

async transitionTo(newPage: Page) {
newPage.message = this.message;
newPage.latestInteraction = this.latestInteraction;
await this.client.updatePage(newPage, newPage.state);
return;
}
Expand Down
47 changes: 27 additions & 20 deletions src/SlashasaurusClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ApplicationCommandData,
ApplicationCommandManager,
ApplicationCommandOptionData,
AutocompleteInteraction,
Awaitable,
Expand All @@ -13,7 +12,6 @@ import {
CommandInteraction,
ContextMenuInteraction,
DMChannel,
GuildApplicationCommandManager,
Interaction,
InteractionWebhook,
Message,
Expand Down Expand Up @@ -239,12 +237,10 @@ export class SlashasaurusClient extends Client<true> {

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);
}

Expand Down Expand Up @@ -868,6 +864,7 @@ export class SlashasaurusClient extends Client<true> {
messageToMessageData(page.message)
);
}
page.latestInteraction = interaction;
page.handleId(interaction.customId.split(';')[1], interaction);
}

Expand Down Expand Up @@ -912,6 +909,7 @@ export class SlashasaurusClient extends Client<true> {
messageToMessageData(page.message)
);
}
page.latestInteraction = interaction;
page.handleId(interaction.customId.split(';')[1], interaction);
}

Expand Down Expand Up @@ -952,14 +950,10 @@ export class SlashasaurusClient extends Client<true> {
: 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,
Expand Down Expand Up @@ -998,12 +992,25 @@ export class SlashasaurusClient extends Client<true> {
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(
Expand Down

0 comments on commit 93e2d87

Please sign in to comment.