From 9492459233ff26ae3e28ed6e96028db30c65f4f9 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 11 Aug 2024 00:47:44 -0500 Subject: [PATCH] Start trying to abstract button interactions --- src/bot-component.ts | 4 ++- src/command-abstractions/button.ts | 41 ++++++++++++++++++++++++++++++ src/wheatley.ts | 8 +++++- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/command-abstractions/button.ts diff --git a/src/bot-component.ts b/src/bot-component.ts index 01d6ad4..9d3da58 100644 --- a/src/bot-component.ts +++ b/src/bot-component.ts @@ -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[]; @@ -78,7 +79,8 @@ export class BotComponent { | TextBasedCommandBuilder | TextBasedCommandBuilder | MessageContextMenuInteractionBuilder - | ModalInteractionBuilder, + | ModalInteractionBuilder + | ButtonInteractionBuilder, ) { this.wheatley.add_command(command); } diff --git a/src/command-abstractions/button.ts b/src/command-abstractions/button.ts new file mode 100644 index 0000000..ead14af --- /dev/null +++ b/src/command-abstractions/button.ts @@ -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 extends BaseInteractionBuilder< + HasHandler, + [Discord.ButtonInteraction] +> { + readonly name: string; + permissions: undefined | bigint = undefined; + + constructor(button: Discord.ButtonBuilder, handler: (x: Discord.ButtonInteraction) => Promise) { + super(); + assert("custom_id" in button.data); + this.name = unwrap(button.data.custom_id); + this.handler = handler; + } + + override to_command_descriptors(): [ConditionalOptional>, unknown] { + if (!this.handler) { + return [undefined as ConditionalOptional>, undefined]; + } else { + return [ + new BaseBotInteraction(this.name, this.handler) as ConditionalOptional< + HasHandler, + BaseBotInteraction + >, + undefined, + ]; + } + } + + set_permissions(permissions: bigint) { + this.permissions = permissions; + return this; + } +} diff --git a/src/wheatley.ts b/src/wheatley.ts index dad69ac..b7e77b0 100644 --- a/src/wheatley.ts +++ b/src/wheatley.ts @@ -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; @@ -995,7 +996,8 @@ export class Wheatley { | TextBasedCommandBuilder | TextBasedCommandBuilder | MessageContextMenuInteractionBuilder - | ModalInteractionBuilder, + | ModalInteractionBuilder + | ButtonInteractionBuilder, ) { if (this.passive) { return; @@ -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) {