UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

68 lines (65 loc) 3.51 kB
'use strict'; var pieces = require('@sapphire/pieces'); var result = require('@sapphire/result'); var Events_cjs = require('../types/Events.cjs'); var InteractionHandler_cjs = require('./InteractionHandler.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var _InteractionHandlerStore = class _InteractionHandlerStore extends pieces.Store { constructor() { super(InteractionHandler_cjs.InteractionHandler, { name: "interaction-handlers" }); } async run(interaction) { if (this.size === 0) return false; const promises = []; for (const handler of this.values()) { const filter = InteractionHandlerFilters.get(handler.interactionHandlerType); if (!filter?.(interaction)) continue; const result$1 = await result.Result.fromAsync(() => handler.parse(interaction)); result$1.match({ ok: /* @__PURE__ */ __name((option) => { this.container.client.emit(Events_cjs.Events.InteractionHandlerParseSuccess, option, { interaction, handler }); option.match({ // If the `parse` method returned a `Some` (whatever that `Some`'s value is, it should be handled) some: /* @__PURE__ */ __name((value) => { this.container.client.emit(Events_cjs.Events.InteractionHandlerParseSome, option, { interaction, handler, value }); const promise = result.Result.fromAsync(() => handler.run(interaction, value)).then((res) => res.mapErr((error) => ({ handler, error }))); promises.push(promise); }, "some"), // Emit an event to the user to let them know parse was successful and `none` was returned. none: /* @__PURE__ */ __name(() => this.container.client.emit(Events_cjs.Events.InteractionHandlerParseNone, option, { interaction, handler }), "none") }); }, "ok"), err: /* @__PURE__ */ __name((error) => { this.container.client.emit(Events_cjs.Events.InteractionHandlerParseError, error, { interaction, handler }); }, "err") }); } if (promises.length === 0) return false; const results = await Promise.allSettled(promises); for (const result of results) { const res = result.value; res.inspectErr( (value) => this.container.client.emit(Events_cjs.Events.InteractionHandlerError, value.error, { interaction, handler: value.handler }) ); } return true; } }; __name(_InteractionHandlerStore, "InteractionHandlerStore"); var InteractionHandlerStore = _InteractionHandlerStore; var InteractionHandlerFilters = /* @__PURE__ */ new Map([ [InteractionHandler_cjs.InteractionHandlerTypes.Button, (interaction) => interaction.isButton()], [InteractionHandler_cjs.InteractionHandlerTypes.SelectMenu, (interaction) => interaction.isAnySelectMenu()], [InteractionHandler_cjs.InteractionHandlerTypes.ModalSubmit, (interaction) => interaction.isModalSubmit()], [InteractionHandler_cjs.InteractionHandlerTypes.MessageComponent, (interaction) => interaction.isMessageComponent()], [InteractionHandler_cjs.InteractionHandlerTypes.Autocomplete, (Interaction) => Interaction.isAutocomplete()] ]); exports.InteractionHandlerFilters = InteractionHandlerFilters; exports.InteractionHandlerStore = InteractionHandlerStore; //# sourceMappingURL=InteractionHandlerStore.cjs.map //# sourceMappingURL=InteractionHandlerStore.cjs.map