UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

74 lines (71 loc) 3.22 kB
'use strict'; var pieces = require('@sapphire/pieces'); var result = require('@sapphire/result'); var Identifiers_cjs = require('../errors/Identifiers.cjs'); var Precondition_cjs = require('./Precondition.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var _PreconditionStore = class _PreconditionStore extends pieces.Store { constructor() { super(Precondition_cjs.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: Identifiers_cjs.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 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: Identifiers_cjs.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 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: Identifiers_cjs.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 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(); } }; __name(_PreconditionStore, "PreconditionStore"); var PreconditionStore = _PreconditionStore; exports.PreconditionStore = PreconditionStore; //# sourceMappingURL=PreconditionStore.cjs.map //# sourceMappingURL=PreconditionStore.cjs.map