UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

97 lines (95 loc) 2.66 kB
import { Args } from "../parsers/Args.mjs"; import { AliasPiece } from "@sapphire/pieces"; //#region src/lib/structures/Argument.ts /** * The base argument class. This class is abstract and is to be extended by subclasses implementing the methods. In * Sapphire's workflow, arguments are called when using {@link Args}'s methods (usually used inside {@link Command}s by default). * * @example * ```typescript * // TypeScript: * import { Argument } from '@sapphire/framework'; * import { URL } from 'node:url'; * * // Define a class extending `Argument`, then export it. * // NOTE: You can use `export default` or `export =` too. * export class CoreArgument extends Argument<URL> { * public constructor(context: Argument.LoaderContext) { * super(context, { name: 'hyperlink', aliases: ['url'] }); * } * * public run(argument: string, context: Argument.Context): Argument.Result<URL> { * try { * return this.ok(new URL(argument)); * } catch { * return this.error({ * parameter: argument, * context, * identifier: 'ArgumentHyperlinkInvalidURL', * message: 'The argument did not resolve to a valid URL.' * }); * } * } * } * * // Augment the ArgType structure so `args.pick('url')`, `args.repeat('url')` * // and others have a return type of `URL`. * declare module '@sapphire/framework' { * export interface ArgType { * url: URL; * } * } * ``` * * @example * ```javascript * // JavaScript: * const { Argument } = require('@sapphire/framework'); * * // Define a class extending `Argument`, then export it. * module.exports = class CoreArgument extends Argument { * constructor(context) { * super(context, { name: 'hyperlink', aliases: ['url'] }); * } * * run(argument, context) { * try { * return this.ok(new URL(argument)); * } catch { * return this.error({ * parameter: argument, * context, * identifier: 'ArgumentHyperlinkInvalidURL', * message: 'The argument did not resolve to a valid URL.' * }); * } * } * } * ``` */ var Argument = class extends AliasPiece { constructor(context, options = {}) { super(context, options); } /** * Wraps a value into a successful value. * @param value The value to wrap. */ ok(value) { return Args.ok(value); } /** * Constructs an {@link Err} result containing an {@link ArgumentError} with a custom type. * @param options The options to pass to the ArgumentError. */ error(options) { return Args.error({ argument: this, identifier: this.name, ...options }); } }; //#endregion export { Argument }; //# sourceMappingURL=Argument.mjs.map