@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
1 lines • 7.49 kB
Source Map (JSON)
{"version":3,"file":"PreconditionContainerSingle.mjs","names":[],"sources":["../../../../../src/lib/utils/preconditions/PreconditionContainerSingle.ts"],"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"],"mappings":";;;;;;;;;;AAmDA,IAAa,8BAAb,MAA2E;CAe1E,AAAO,YAAY,MAAoC;AACtD,MAAI,OAAO,SAAS,UAAU;AAC7B,QAAK,UAAU,EAAE;AACjB,QAAK,OAAO;SACN;AACN,QAAK,UAAU,QAAQ,IAAI,MAAM,UAAU,IAAI,EAAE;AACjD,QAAK,OAAO,KAAK;;;;;;;;;;CAWnB,AAAO,WAAW,SAAkB,SAAyB,UAA+B,EAAE,EAAE;EAC/F,MAAM,eAAe,UAAU,OAAO,IAAI,gBAAgB,CAAC,IAAI,KAAK,KAAK;AACzE,MAAI,aACH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