From 2356d2c44736fb83021dacb551625852111c8ce6 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Thu, 18 Aug 2022 22:42:12 -0400 Subject: restructure, experimental presence and member automod, fixed bugs probably made some more bugs --- lib/utils/Arg.ts | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 lib/utils/Arg.ts (limited to 'lib/utils/Arg.ts') diff --git a/lib/utils/Arg.ts b/lib/utils/Arg.ts new file mode 100644 index 0000000..d362225 --- /dev/null +++ b/lib/utils/Arg.ts @@ -0,0 +1,192 @@ +import { + type BaseBushArgumentType, + type BushArgumentType, + type BushArgumentTypeCaster, + type CommandMessage, + type SlashMessage +} from '#lib'; +import { Argument, type Command, type Flag, type ParsedValuePredicate } from 'discord-akairo'; +import { type Message } from 'discord.js'; + +/** + * Casts a phrase to this argument's type. + * @param type - The type to cast to. + * @param message - Message that called the command. + * @param phrase - Phrase to process. + */ +export async function cast(type: T, message: CommandMessage | SlashMessage, phrase: string): Promise>; +export async function cast(type: T, message: CommandMessage | SlashMessage, phrase: string): Promise; +export async function cast(type: AT | ATC, message: CommandMessage | SlashMessage, phrase: string): Promise; +export async function cast( + this: ThisType, + type: ATC | AT, + message: CommandMessage | SlashMessage, + phrase: string +): Promise { + return Argument.cast.call(this, type as any, message.client.commandHandler.resolver, message as Message, phrase); +} + +/** + * Creates a type that is the left-to-right composition of the given types. + * If any of the types fails, the entire composition fails. + * @param types - Types to use. + */ +export function compose(...types: T[]): ATCATCR; +export function compose(...types: T[]): ATCBAT; +export function compose(...types: (AT | ATC)[]): ATC; +export function compose(...types: (AT | ATC)[]): ATC { + return Argument.compose(...(types as any)); +} + +/** + * Creates a type that is the left-to-right composition of the given types. + * If any of the types fails, the composition still continues with the failure passed on. + * @param types - Types to use. + */ +export function composeWithFailure(...types: T[]): ATCATCR; +export function composeWithFailure(...types: T[]): ATCBAT; +export function composeWithFailure(...types: (AT | ATC)[]): ATC; +export function composeWithFailure(...types: (AT | ATC)[]): ATC { + return Argument.composeWithFailure(...(types as any)); +} + +/** + * Checks if something is null, undefined, or a fail flag. + * @param value - Value to check. + */ +export function isFailure(value: any): value is null | undefined | (Flag & { value: any }) { + return Argument.isFailure(value); +} + +/** + * Creates a type from multiple types (product type). + * Only inputs where each type resolves with a non-void value are valid. + * @param types - Types to use. + */ +export function product(...types: T[]): ATCATCR; +export function product(...types: T[]): ATCBAT; +export function product(...types: (AT | ATC)[]): ATC; +export function product(...types: (AT | ATC)[]): ATC { + return Argument.product(...(types as any)); +} + +/** + * Creates a type where the parsed value must be within a range. + * @param type - The type to use. + * @param min - Minimum value. + * @param max - Maximum value. + * @param inclusive - Whether or not to be inclusive on the upper bound. + */ +export function range(type: T, min: number, max: number, inclusive?: boolean): ATCATCR; +export function range(type: T, min: number, max: number, inclusive?: boolean): ATCBAT; +export function range(type: AT | ATC, min: number, max: number, inclusive?: boolean): ATC; +export function range(type: AT | ATC, min: number, max: number, inclusive?: boolean): ATC { + return Argument.range(type as any, min, max, inclusive); +} + +/** + * Creates a type that parses as normal but also tags it with some data. + * Result is in an object `{ tag, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + * @param tag - Tag to add. Defaults to the `type` argument, so useful if it is a string. + */ +export function tagged(type: T, tag?: any): ATCATCR; +export function tagged(type: T, tag?: any): ATCBAT; +export function tagged(type: AT | ATC, tag?: any): ATC; +export function tagged(type: AT | ATC, tag?: any): ATC { + return Argument.tagged(type as any, tag); +} + +/** + * Creates a type from multiple types (union type). + * The first type that resolves to a non-void value is used. + * Each type will also be tagged using `tagged` with themselves. + * @param types - Types to use. + */ +export function taggedUnion(...types: T[]): ATCATCR; +export function taggedUnion(...types: T[]): ATCBAT; +export function taggedUnion(...types: (AT | ATC)[]): ATC; +export function taggedUnion(...types: (AT | ATC)[]): ATC { + return Argument.taggedUnion(...(types as any)); +} + +/** + * Creates a type that parses as normal but also tags it with some data and carries the original input. + * Result is in an object `{ tag, input, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + * @param tag - Tag to add. Defaults to the `type` argument, so useful if it is a string. + */ +export function taggedWithInput(type: T, tag?: any): ATCATCR; +export function taggedWithInput(type: T, tag?: any): ATCBAT; +export function taggedWithInput(type: AT | ATC, tag?: any): ATC; +export function taggedWithInput(type: AT | ATC, tag?: any): ATC { + return Argument.taggedWithInput(type as any, tag); +} + +/** + * Creates a type from multiple types (union type). + * The first type that resolves to a non-void value is used. + * @param types - Types to use. + */ +export function union(...types: T[]): ATCATCR; +export function union(...types: T[]): ATCBAT; +export function union(...types: (AT | ATC)[]): ATC; +export function union(...types: (AT | ATC)[]): ATC { + return Argument.union(...(types as any)); +} + +/** + * Creates a type with extra validation. + * If the predicate is not true, the value is considered invalid. + * @param type - The type to use. + * @param predicate - The predicate function. + */ +export function validate(type: T, predicate: ParsedValuePredicate): ATCATCR; +export function validate(type: T, predicate: ParsedValuePredicate): ATCBAT; +export function validate(type: AT | ATC, predicate: ParsedValuePredicate): ATC; +export function validate(type: AT | ATC, predicate: ParsedValuePredicate): ATC { + return Argument.validate(type as any, predicate); +} + +/** + * Creates a type that parses as normal but also carries the original input. + * Result is in an object `{ input, value }` and wrapped in `Flag.fail` when failed. + * @param type - The type to use. + */ +export function withInput(type: T): ATC>; +export function withInput(type: T): ATCBAT; +export function withInput(type: AT | ATC): ATC; +export function withInput(type: AT | ATC): ATC { + return Argument.withInput(type as any); +} + +type BushArgumentTypeCasterReturn = R extends BushArgumentTypeCaster ? S : R; +/** ```ts + * = BushArgumentTypeCaster + * ``` */ +type ATC = BushArgumentTypeCaster; +/** ```ts + * keyof BaseBushArgumentType + * ``` */ +type KBAT = keyof BaseBushArgumentType; +/** ```ts + * = BushArgumentTypeCasterReturn + * ``` */ +type ATCR = BushArgumentTypeCasterReturn; +/** ```ts + * BushArgumentType + * ``` */ +type AT = BushArgumentType; +/** ```ts + * BaseBushArgumentType + * ``` */ +type BAT = BaseBushArgumentType; + +/** ```ts + * = BushArgumentTypeCaster> + * ``` */ +type ATCATCR = BushArgumentTypeCaster>; +/** ```ts + * = BushArgumentTypeCaster + * ``` */ +type ATCBAT = BushArgumentTypeCaster; -- cgit