1
1
import {
2
+ ApplicationCommandOptionBase ,
3
+ ApplicationCommandOptionWithChoicesAndAutocompleteMixin ,
2
4
SlashCommandBuilder ,
5
+ SlashCommandIntegerOption ,
6
+ SlashCommandNumberOption ,
7
+ SlashCommandStringOption ,
3
8
SlashCommandSubcommandBuilder ,
4
9
SlashCommandSubcommandsOnlyBuilder ,
5
- ApplicationCommandOptionWithChoicesAndAutocompleteMixin ,
6
- ApplicationCommandOptionBase ,
7
10
} from '@discordjs/builders' ;
8
11
import {
9
12
ApplicationCommandRegistries ,
10
- Args ,
11
13
ArgType ,
14
+ Args ,
12
15
ChatInputCommand ,
13
- Command as SapphireCommand ,
14
- container ,
15
16
RegisterBehavior ,
16
17
SapphireClient ,
18
+ Command as SapphireCommand ,
19
+ container ,
17
20
} from '@sapphire/framework' ;
18
- import { APIMessage , APIApplicationCommandOptionChoice } from 'discord-api-types/v9' ;
21
+ import { APIApplicationCommandOptionChoice , APIMessage } from 'discord-api-types/v9' ;
19
22
import {
20
23
ApplicationCommandOptionType ,
21
24
BaseMessageOptions ,
@@ -24,8 +27,8 @@ import {
24
27
MessagePayload ,
25
28
User ,
26
29
} from 'discord.js' ;
27
- import { logger } from './logger/default' ;
28
30
import { CodeyUserError } from './codeyUserError' ;
31
+ import { logger } from './logger/default' ;
29
32
30
33
export type SapphireSentMessageType = Message | CommandInteraction ;
31
34
export type SapphireMessageResponse =
@@ -85,6 +88,13 @@ export interface CodeyCommandOption {
85
88
required : boolean ;
86
89
/** Mention choices for the field if needed */
87
90
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
+ } ;
88
98
}
89
99
90
100
/** Sets the command option in the slash command builder */
@@ -93,10 +103,30 @@ const setCommandOption = (
93
103
option : CodeyCommandOption ,
94
104
) : SlashCommandBuilder | SlashCommandSubcommandBuilder => {
95
105
function setupCommand < T extends ApplicationCommandOptionBase > ( commandOption : T ) : T {
96
- return commandOption
106
+ let result = commandOption
97
107
. setName ( option . name )
98
108
. setDescription ( option . description )
99
109
. 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 ;
100
130
}
101
131
102
132
function setupChoices <
@@ -108,6 +138,7 @@ const setCommandOption = (
108
138
? commandOption . addChoices ( ...( option . choices as APIApplicationCommandOptionChoice < B > [ ] ) )
109
139
: commandOption ;
110
140
}
141
+
111
142
switch ( option . type ) {
112
143
case CodeyCommandOptionType . STRING :
113
144
return < SlashCommandBuilder > builder . addStringOption ( ( x ) => setupCommand ( setupChoices ( x ) ) ) ;
0 commit comments