@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
1 lines • 8.53 kB
Source Map (JSON)
{"version":3,"sources":["../../../../../src/lib/utils/preconditions/PreconditionContainerSingle.ts"],"names":[],"mappings":";;;;;;AAmDO,IAAM,4BAAA,GAAN,MAAM,4BAA8D,CAAA;AAAA,EAenE,YAAY,IAAoC,EAAA;AACtD,IAAI,IAAA,OAAO,SAAS,QAAU,EAAA;AAC7B,MAAA,IAAA,CAAK,UAAU,EAAC;AAChB,MAAA,IAAA,CAAK,IAAO,GAAA,IAAA;AAAA,KACN,MAAA;AACN,MAAA,IAAA,CAAK,UAAU,OAAQ,CAAA,GAAA,CAAI,IAAM,EAAA,SAAS,KAAK,EAAC;AAChD,MAAA,IAAA,CAAK,OAAO,IAAK,CAAA,IAAA;AAAA;AAClB;AACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,UAAW,CAAA,OAAA,EAAkB,OAAyB,EAAA,OAAA,GAA+B,EAAI,EAAA;AAC/F,IAAM,MAAA,YAAA,GAAe,UAAU,MAAO,CAAA,GAAA,CAAI,eAAe,CAAE,CAAA,GAAA,CAAI,KAAK,IAAI,CAAA;AACxE,IAAA,IAAI,YAAc,EAAA;AACjB,MAAA,OAAO,YAAa,CAAA,UAAA,GACjB,YAAa,CAAA,UAAA,CAAW,SAAS,OAAS,EAAA,EAAE,GAAG,OAAA,EAAS,GAAG,IAAK,CAAA,OAAA,EAAS,CAAA,GACzE,aAAa,KAAM,CAAA;AAAA,QACnB,YAAY,WAAY,CAAA,iCAAA;AAAA,QACxB,SAAS,CAAqB,kBAAA,EAAA,YAAA,CAAa,IAAI,CAAA,mEAAA,EAAsE,QAAQ,IAAI,CAAA,UAAA;AAAA,OACjI,CAAA;AAAA;AAEJ,IAAA,OAAO,GAAI,CAAA,IAAI,SAAU,CAAA,EAAE,UAAY,EAAA,WAAA,CAAY,uBAAyB,EAAA,OAAA,EAAS,CAAqB,kBAAA,EAAA,IAAA,CAAK,IAAI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aAAa,KAAM,CAAA;AAAA,QACnB,YAAY,WAAY,CAAA,qCAAA;AAAA,QACxB,SAAS,CAAqB,kBAAA,EAAA,YAAA,CAAa,IAAI,CAAA,uEAAA,EAA0E,QAAQ,IAAI,CAAA,UAAA;AAAA,OACrI,CAAA;AAAA;AAEJ,IAAA,OAAO,GAAI,CAAA,IAAI,SAAU,CAAA,EAAE,UAAY,EAAA,WAAA,CAAY,uBAAyB,EAAA,OAAA,EAAS,CAAqB,kBAAA,EAAA,IAAA,CAAK,IAAI,CAAA,mBAAA,CAAA,EAAuB,CAAC,CAAA;AAAA;AAE7I,CAAA;AApF2E,MAAA,CAAA,4BAAA,EAAA,6BAAA,CAAA;AAApE,IAAM,2BAAN,GAAA","file":"PreconditionContainerSingle.mjs","sourcesContent":["import { container } from '@sapphire/pieces';\nimport { err } from '@sapphire/result';\nimport type { ChatInputCommandInteraction, ContextMenuCommandInteraction, Message } from 'discord.js';\nimport { Identifiers } from '../../errors/Identifiers';\nimport { UserError } from '../../errors/UserError';\nimport type { Precondition, PreconditionContext, PreconditionKeys, Preconditions, SimplePreconditionKeys } from '../../structures/Precondition';\nimport type { ChatInputCommand, ContextMenuCommand, MessageCommand } from '../../types/CommandTypes';\nimport type { IPreconditionContainer } from './IPreconditionContainer';\n\n/**\n * Defines the simple options for the {@link PreconditionContainerSingle}, where only the name of the precondition can\n * be defined.\n * @since 2.0.0\n */\nexport interface SimplePreconditionSingleResolvableDetails {\n\t/**\n\t * The name of the precondition to retrieve from {@link SapphireClient.preconditions}.\n\t * @since 2.0.0\n\t */\n\tname: SimplePreconditionKeys;\n}\n\n/**\n * Defines the detailed options for the {@link PreconditionContainerSingle}, where both the {@link PreconditionContext} and the\n * name of the precondition can be defined.\n * @since 1.0.0\n */\nexport interface PreconditionSingleResolvableDetails<K extends PreconditionKeys = PreconditionKeys> {\n\t/**\n\t * The name of the precondition to retrieve from {@link SapphireClient.preconditions}.\n\t * @since 1.0.0\n\t */\n\tname: K;\n\n\t/**\n\t * The context to be set at {@link PreconditionContainerSingle.context}.\n\t * @since 1.0.0\n\t */\n\tcontext: Preconditions[K];\n}\n\n/**\n * Defines the data accepted by {@link PreconditionContainerSingle}'s constructor.\n * @since 1.0.0\n */\nexport type PreconditionSingleResolvable = SimplePreconditionKeys | SimplePreconditionSingleResolvableDetails | PreconditionSingleResolvableDetails;\n\n/**\n * An {@link IPreconditionContainer} which runs a single precondition from {@link SapphireClient.preconditions}.\n * @since 1.0.0\n */\nexport class PreconditionContainerSingle implements IPreconditionContainer {\n\t/**\n\t * The context to be used when calling {@link Precondition.run}. This will always be an empty object (`{}`) when the\n\t * container was constructed with a string, otherwise it is a direct reference to the value from\n\t * {@link PreconditionSingleResolvableDetails.context}.\n\t * @since 1.0.0\n\t */\n\tpublic readonly context: Record<PropertyKey, unknown>;\n\n\t/**\n\t * The name of the precondition to run.\n\t * @since 1.0.0\n\t */\n\tpublic readonly name: string;\n\n\tpublic constructor(data: PreconditionSingleResolvable) {\n\t\tif (typeof data === 'string') {\n\t\t\tthis.context = {};\n\t\t\tthis.name = data;\n\t\t} else {\n\t\t\tthis.context = Reflect.get(data, 'context') ?? {};\n\t\t\tthis.name = data.name;\n\t\t}\n\t}\n\n\t/**\n\t * Runs the container.\n\t * @since 1.0.0\n\t * @param message The message that ran this precondition.\n\t * @param command The command the message invoked.\n\t * @param context The context for the message precondition.\n\t */\n\tpublic messageRun(message: Message, command: MessageCommand, context: PreconditionContext = {}) {\n\t\tconst precondition = container.stores.get('preconditions').get(this.name);\n\t\tif (precondition) {\n\t\t\treturn precondition.messageRun\n\t\t\t\t? precondition.messageRun(message, command, { ...context, ...this.context })\n\t\t\t\t: 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\t\t}\n\t\treturn err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition \"${this.name}\" is not available.` }));\n\t}\n\n\t/**\n\t * Runs the container.\n\t * @since 3.0.0\n\t * @param interaction The interaction that ran this precondition.\n\t * @param command The command the interaction invoked.\n\t * @param context The context for the chat input command precondition.\n\t */\n\tpublic chatInputRun(interaction: ChatInputCommandInteraction, command: ChatInputCommand, context: PreconditionContext = {}) {\n\t\tconst precondition = container.stores.get('preconditions').get(this.name);\n\t\tif (precondition) {\n\t\t\treturn precondition.chatInputRun\n\t\t\t\t? precondition.chatInputRun(interaction, command, { ...context, ...this.context })\n\t\t\t\t: 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\t\t}\n\t\treturn err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition \"${this.name}\" is not available.` }));\n\t}\n\n\t/**\n\t * Runs the container.\n\t * @since 3.0.0\n\t * @param interaction The interaction that ran this precondition.\n\t * @param command The command the interaction invoked.\n\t * @param context The context for the context menu command precondition.\n\t */\n\tpublic contextMenuRun(interaction: ContextMenuCommandInteraction, command: ContextMenuCommand, context: PreconditionContext = {}) {\n\t\tconst precondition = container.stores.get('preconditions').get(this.name);\n\t\tif (precondition) {\n\t\t\treturn precondition.contextMenuRun\n\t\t\t\t? precondition.contextMenuRun(interaction, command, { ...context, ...this.context })\n\t\t\t\t: 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\t\t}\n\t\treturn err(new UserError({ identifier: Identifiers.PreconditionUnavailable, message: `The precondition \"${this.name}\" is not available.` }));\n\t}\n}\n"]}