all repos — stealth-developers @ 158b915b8f28f27e37e948b8f6ae783b0cadbccf

apps/bot/src/handlers/modals.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 { InteractionHandler, InteractionHandlerTypes, container } from "@sapphire/framework";
import type { ModalSubmitInteraction } from "discord.js";
import { modalRegistry } from "$/structures/ModalRegistry";

export class ModalHandler extends InteractionHandler {
	public constructor(ctx: InteractionHandler.LoaderContext, options: InteractionHandler.Options) {
		super(ctx, {
			...options,
			interactionHandlerType: InteractionHandlerTypes.ModalSubmit,
		});
	}

	public override parse(interaction: ModalSubmitInteraction) {
		const [id] = interaction.customId.split(":");
		if (!id) return this.none();
		const registered = modalRegistry.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: ModalSubmitInteraction,
		{ registered, args }: { registered: any; args: string[] },
	) {
		console.log("Running modal interaction with ID:", registered.definition.id);
		try {
			await registered.run(interaction, args);
		} catch (error) {
			this.container.logger.error("Failed to run modal interaction:", error);
		}
	}
}

container.stores.loadPiece({
	name: "ModalHandler",
	piece: ModalHandler,
	store: "interaction-handlers",
});