UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

82 lines (80 loc) 3.2 kB
"use strict"; let __sapphire_pieces = require("@sapphire/pieces"); let __sapphire_result = require("@sapphire/result"); //#region src/lib/structures/InteractionHandler.ts var InteractionHandler = class extends __sapphire_pieces.Piece { constructor(context, options) { super(context, options); this.interactionHandlerType = options.interactionHandlerType; } /** * A custom function that will be called when checking if an interaction should be passed to this handler. * You can use this method to not only filter by ids, but also pre-parse the data from the id for use in the run method. * * By default, all interactions of the type you specified will run in a handler. You should override this method * to change that behavior. * * @example * ```typescript * // Parsing a button handler * public override parse(interaction: ButtonInteraction) { * if (interaction.customId.startsWith('my-awesome-clicky-button')) { * // Returning a `some` here means that the run method should be called next! * return this.some({ isMyBotAwesome: true, awesomenessLevel: 9001 }); * } * * // Returning a `none` means this interaction shouldn't run in this handler * return this.none(); * } * ``` * * @example * ```typescript * // Getting data from a database based on the custom id * public override async parse(interaction: ButtonInteraction) { * // This code is purely for demonstration purposes only! * if (interaction.customId.startsWith('example-data')) { * const [, userId, channelId] = interaction.customId.split('.'); * * const dataFromDatabase = await container.prisma.exampleData.findFirst({ where: { userId, channelId } }); * * // Returning a `some` here means that the run method should be called next! * return this.some(dataFromDatabase); * } * * // Returning a `none` means this interaction shouldn't run in this handler * return this.none(); * } * ``` * * @returns An {@link Option} (or a {@link Promise Promised} {@link Option}) that indicates if this interaction should be * handled by this handler, and any extra data that should be passed to the {@link InteractionHandler.run run method} */ parse(_interaction) { return this.some(); } some(data) { return __sapphire_result.Option.some(data); } none() { return __sapphire_result.Option.none; } toJSON() { return { ...super.toJSON(), interactionHandlerType: this.interactionHandlerType }; } }; let InteractionHandlerTypes = /* @__PURE__ */ function(InteractionHandlerTypes$1) { InteractionHandlerTypes$1[InteractionHandlerTypes$1["Button"] = 0] = "Button"; InteractionHandlerTypes$1[InteractionHandlerTypes$1["SelectMenu"] = 1] = "SelectMenu"; InteractionHandlerTypes$1[InteractionHandlerTypes$1["ModalSubmit"] = 2] = "ModalSubmit"; InteractionHandlerTypes$1[InteractionHandlerTypes$1["MessageComponent"] = 3] = "MessageComponent"; InteractionHandlerTypes$1[InteractionHandlerTypes$1["Autocomplete"] = 4] = "Autocomplete"; return InteractionHandlerTypes$1; }({}); //#endregion exports.InteractionHandler = InteractionHandler; exports.InteractionHandlerTypes = InteractionHandlerTypes; //# sourceMappingURL=InteractionHandler.cjs.map