@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
1 lines • 5.34 kB
Source Map (JSON)
{"version":3,"file":"PreconditionStore.mjs","names":[],"sources":["../../../../src/lib/structures/PreconditionStore.ts"],"sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Result } from '@sapphire/result';\nimport type { ChatInputCommandInteraction, ContextMenuCommandInteraction, Message } from 'discord.js';\nimport { Identifiers } from '../errors/Identifiers';\nimport type { ChatInputCommand, ContextMenuCommand, MessageCommand } from '../types/CommandTypes';\nimport { Precondition, type AsyncPreconditionResult } from './Precondition';\n\nexport class PreconditionStore extends Store<Precondition, 'preconditions'> {\n\tprivate readonly globalPreconditions: Precondition[] = [];\n\n\tpublic constructor() {\n\t\tsuper(Precondition, { name: 'preconditions' });\n\t}\n\n\tpublic async messageRun(message: Message, command: MessageCommand, context: Precondition.Context = {}): AsyncPreconditionResult {\n\t\tfor (const precondition of this.globalPreconditions) {\n\t\t\tconst result = precondition.messageRun\n\t\t\t\t? await precondition.messageRun(message, command, context)\n\t\t\t\t: await precondition.error({\n\t\t\t\t\t\tidentifier: Identifiers.PreconditionMissingMessageHandler,\n\t\t\t\t\t\tmessage: `The precondition \"${precondition.name}\" is missing a \"messageRun\" handler, but it was requested for the \"${command.name}\" command.`\n\t\t\t\t\t});\n\n\t\t\tif (result.isErr()) {\n\t\t\t\treturn result;\n\t\t\t}\n\t\t}\n\n\t\treturn Result.ok();\n\t}\n\n\tpublic async chatInputRun(\n\t\tinteraction: ChatInputCommandInteraction,\n\t\tcommand: ChatInputCommand,\n\t\tcontext: Precondition.Context = {}\n\t): AsyncPreconditionResult {\n\t\tfor (const precondition of this.globalPreconditions) {\n\t\t\tconst result = precondition.chatInputRun\n\t\t\t\t? await precondition.chatInputRun(interaction, command, context)\n\t\t\t\t: await precondition.error({\n\t\t\t\t\t\tidentifier: Identifiers.PreconditionMissingChatInputHandler,\n\t\t\t\t\t\tmessage: `The precondition \"${precondition.name}\" is missing a \"chatInputRun\" handler, but it was requested for the \"${command.name}\" command.`\n\t\t\t\t\t});\n\n\t\t\tif (result.isErr()) {\n\t\t\t\treturn result;\n\t\t\t}\n\t\t}\n\n\t\treturn Result.ok();\n\t}\n\n\tpublic async contextMenuRun(\n\t\tinteraction: ContextMenuCommandInteraction,\n\t\tcommand: ContextMenuCommand,\n\t\tcontext: Precondition.Context = {}\n\t): AsyncPreconditionResult {\n\t\tfor (const precondition of this.globalPreconditions) {\n\t\t\tconst result = precondition.contextMenuRun\n\t\t\t\t? await precondition.contextMenuRun(interaction, command, context)\n\t\t\t\t: await precondition.error({\n\t\t\t\t\t\tidentifier: Identifiers.PreconditionMissingContextMenuHandler,\n\t\t\t\t\t\tmessage: `The precondition \"${precondition.name}\" is missing a \"contextMenuRun\" handler, but it was requested for the \"${command.name}\" command.`\n\t\t\t\t\t});\n\n\t\t\tif (result.isErr()) {\n\t\t\t\treturn result;\n\t\t\t}\n\t\t}\n\n\t\treturn Result.ok();\n\t}\n\n\tpublic override set(key: string, value: Precondition): this {\n\t\tif (value.position !== null) {\n\t\t\tconst index = this.globalPreconditions.findIndex((precondition) => precondition.position! >= value.position!);\n\n\t\t\t// If a precondition with lower priority wasn't found, push to the end of the array\n\t\t\tif (index === -1) this.globalPreconditions.push(value);\n\t\t\telse this.globalPreconditions.splice(index, 0, value);\n\t\t}\n\n\t\treturn super.set(key, value);\n\t}\n\n\tpublic override delete(key: string): boolean {\n\t\tconst index = this.globalPreconditions.findIndex((precondition) => precondition.name === key);\n\n\t\t// If the precondition was found, remove it\n\t\tif (index !== -1) this.globalPreconditions.splice(index, 1);\n\n\t\treturn super.delete(key);\n\t}\n\n\tpublic override clear(): void {\n\t\tthis.globalPreconditions.length = 0;\n\t\treturn super.clear();\n\t}\n}\n"],"mappings":";;;;;;AAOA,IAAa,oBAAb,cAAuC,MAAqC;CAG3E,AAAO,cAAc;AACpB,QAAM,cAAc,EAAE,MAAM,iBAAiB,CAAC;OAH9B,sBAAsC,EAAE;;CAMzD,MAAa,WA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