UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

88 lines (86 loc) 3.46 kB
import { Identifiers } from "../../errors/Identifiers.mjs"; import { UserError } from "../../errors/UserError.mjs"; import { container } from "@sapphire/pieces"; import { err } from "@sapphire/result"; //#region src/lib/utils/preconditions/PreconditionContainerSingle.ts /** * An {@link IPreconditionContainer} which runs a single precondition from {@link SapphireClient.preconditions}. * @since 1.0.0 */ var PreconditionContainerSingle = class { constructor(data) { if (typeof data === "string") { this.context = {}; this.name = data; } else { this.context = Reflect.get(data, "context") ?? {}; this.name = data.name; } } /** * Runs the container. * @since 1.0.0 * @param message The message that ran this precondition. * @param command The command the message invoked. * @param context The context for the message precondition. */ messageRun(message, command, context = {}) { const precondition = container.stores.get("preconditions").get(this.name); if (precondition) return precondition.messageRun ? precondition.messageRun(message, command, { ...context, ...this.context }) : precondition.error({ identifier: Identifiers.PreconditionMissingMessageHandler, message: `The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.` }); return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })); } /** * Runs the container. * @since 3.0.0 * @param interaction The interaction that ran this precondition. * @param command The command the interaction invoked. * @param context The context for the chat input command precondition. */ chatInputRun(interaction, command, context = {}) { const precondition = container.stores.get("preconditions").get(this.name); if (precondition) return precondition.chatInputRun ? precondition.chatInputRun(interaction, command, { ...context, ...this.context }) : precondition.error({ identifier: Identifiers.PreconditionMissingChatInputHandler, message: `The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.` }); return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })); } /** * Runs the container. * @since 3.0.0 * @param interaction The interaction that ran this precondition. * @param command The command the interaction invoked. * @param context The context for the context menu command precondition. */ contextMenuRun(interaction, command, context = {}) { const precondition = container.stores.get("preconditions").get(this.name); if (precondition) return precondition.contextMenuRun ? precondition.contextMenuRun(interaction, command, { ...context, ...this.context }) : precondition.error({ identifier: Identifiers.PreconditionMissingContextMenuHandler, message: `The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.` }); return err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition "${this.name}" is not available.` })); } }; //#endregion export { PreconditionContainerSingle }; //# sourceMappingURL=PreconditionContainerSingle.mjs.map