UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

1 lines 6.39 kB
{"version":3,"sources":["../../../../src/lib/structures/PreconditionStore.ts"],"names":[],"mappings":";;;;;;AAOO,IAAM,kBAAA,GAAN,MAAM,kBAAA,SAA0B,KAAqC,CAAA;AAAA,EAGpE,WAAc,GAAA;AACpB,IAAA,KAAA,CAAM,YAAc,EAAA,EAAE,IAAM,EAAA,eAAA,EAAiB,CAAA;AAH9C,IAAA,IAAA,CAAiB,sBAAsC,EAAC;AAAA;AAIxD,EAEA,MAAa,UAAW,CAAA,OAAA,EAAkB,OAAyB,EAAA,OAAA,GAAgC,EAA6B,EAAA;AAC/H,IAAW,KAAA,MAAA,YAAA,IAAgB,KAAK,mBAAqB,EAAA;AACpD,MAAA,MAAM,MAAS,GAAA,YAAA,CAAa,UACzB,GAAA,MAAM,YAAa,CAAA,UAAA,CAAW,OAAS,EAAA,OAAA,EAAS,OAAO,CAAA,GACvD,MAAM,YAAA,CAAa,KAAM,CAAA;AAAA,QACzB,YAAY,WAAY,CAAA,iCAAA;AAAA,QACxB,SAAS,CAAqB,kBAAA,EAAA,YAAA,CAAa,IAAI,CAAA,mEAAA,EAAsE,QAAQ,IAAI,CAAA,UAAA;AAAA,OACjI,CAAA;AAEH,MAAI,IAAA,MAAA,CAAO,OAAS,EAAA;AACnB,QAAO,OAAA,MAAA;AAAA;AACR;AAGD,IAAA,OAAO,OAAO,EAAG,EAAA;AAAA;AAClB,EAEA,MAAa,YACZ,CAAA,WAAA,EACA,OACA,EAAA,OAAA,GAAgC,EACN,EAAA;AAC1B,IAAW,KAAA,MAAA,YAAA,IAAgB,KAAK,mBAAqB,EAAA;AACpD,MAAA,MAAM,MAAS,GAAA,YAAA,CAAa,YACzB,GAAA,MAAM,YAAa,CAAA,YAAA,CAAa,WAAa,EAAA,OAAA,EAAS,OAAO,CAAA,GAC7D,MAAM,YAAA,CAAa,KAAM,CAAA;AAAA,QACzB,YAAY,WAAY,CAAA,mCAAA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AAA,CAAM,GAAI,CAAA,GAAA,EAAK,KAAK,CAAA;AAAA;AAC5B,EAEgB,OAAO,GAAsB,EAAA;AAC5C,IAAM,MAAA,KAAA,GAAQ,KAAK,mBAAoB,CAAA,SAAA,CAAU,CAAC,YAAiB,KAAA,YAAA,CAAa,SAAS,GAAG,CAAA;AAG5F,IAAA,IAAI,UAAU,EAAI,EAAA,IAAA,CAAK,mBAAoB,CAAA,MAAA,CAAO,OAAO,CAAC,CAAA;AAE1D,IAAO,OAAA,KAAA,CAAM,OAAO,GAAG,CAAA;AAAA;AACxB,EAEgB,KAAc,GAAA;AAC7B,IAAA,IAAA,CAAK,oBAAoB,MAAS,GAAA,CAAA;AAClC,IAAA,OAAO,MAAM,KAAM,EAAA;AAAA;AAErB,CAAA;AA3F4E,MAAA,CAAA,kBAAA,EAAA,mBAAA,CAAA;AAArE,IAAM,iBAAN,GAAA","file":"PreconditionStore.mjs","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"]}