Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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>({
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/voice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 12 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2855,7 +2855,7 @@ __metadata:
"@sapphire/shapeshift": ^3.4.1
"@types/node": ^18.0.3
c8: ^7.11.3
discord-api-types: ^0.33.5
discord-api-types: ^0.36.1
eslint: ^8.19.0
fast-deep-equal: ^3.1.3
prettier: ^2.7.1
Expand Down Expand Up @@ -3001,7 +3001,7 @@ __metadata:
"@sapphire/async-queue": ^1.3.2
"@sapphire/snowflake": ^3.2.2
c8: ^7.11.3
discord-api-types: ^0.33.5
discord-api-types: ^0.36.1
eslint: ^8.19.0
prettier: ^2.7.1
tslib: ^2.4.0
Expand Down Expand Up @@ -3041,7 +3041,7 @@ __metadata:
"@microsoft/api-extractor": ^7.28.3
"@types/jest": ^28.1.4
"@types/ws": ^8.5.3
discord-api-types: ^0.33.5
discord-api-types: ^0.36.1
eslint: ^8.19.0
jest: ^28.1.2
jest-websocket-mock: ^2.3.0
Expand Down Expand Up @@ -7886,13 +7886,20 @@ __metadata:
languageName: node
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
resolution: "discord-api-types@npm:0.33.5"
checksum: 6dcaad640c5693a69c9a4f5e444e739dde11ba835164ae6fd3dd5a1ab7b4d7f96cd022ed653eeaff2c8051ead0d998a5d502a2915cfacdde596364b82d9e3b3f
languageName: node
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":
version: 0.0.0-use.local
resolution: "discord.js@workspace:packages/discord.js"
Expand All @@ -7905,7 +7912,7 @@ __metadata:
"@sapphire/snowflake": ^3.2.2
"@types/node": ^18.0.3
"@types/ws": ^8.5.3
discord-api-types: ^0.33.5
discord-api-types: ^0.36.1
dtslint: ^4.2.1
eslint: ^8.19.0
fast-deep-equal: ^3.1.3
Expand Down