@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
82 lines (80 loc) • 3.35 kB
JavaScript
import { __name } from '../../chunk-PAWJFY3S.mjs';
import { Piece } from '@sapphire/pieces';
import { Option } from '@sapphire/result';
var _InteractionHandler = class _InteractionHandler extends 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 Option.some(data);
}
none() {
return Option.none;
}
toJSON() {
return {
...super.toJSON(),
interactionHandlerType: this.interactionHandlerType
};
}
};
__name(_InteractionHandler, "InteractionHandler");
var InteractionHandler = _InteractionHandler;
var InteractionHandlerTypes = /* @__PURE__ */ ((InteractionHandlerTypes2) => {
InteractionHandlerTypes2[InteractionHandlerTypes2["Button"] = 0] = "Button";
InteractionHandlerTypes2[InteractionHandlerTypes2["SelectMenu"] = 1] = "SelectMenu";
InteractionHandlerTypes2[InteractionHandlerTypes2["ModalSubmit"] = 2] = "ModalSubmit";
InteractionHandlerTypes2[InteractionHandlerTypes2["MessageComponent"] = 3] = "MessageComponent";
InteractionHandlerTypes2[InteractionHandlerTypes2["Autocomplete"] = 4] = "Autocomplete";
return InteractionHandlerTypes2;
})(InteractionHandlerTypes || {});
export { InteractionHandler, InteractionHandlerTypes };
//# sourceMappingURL=InteractionHandler.mjs.map
//# sourceMappingURL=InteractionHandler.mjs.map