Skip to content

Commit

Permalink
Start trying to abstract button interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Aug 11, 2024
1 parent 5de7ed8 commit 9492459
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/bot-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Wheatley } from "./wheatley.js";
import { MessageContextMenuInteractionBuilder } from "./command-abstractions/context-menu.js";
import { ModalInteractionBuilder } from "./command-abstractions/modal.js";
import { TextBasedCommandBuilder } from "./command-abstractions/text-based-command-builder.js";
import { ButtonInteractionBuilder } from "./command-abstractions/button.js";

type Arr = readonly unknown[];

Expand Down Expand Up @@ -78,7 +79,8 @@ export class BotComponent {
| TextBasedCommandBuilder<T, true, true>
| TextBasedCommandBuilder<T, true, false, true>
| MessageContextMenuInteractionBuilder<true>
| ModalInteractionBuilder<true>,
| ModalInteractionBuilder<true>
| ButtonInteractionBuilder<true>,
) {
this.wheatley.add_command(command);
}
Expand Down
41 changes: 41 additions & 0 deletions src/command-abstractions/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { strict as assert } from "assert";

import * as Discord from "discord.js";

import { unwrap } from "../utils/misc.js";
import { ConditionalOptional } from "../utils/typing.js";
import { BaseInteractionBuilder, BaseBotInteraction } from "./interaction-base.js";

export class ButtonInteractionBuilder<HasHandler extends boolean = false> extends BaseInteractionBuilder<
HasHandler,
[Discord.ButtonInteraction]
> {
readonly name: string;
permissions: undefined | bigint = undefined;

constructor(button: Discord.ButtonBuilder, handler: (x: Discord.ButtonInteraction) => Promise<void>) {
super();
assert("custom_id" in button.data);
this.name = unwrap(button.data.custom_id);
this.handler = handler;
}

override to_command_descriptors(): [ConditionalOptional<HasHandler, BaseBotInteraction<any>>, unknown] {
if (!this.handler) {
return [undefined as ConditionalOptional<HasHandler, BaseBotInteraction<any>>, undefined];
} else {
return [
new BaseBotInteraction(this.name, this.handler) as ConditionalOptional<
HasHandler,
BaseBotInteraction<any>
>,
undefined,
];
}
}

set_permissions(permissions: bigint) {
this.permissions = permissions;
return this;
}
}
8 changes: 7 additions & 1 deletion src/wheatley.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { TypedEventEmitter } from "./utils/event-emitter.js";
import { setup_metrics_server } from "./infra/prometheus.js";
import { moderation_entry } from "./infra/schemata/moderation.js";
import { wheatley_database_info } from "./infra/schemata/wheatley.js";
import { ButtonInteractionBuilder } from "./command-abstractions/button.js";

// Thu Jul 01 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
export const SERVER_SUGGESTION_TRACKER_START_TIME = 1625112000000;
Expand Down Expand Up @@ -995,7 +996,8 @@ export class Wheatley {
| TextBasedCommandBuilder<T, true, true>
| TextBasedCommandBuilder<T, true, false, true>
| MessageContextMenuInteractionBuilder<true>
| ModalInteractionBuilder<true>,
| ModalInteractionBuilder<true>
| ButtonInteractionBuilder<true>,
) {
if (this.passive) {
return;
Expand Down Expand Up @@ -1244,6 +1246,10 @@ export class Wheatley {
const fields = command.fields.map(id => interaction.fields.getTextInputValue(id));
await command.handler(interaction, ...(id ? [id, ...fields] : fields));
}
} else if (interaction.isButton()) {
// TODO: permissions
assert(interaction.customId in this.other_commands);
await this.other_commands[interaction.customId].handler(interaction);
}
// TODO: Notify if errors occur in the handler....
} catch (e) {
Expand Down

0 comments on commit 9492459

Please sign in to comment.