Skip to content

Commit 5e3808a

Browse files
author
hyper-neutrino
committed
add client-side validation (min/max for string and number options)
1 parent 41a2dab commit 5e3808a

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/codeyCommand.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import {
2+
ApplicationCommandOptionBase,
3+
ApplicationCommandOptionWithChoicesAndAutocompleteMixin,
24
SlashCommandBuilder,
5+
SlashCommandIntegerOption,
6+
SlashCommandNumberOption,
7+
SlashCommandStringOption,
38
SlashCommandSubcommandBuilder,
49
SlashCommandSubcommandsOnlyBuilder,
5-
ApplicationCommandOptionWithChoicesAndAutocompleteMixin,
6-
ApplicationCommandOptionBase,
710
} from '@discordjs/builders';
811
import {
912
ApplicationCommandRegistries,
10-
Args,
1113
ArgType,
14+
Args,
1215
ChatInputCommand,
13-
Command as SapphireCommand,
14-
container,
1516
RegisterBehavior,
1617
SapphireClient,
18+
Command as SapphireCommand,
19+
container,
1720
} from '@sapphire/framework';
18-
import { APIMessage, APIApplicationCommandOptionChoice } from 'discord-api-types/v9';
21+
import { APIApplicationCommandOptionChoice, APIMessage } from 'discord-api-types/v9';
1922
import {
2023
ApplicationCommandOptionType,
2124
BaseMessageOptions,
@@ -24,8 +27,8 @@ import {
2427
MessagePayload,
2528
User,
2629
} from 'discord.js';
27-
import { logger } from './logger/default';
2830
import { CodeyUserError } from './codeyUserError';
31+
import { logger } from './logger/default';
2932

3033
export type SapphireSentMessageType = Message | CommandInteraction;
3134
export type SapphireMessageResponse =
@@ -85,6 +88,13 @@ export interface CodeyCommandOption {
8588
required: boolean;
8689
/** Mention choices for the field if needed */
8790
choices?: APIApplicationCommandOptionChoice[];
91+
/** Client-side validation options */
92+
validation?: {
93+
/** Minimum length or value */
94+
min?: number;
95+
/** Maximum length or value */
96+
max?: number;
97+
};
8898
}
8999

90100
/** Sets the command option in the slash command builder */
@@ -93,10 +103,30 @@ const setCommandOption = (
93103
option: CodeyCommandOption,
94104
): SlashCommandBuilder | SlashCommandSubcommandBuilder => {
95105
function setupCommand<T extends ApplicationCommandOptionBase>(commandOption: T): T {
96-
return commandOption
106+
let result = commandOption
97107
.setName(option.name)
98108
.setDescription(option.description)
99109
.setRequired(option.required);
110+
111+
if (result instanceof SlashCommandStringOption && option.validation?.min !== undefined)
112+
result = result.setMinLength(option.validation.min);
113+
114+
if (result instanceof SlashCommandStringOption && option.validation?.max !== undefined)
115+
result = result.setMaxLength(option.validation.max);
116+
117+
if (
118+
(result instanceof SlashCommandNumberOption || result instanceof SlashCommandIntegerOption) &&
119+
option.validation?.min !== undefined
120+
)
121+
result = result.setMinValue(option.validation.min);
122+
123+
if (
124+
(result instanceof SlashCommandNumberOption || result instanceof SlashCommandIntegerOption) &&
125+
option.validation?.max !== undefined
126+
)
127+
result = result.setMaxValue(option.validation.max);
128+
129+
return result;
100130
}
101131

102132
function setupChoices<
@@ -108,6 +138,7 @@ const setCommandOption = (
108138
? commandOption.addChoices(...(option.choices as APIApplicationCommandOptionChoice<B>[]))
109139
: commandOption;
110140
}
141+
111142
switch (option.type) {
112143
case CodeyCommandOptionType.STRING:
113144
return <SlashCommandBuilder>builder.addStringOption((x) => setupCommand(setupChoices(x)));

0 commit comments

Comments
 (0)