UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

71 lines (69 loc) 3.02 kB
import { __name } from '../../chunk-PAWJFY3S.mjs'; import { Store } from '@sapphire/pieces'; import { Result } from '@sapphire/result'; import { Identifiers } from '../errors/Identifiers.mjs'; import { Precondition } from './Precondition.mjs'; var _PreconditionStore = class _PreconditionStore extends Store { constructor() { super(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.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.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.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.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.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.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; export { PreconditionStore }; //# sourceMappingURL=PreconditionStore.mjs.map //# sourceMappingURL=PreconditionStore.mjs.map