refactor(builders-methods): make methods consistent (#7395)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
Ben
2022-02-12 11:03:13 -07:00
committed by GitHub
parent 861f0e2134
commit f4953647ff
7 changed files with 39 additions and 35 deletions

View File

@@ -4,7 +4,9 @@ import { validateMaxChoicesLength } from '../Assertions';
const stringPredicate = z.string().min(1).max(100);
const numberPredicate = z.number().gt(-Infinity).lt(Infinity);
const choicesPredicate = z.tuple([stringPredicate, z.union([stringPredicate, numberPredicate])]).array();
const choicesPredicate = z
.object({ name: stringPredicate, value: z.union([stringPredicate, numberPredicate]) })
.array();
const booleanPredicate = z.boolean();
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends string | number> {
@@ -17,10 +19,10 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
/**
* Adds a choice for this option
*
* @param name The name of the choice
* @param value The value of the choice
* @param choice The choice to add
*/
public addChoice(name: string, value: T): this {
public addChoice(choice: APIApplicationCommandOptionChoice<T>): this {
const { name, value } = choice;
if (this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
@@ -51,18 +53,18 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
*
* @param choices The choices to add
*/
public addChoices(choices: [name: string, value: T][]): this {
public addChoices(...choices: APIApplicationCommandOptionChoice<T>[]): this {
if (this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
choicesPredicate.parse(choices);
for (const [label, value] of choices) this.addChoice(label, value);
for (const entry of choices) this.addChoice(entry);
return this;
}
public setChoices(choices: [name: string, value: T][]): this {
public setChoices<Input extends APIApplicationCommandOptionChoice<T>[]>(...choices: Input): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
@@ -70,7 +72,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
choicesPredicate.parse(choices);
Reflect.set(this, 'choices', []);
for (const [label, value] of choices) this.addChoice(label, value);
for (const entry of choices) this.addChoice(entry);
return this;
}