3030 */
3131public class AddFieldFormSubcommand extends FormSubcommand implements AutoCompletable {
3232
33+ private static final String FORM_VALUE_FIELD = "value" ;
34+ private static final String FORM_STYLE_FIELD = "style" ;
35+ private static final String FORM_REQUIRED_FIELD = "required" ;
36+ private static final String FORM_PLACEHOLDER_FIELD = "placeholder" ;
37+ private static final String FORM_MAX_FIELD = "max" ;
38+ private static final String FORM_MIN_FIELD = "min" ;
39+ private static final String FORM_LABEL_FIELD = "label" ;
3340 private final FormsRepository formsRepo ;
3441
3542 /**
@@ -43,14 +50,14 @@ public AddFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
4350 this .formsRepo = formsRepo ;
4451 setCommandData (new SubcommandData ("add-field" , "Adds a field to an existing form" )
4552 .addOption (OptionType .INTEGER , FORM_ID_FIELD , "Form ID to add the field to" , true , true )
46- .addOption (OptionType .STRING , "label" , "Field label" , true )
47- .addOption (OptionType .INTEGER , "min" , "Minimum number of characters" )
48- .addOption (OptionType .INTEGER , "max" , "Maximum number of characters" )
49- .addOption (OptionType .STRING , "placeholder" , "Field placeholder" )
50- .addOption (OptionType .BOOLEAN , "required" ,
53+ .addOption (OptionType .STRING , FORM_LABEL_FIELD , "Field label" , true )
54+ .addOption (OptionType .INTEGER , FORM_MIN_FIELD , "Minimum number of characters" )
55+ .addOption (OptionType .INTEGER , FORM_MAX_FIELD , "Maximum number of characters" )
56+ .addOption (OptionType .STRING , FORM_PLACEHOLDER_FIELD , "Field placeholder" )
57+ .addOption (OptionType .BOOLEAN , FORM_REQUIRED_FIELD ,
5158 "Whether or not the user has to input data in this field. Default: false" )
52- .addOption (OptionType .STRING , "style" , "Input style. Default: SHORT" , false , true )
53- .addOption (OptionType .STRING , "value" , "Initial field value" ));
59+ .addOption (OptionType .STRING , FORM_STYLE_FIELD , "Input style. Default: SHORT" , false , true )
60+ .addOption (OptionType .STRING , FORM_VALUE_FIELD , "Initial field value" ));
5461 }
5562
5663 @ Override
@@ -75,7 +82,7 @@ public void execute(SlashCommandInteractionEvent event) {
7582
7683 @ Override
7784 public void handleAutoComplete (CommandAutoCompleteInteractionEvent event , AutoCompleteQuery target ) {
78- if (!handleFormIDAutocomplete (event , target ) && "style" .equals (target .getName ())) {
85+ if (!handleFormIDAutocomplete (event , target ) && FORM_STYLE_FIELD .equals (target .getName ())) {
7986 event .replyChoices (AutoCompleteUtils .filterChoices (event ,
8087 Arrays .stream (TextInputStyle .values ()).filter (t -> t != TextInputStyle .UNKNOWN )
8188 .map (style -> new Choice (style .name (), style .name ())).toList ()))
@@ -84,19 +91,19 @@ public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCo
8491 }
8592
8693 private static FormField createFormFieldFromEvent (SlashCommandInteractionEvent e ) {
87- String label = e .getOption ("label" , OptionMapping ::getAsString );
88- int min = e .getOption ("min" , 0 , OptionMapping ::getAsInt );
89- int max = e .getOption ("max" , 64 , OptionMapping ::getAsInt );
90- String placeholder = e .getOption ("placeholder" , OptionMapping ::getAsString );
91- boolean required = e .getOption ("required" , false , OptionMapping ::getAsBoolean );
92- TextInputStyle style = e .getOption ("style" , TextInputStyle .SHORT , t -> {
94+ String label = e .getOption (FORM_LABEL_FIELD , OptionMapping ::getAsString );
95+ int min = e .getOption (FORM_MIN_FIELD , 0 , OptionMapping ::getAsInt );
96+ int max = e .getOption (FORM_MAX_FIELD , 64 , OptionMapping ::getAsInt );
97+ String placeholder = e .getOption (FORM_PLACEHOLDER_FIELD , OptionMapping ::getAsString );
98+ boolean required = e .getOption (FORM_REQUIRED_FIELD , false , OptionMapping ::getAsBoolean );
99+ TextInputStyle style = e .getOption (FORM_STYLE_FIELD , TextInputStyle .SHORT , t -> {
93100 try {
94101 return TextInputStyle .valueOf (t .getAsString ().toUpperCase ());
95102 } catch (IllegalArgumentException e2 ) {
96103 return TextInputStyle .SHORT ;
97104 }
98105 });
99- String value = e .getOption ("value" , OptionMapping ::getAsString );
106+ String value = e .getOption (FORM_VALUE_FIELD , OptionMapping ::getAsString );
100107
101108 return new FormField (label , max , min , placeholder , required , style , value , 0 );
102109 }
0 commit comments