import { container, InteractionHandler, InteractionHandlerTypes } from "@sapphire/framework"; import type { ButtonInteraction } from "discord.js"; import { buttonRegistry } from "../lib/registries"; export class ButtonHandler extends InteractionHandler { public constructor(ctx: InteractionHandler.LoaderContext, options: InteractionHandler.Options) { super(ctx, { ...options, interactionHandlerType: InteractionHandlerTypes.Button, }); } public override parse(interaction: ButtonInteraction) { const [id] = interaction.customId.split(":"); if (!id) return this.none(); const registered = buttonRegistry.get(id); if (!registered) return this.none(); const args = registered.definition.parse(interaction.customId); if (!args) return this.none(); return this.some({ registered, args }); } public async run( interaction: ButtonInteraction, { registered, args }: { registered: any; args: string[] }, ) { console.log("Running button interaction with ID:", registered.definition.id); try { await registered.run(interaction, args); } catch (error) { this.container.logger.error("Failed to execute button interaction:", error); } } } container.stores.loadPiece({ name: "ButtonHandler", piece: ButtonHandler, store: "interaction-handlers", });