apps/bot/src/handlers/buttons.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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",
});
|