mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
feat(builder): add max min length in string option (#8214)
This commit is contained in:
@@ -167,11 +167,13 @@ describe('Application Command toJSON() results', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('GIVEN a string option THEN calling toJSON should return a valid JSON', () => {
|
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',
|
name: 'owo',
|
||||||
description: 'Testing 123',
|
description: 'Testing 123',
|
||||||
type: ApplicationCommandOptionType.String,
|
type: ApplicationCommandOptionType.String,
|
||||||
required: true,
|
required: true,
|
||||||
|
max_length: 10,
|
||||||
|
min_length: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(getStringOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandStringOption>({
|
expect(getStringOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandStringOption>({
|
||||||
|
|||||||
@@ -54,7 +54,7 @@
|
|||||||
"homepage": "https://discord.js.org",
|
"homepage": "https://discord.js.org",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sapphire/shapeshift": "^3.4.1",
|
"@sapphire/shapeshift": "^3.4.1",
|
||||||
"discord-api-types": "^0.33.5",
|
"discord-api-types": "^0.36.1",
|
||||||
"fast-deep-equal": "^3.1.3",
|
"fast-deep-equal": "^3.1.3",
|
||||||
"ts-mixer": "^6.0.1",
|
"ts-mixer": "^6.0.1",
|
||||||
"tslib": "^2.4.0"
|
"tslib": "^2.4.0"
|
||||||
|
|||||||
@@ -1,11 +1,43 @@
|
|||||||
|
import { s } from '@sapphire/shapeshift';
|
||||||
import { APIApplicationCommandStringOption, ApplicationCommandOptionType } from 'discord-api-types/v10';
|
import { APIApplicationCommandStringOption, ApplicationCommandOptionType } from 'discord-api-types/v10';
|
||||||
import { mix } from 'ts-mixer';
|
import { mix } from 'ts-mixer';
|
||||||
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase';
|
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase';
|
||||||
import { ApplicationCommandOptionWithChoicesAndAutocompleteMixin } from '../mixins/ApplicationCommandOptionWithChoicesAndAutocompleteMixin';
|
import { ApplicationCommandOptionWithChoicesAndAutocompleteMixin } from '../mixins/ApplicationCommandOptionWithChoicesAndAutocompleteMixin';
|
||||||
|
|
||||||
|
const minLengthValidator = s.number.greaterThanOrEqual(0).lessThanOrEqual(6000);
|
||||||
|
const maxLengthValidator = s.number.greaterThanOrEqual(1).lessThanOrEqual(6000);
|
||||||
|
|
||||||
@mix(ApplicationCommandOptionWithChoicesAndAutocompleteMixin)
|
@mix(ApplicationCommandOptionWithChoicesAndAutocompleteMixin)
|
||||||
export class SlashCommandStringOption extends ApplicationCommandOptionBase {
|
export class SlashCommandStringOption extends ApplicationCommandOptionBase {
|
||||||
public readonly type = ApplicationCommandOptionType.String as const;
|
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 {
|
public toJSON(): APIApplicationCommandStringOption {
|
||||||
this.runRequiredValidations();
|
this.runRequiredValidations();
|
||||||
|
|||||||
@@ -53,7 +53,7 @@
|
|||||||
"@discordjs/rest": "workspace:^",
|
"@discordjs/rest": "workspace:^",
|
||||||
"@sapphire/snowflake": "^3.2.2",
|
"@sapphire/snowflake": "^3.2.2",
|
||||||
"@types/ws": "^8.5.3",
|
"@types/ws": "^8.5.3",
|
||||||
"discord-api-types": "^0.33.5",
|
"discord-api-types": "^0.36.1",
|
||||||
"fast-deep-equal": "^3.1.3",
|
"fast-deep-equal": "^3.1.3",
|
||||||
"lodash.snakecase": "^4.1.1",
|
"lodash.snakecase": "^4.1.1",
|
||||||
"tslib": "^2.4.0",
|
"tslib": "^2.4.0",
|
||||||
|
|||||||
@@ -54,7 +54,7 @@
|
|||||||
"@discordjs/collection": "workspace:^",
|
"@discordjs/collection": "workspace:^",
|
||||||
"@sapphire/async-queue": "^1.3.2",
|
"@sapphire/async-queue": "^1.3.2",
|
||||||
"@sapphire/snowflake": "^3.2.2",
|
"@sapphire/snowflake": "^3.2.2",
|
||||||
"discord-api-types": "^0.33.5",
|
"discord-api-types": "^0.36.1",
|
||||||
"tslib": "^2.4.0",
|
"tslib": "^2.4.0",
|
||||||
"undici": "^5.6.0"
|
"undici": "^5.6.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -52,7 +52,7 @@
|
|||||||
"homepage": "https://discord.js.org",
|
"homepage": "https://discord.js.org",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/ws": "^8.5.3",
|
"@types/ws": "^8.5.3",
|
||||||
"discord-api-types": "^0.33.5",
|
"discord-api-types": "^0.36.1",
|
||||||
"prism-media": "^1.3.2",
|
"prism-media": "^1.3.2",
|
||||||
"tslib": "^2.4.0",
|
"tslib": "^2.4.0",
|
||||||
"ws": "^8.8.0"
|
"ws": "^8.8.0"
|
||||||
|
|||||||
17
yarn.lock
17
yarn.lock
@@ -2855,7 +2855,7 @@ __metadata:
|
|||||||
"@sapphire/shapeshift": ^3.4.1
|
"@sapphire/shapeshift": ^3.4.1
|
||||||
"@types/node": ^18.0.3
|
"@types/node": ^18.0.3
|
||||||
c8: ^7.11.3
|
c8: ^7.11.3
|
||||||
discord-api-types: ^0.33.5
|
discord-api-types: ^0.36.1
|
||||||
eslint: ^8.19.0
|
eslint: ^8.19.0
|
||||||
fast-deep-equal: ^3.1.3
|
fast-deep-equal: ^3.1.3
|
||||||
prettier: ^2.7.1
|
prettier: ^2.7.1
|
||||||
@@ -3001,7 +3001,7 @@ __metadata:
|
|||||||
"@sapphire/async-queue": ^1.3.2
|
"@sapphire/async-queue": ^1.3.2
|
||||||
"@sapphire/snowflake": ^3.2.2
|
"@sapphire/snowflake": ^3.2.2
|
||||||
c8: ^7.11.3
|
c8: ^7.11.3
|
||||||
discord-api-types: ^0.33.5
|
discord-api-types: ^0.36.1
|
||||||
eslint: ^8.19.0
|
eslint: ^8.19.0
|
||||||
prettier: ^2.7.1
|
prettier: ^2.7.1
|
||||||
tslib: ^2.4.0
|
tslib: ^2.4.0
|
||||||
@@ -3041,7 +3041,7 @@ __metadata:
|
|||||||
"@microsoft/api-extractor": ^7.28.3
|
"@microsoft/api-extractor": ^7.28.3
|
||||||
"@types/jest": ^28.1.4
|
"@types/jest": ^28.1.4
|
||||||
"@types/ws": ^8.5.3
|
"@types/ws": ^8.5.3
|
||||||
discord-api-types: ^0.33.5
|
discord-api-types: ^0.36.1
|
||||||
eslint: ^8.19.0
|
eslint: ^8.19.0
|
||||||
jest: ^28.1.2
|
jest: ^28.1.2
|
||||||
jest-websocket-mock: ^2.3.0
|
jest-websocket-mock: ^2.3.0
|
||||||
@@ -7886,13 +7886,20 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"discord-api-types@npm:^0.33.3, discord-api-types@npm:^0.33.5":
|
"discord-api-types@npm:^0.33.3":
|
||||||
version: 0.33.5
|
version: 0.33.5
|
||||||
resolution: "discord-api-types@npm:0.33.5"
|
resolution: "discord-api-types@npm:0.33.5"
|
||||||
checksum: 6dcaad640c5693a69c9a4f5e444e739dde11ba835164ae6fd3dd5a1ab7b4d7f96cd022ed653eeaff2c8051ead0d998a5d502a2915cfacdde596364b82d9e3b3f
|
checksum: 6dcaad640c5693a69c9a4f5e444e739dde11ba835164ae6fd3dd5a1ab7b4d7f96cd022ed653eeaff2c8051ead0d998a5d502a2915cfacdde596364b82d9e3b3f
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"discord-api-types@npm:^0.36.1":
|
||||||
|
version: 0.36.1
|
||||||
|
resolution: "discord-api-types@npm:0.36.1"
|
||||||
|
checksum: 6f9cae3d855a5c8fe6585f5f827795260683038d7b66ba9f1633e96005047d4c0d07bc084eb7bfbb967c5b99a0257b03cb94634fe7cd8a4975fce12bba89b306
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"discord.js@workspace:packages/discord.js":
|
"discord.js@workspace:packages/discord.js":
|
||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "discord.js@workspace:packages/discord.js"
|
resolution: "discord.js@workspace:packages/discord.js"
|
||||||
@@ -7905,7 +7912,7 @@ __metadata:
|
|||||||
"@sapphire/snowflake": ^3.2.2
|
"@sapphire/snowflake": ^3.2.2
|
||||||
"@types/node": ^18.0.3
|
"@types/node": ^18.0.3
|
||||||
"@types/ws": ^8.5.3
|
"@types/ws": ^8.5.3
|
||||||
discord-api-types: ^0.33.5
|
discord-api-types: ^0.36.1
|
||||||
dtslint: ^4.2.1
|
dtslint: ^4.2.1
|
||||||
eslint: ^8.19.0
|
eslint: ^8.19.0
|
||||||
fast-deep-equal: ^3.1.3
|
fast-deep-equal: ^3.1.3
|
||||||
|
|||||||
Reference in New Issue
Block a user