UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

64 lines (62 loc) 3 kB
"use strict"; const require_lib_errors_Identifiers = require('../errors/Identifiers.cjs'); const require_lib_structures_Precondition = require('./Precondition.cjs'); let __sapphire_pieces = require("@sapphire/pieces"); let __sapphire_result = require("@sapphire/result"); //#region src/lib/structures/PreconditionStore.ts var PreconditionStore = class extends __sapphire_pieces.Store { constructor() { super(require_lib_structures_Precondition.Precondition, { name: "preconditions" }); this.globalPreconditions = []; } async messageRun(message, command, context = {}) { for (const precondition of this.globalPreconditions) { const result = precondition.messageRun ? await precondition.messageRun(message, command, context) : await precondition.error({ identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingMessageHandler, message: `The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.` }); if (result.isErr()) return result; } return __sapphire_result.Result.ok(); } async chatInputRun(interaction, command, context = {}) { for (const precondition of this.globalPreconditions) { const result = precondition.chatInputRun ? await precondition.chatInputRun(interaction, command, context) : await precondition.error({ identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingChatInputHandler, message: `The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.` }); if (result.isErr()) return result; } return __sapphire_result.Result.ok(); } async contextMenuRun(interaction, command, context = {}) { for (const precondition of this.globalPreconditions) { const result = precondition.contextMenuRun ? await precondition.contextMenuRun(interaction, command, context) : await precondition.error({ identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingContextMenuHandler, message: `The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.` }); if (result.isErr()) return result; } return __sapphire_result.Result.ok(); } set(key, value) { if (value.position !== null) { const index = this.globalPreconditions.findIndex((precondition) => precondition.position >= value.position); if (index === -1) this.globalPreconditions.push(value); else this.globalPreconditions.splice(index, 0, value); } return super.set(key, value); } delete(key) { const index = this.globalPreconditions.findIndex((precondition) => precondition.name === key); if (index !== -1) this.globalPreconditions.splice(index, 1); return super.delete(key); } clear() { this.globalPreconditions.length = 0; return super.clear(); } }; //#endregion exports.PreconditionStore = PreconditionStore; //# sourceMappingURL=PreconditionStore.cjs.map