feat(builder): add max min length in string option (#8214)

This commit is contained in:
Parbez
2022-07-08 00:15:32 +05:30
committed by GitHub
parent 10ba0080cc
commit 96c8d21f95
7 changed files with 51 additions and 10 deletions

View File

@@ -167,11 +167,13 @@ describe('Application Command toJSON() results', () => {
});
test('GIVEN a string option THEN calling toJSON should return a valid JSON', () => {
expect(getStringOption().toJSON()).toEqual<APIApplicationCommandStringOption>({
expect(getStringOption().setMinLength(1).setMaxLength(10).toJSON()).toEqual<APIApplicationCommandStringOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.String,
required: true,
max_length: 10,
min_length: 1,
});
expect(getStringOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandStringOption>({

View File

@@ -54,7 +54,7 @@
"homepage": "https://discord.js.org",
"dependencies": {
"@sapphire/shapeshift": "^3.4.1",
"discord-api-types": "^0.33.5",
"discord-api-types": "^0.36.1",
"fast-deep-equal": "^3.1.3",
"ts-mixer": "^6.0.1",
"tslib": "^2.4.0"

View File

@@ -1,11 +1,43 @@
import { s } from '@sapphire/shapeshift';
import { APIApplicationCommandStringOption, ApplicationCommandOptionType } from 'discord-api-types/v10';
import { mix } from 'ts-mixer';
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase';
import { ApplicationCommandOptionWithChoicesAndAutocompleteMixin } from '../mixins/ApplicationCommandOptionWithChoicesAndAutocompleteMixin';
const minLengthValidator = s.number.greaterThanOrEqual(0).lessThanOrEqual(6000);
const maxLengthValidator = s.number.greaterThanOrEqual(1).lessThanOrEqual(6000);
@mix(ApplicationCommandOptionWithChoicesAndAutocompleteMixin)
export class SlashCommandStringOption extends ApplicationCommandOptionBase {
public readonly type = ApplicationCommandOptionType.String as const;
public readonly max_length?: number;
public readonly min_length?: number;
/**
* Sets the maximum length of this string option.
*
* @param max - The maximum length this option can be
*/
public setMaxLength(max: number): this {
maxLengthValidator.parse(max);
Reflect.set(this, 'max_length', max);
return this;
}
/**
* Sets the minimum length of this string option.
*
* @param min - The minimum length this option can be
*/
public setMinLength(min: number): this {
minLengthValidator.parse(min);
Reflect.set(this, 'min_length', min);
return this;
}
public toJSON(): APIApplicationCommandStringOption {
this.runRequiredValidations();

View File

@@ -53,7 +53,7 @@
"@discordjs/rest": "workspace:^",
"@sapphire/snowflake": "^3.2.2",
"@types/ws": "^8.5.3",
"discord-api-types": "^0.33.5",
"discord-api-types": "^0.36.1",
"fast-deep-equal": "^3.1.3",
"lodash.snakecase": "^4.1.1",
"tslib": "^2.4.0",

View File

@@ -54,7 +54,7 @@
"@discordjs/collection": "workspace:^",
"@sapphire/async-queue": "^1.3.2",
"@sapphire/snowflake": "^3.2.2",
"discord-api-types": "^0.33.5",
"discord-api-types": "^0.36.1",
"tslib": "^2.4.0",
"undici": "^5.6.0"
},

View File

@@ -52,7 +52,7 @@
"homepage": "https://discord.js.org",
"dependencies": {
"@types/ws": "^8.5.3",
"discord-api-types": "^0.33.5",
"discord-api-types": "^0.36.1",
"prism-media": "^1.3.2",
"tslib": "^2.4.0",
"ws": "^8.8.0"