Skip to content

Commit 992be05

Browse files
committed
Extract subcommand field ids to constants
1 parent 40728f5 commit 992be05

File tree

8 files changed

+69
-48
lines changed

8 files changed

+69
-48
lines changed

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AddFieldFormSubcommand.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
*/
3131
public 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
}

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/AttachFormSubcommand.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
*/
4343
public class AttachFormSubcommand extends FormSubcommand implements AutoCompletable {
4444

45+
private static final String FORM_BUTTON_STYLE_FIELD = "button-style";
46+
private static final String FORM_BUTTON_LABEL_FIELD = "button-label";
4547
private final FormsRepository formsRepo;
4648

4749
/**
@@ -55,12 +57,14 @@ public AttachFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
5557
this.formsRepo = formsRepo;
5658
setCommandData(new SubcommandData("attach", "Add a button for bringing up the form to a message").addOptions(
5759
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "ID of the form to attach", true, true),
58-
new OptionData(OptionType.STRING, "message-id", "ID of the message to attach the form to", true),
59-
new OptionData(OptionType.CHANNEL, "channel",
60+
new OptionData(OptionType.STRING, FORM_MESSAGE_ID_FIELD, "ID of the message to attach the form to",
61+
true),
62+
new OptionData(OptionType.CHANNEL, FORM_CHANNEL_FIELD,
6063
"Channel of the message. Required if the message is in a different channel"),
61-
new OptionData(OptionType.STRING, "button-label", "Label of the submit button. Default is \"Submit\""),
62-
new OptionData(OptionType.STRING, "button-style", "Submit button style. Defaults to primary", false,
63-
true)));
64+
new OptionData(OptionType.STRING, FORM_BUTTON_LABEL_FIELD,
65+
"Label of the submit button. Default is \"Submit\""),
66+
new OptionData(OptionType.STRING, FORM_BUTTON_STYLE_FIELD, "Submit button style. Defaults to primary",
67+
false, true)));
6468
}
6569

6670
@Override
@@ -87,8 +91,8 @@ public void execute(SlashCommandInteractionEvent event) {
8791
return;
8892
}
8993

90-
String messageId = event.getOption("message-id", OptionMapping::getAsString);
91-
GuildChannel channel = event.getOption("channel", event.getChannel().asGuildMessageChannel(),
94+
String messageId = event.getOption(FORM_MESSAGE_ID_FIELD, OptionMapping::getAsString);
95+
GuildChannel channel = event.getOption(FORM_CHANNEL_FIELD, event.getChannel().asGuildMessageChannel(),
9296
OptionMapping::getAsChannel);
9397

9498
if (channel == null) {
@@ -101,9 +105,9 @@ public void execute(SlashCommandInteractionEvent event) {
101105
return;
102106
}
103107

104-
String buttonLabel = event.getOption("button-label", "Submit", OptionMapping::getAsString);
105-
net.dv8tion.jda.api.components.buttons.ButtonStyle style = event.getOption("button-style", ButtonStyle.PRIMARY,
106-
t -> {
108+
String buttonLabel = event.getOption(FORM_BUTTON_LABEL_FIELD, "Submit", OptionMapping::getAsString);
109+
net.dv8tion.jda.api.components.buttons.ButtonStyle style = event.getOption(FORM_BUTTON_STYLE_FIELD,
110+
ButtonStyle.PRIMARY, t -> {
107111
try {
108112
return ButtonStyle.valueOf(t.getAsString().toUpperCase());
109113
} catch (IllegalArgumentException e) {
@@ -122,7 +126,7 @@ public void execute(SlashCommandInteractionEvent event) {
122126

123127
@Override
124128
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
125-
if (!handleFormIDAutocomplete(event, target) && "button-style".equals(target.getName())) {
129+
if (!handleFormIDAutocomplete(event, target) && FORM_BUTTON_STYLE_FIELD.equals(target.getName())) {
126130
event.replyChoices(AutoCompleteUtils.filterChoices(event,
127131
Set.of(ButtonStyle.DANGER, ButtonStyle.PRIMARY, ButtonStyle.SECONDARY, ButtonStyle.SUCCESS).stream()
128132
.map(style -> new Choice(style.name(), style.name())).toList()))

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/CreateFormSubcommand.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public CreateFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
3636
super(botConfig, formsRepo);
3737
this.formsRepo = formsRepo;
3838
setCommandData(new SubcommandData("create", "Create a new form").addOptions(
39-
new OptionData(OptionType.STRING, "title", "Form title (shown in modal)", true),
40-
new OptionData(OptionType.CHANNEL, "submit-channel", "Channel to log form submissions in", true),
41-
new OptionData(OptionType.STRING, "submit-message",
39+
new OptionData(OptionType.STRING, FORM_TITLE_FIELD, "Form title (shown in modal)", true),
40+
new OptionData(OptionType.CHANNEL, FORM_SUBMIT_CHANNEL_FIELD, "Channel to log form submissions in",
41+
true),
42+
new OptionData(OptionType.STRING, FORM_SUBMIT_MESSAGE_FIELD,
4243
"Message displayed to the user once they submit the form"),
43-
new OptionData(OptionType.STRING, "expiration",
44+
new OptionData(OptionType.STRING, FORM_EXPIRATION_FIELD,
4445
"UTC time after which the form will not accept further submissions. "
4546
+ FormInteractionManager.DATE_FORMAT_STRING),
46-
new OptionData(OptionType.BOOLEAN, "onetime",
47+
new OptionData(OptionType.BOOLEAN, FORM_ONETIME_FIELD,
4748
"If the form should only accept one submission per user. Defaults to false.")));
4849
}
4950

@@ -61,10 +62,10 @@ public void execute(SlashCommandInteractionEvent event) {
6162

6263
Instant expiration = expirationOpt.orElse(null);
6364

64-
FormData form = new FormData(0, List.of(), event.getOption("title", OptionMapping::getAsString),
65-
event.getOption("submit-channel", OptionMapping::getAsChannel).getIdLong(),
66-
event.getOption("submit-message", null, OptionMapping::getAsString), null, null, expiration, false,
67-
event.getOption("onetime", false, OptionMapping::getAsBoolean));
65+
FormData form = new FormData(0, List.of(), event.getOption(FORM_TITLE_FIELD, OptionMapping::getAsString),
66+
event.getOption(FORM_SUBMIT_CHANNEL_FIELD, OptionMapping::getAsChannel).getIdLong(),
67+
event.getOption(FORM_SUBMIT_MESSAGE_FIELD, null, OptionMapping::getAsString), null, null, expiration,
68+
false, event.getOption(FORM_ONETIME_FIELD, false, OptionMapping::getAsBoolean));
6869

6970
formsRepo.insertForm(form);
7071
event.getHook()

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/FormCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class FormCommand extends SlashCommand {
2929
* @param submissionsDeleteSub form submissions-delete subcommand
3030
*
3131
*/
32-
public FormCommand(CreateFormSubcommand createSub, DeleteFormSubcommand deleteSub, CloseFormSubcommand closeSub,
32+
public FormCommand(FormSubcommand createSub, DeleteFormSubcommand deleteSub, CloseFormSubcommand closeSub,
3333
ReopenFormSubcommand reopenSub, DetailsFormSubcommand detailsSub, ModifyFormSubcommand modifySub,
34-
AddFieldFormSubcommand addFieldSub, RemoveFieldFormSubcommand removeFieldSub, ShowFormSubcommand showSub,
35-
AttachFormSubcommand attachSub, DetachFormSubcommand detachSub,
34+
FormSubcommand addFieldSub, RemoveFieldFormSubcommand removeFieldSub, ShowFormSubcommand showSub,
35+
FormSubcommand attachSub, DetachFormSubcommand detachSub,
3636
SubmissionsExportFormSubcommand submissionsGetSub, SubmissionsDeleteFormSubcommand submissionsDeleteSub) {
3737
setCommandData(Commands.slash("form", "Commands for managing modal forms")
3838
.setContexts(InteractionContextType.GUILD).setDefaultPermissions(DefaultMemberPermissions.DISABLED));

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/FormSubcommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public abstract class FormSubcommand extends Subcommand {
1919
* Form ID field identificator used in form subcommands.
2020
*/
2121
protected static final String FORM_ID_FIELD = "form-id";
22+
protected static final String FORM_CHANNEL_FIELD = "channel";
23+
protected static final String FORM_MESSAGE_ID_FIELD = "message-id";
24+
protected static final String FORM_EXPIRATION_FIELD = "expiration";
25+
protected static final String FORM_ONETIME_FIELD = "onetime";
26+
protected static final String FORM_SUBMIT_MESSAGE_FIELD = "submit-message";
27+
protected static final String FORM_SUBMIT_CHANNEL_FIELD = "submit-channel";
28+
protected static final String FORM_TITLE_FIELD = "title";
2229
private final BotConfig botConfig;
2330
private final FormsRepository formsRepository;
2431

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/ModifyFormSubcommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public ModifyFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
3939
this.formsRepo = formsRepo;
4040
setCommandData(new SubcommandData("modify", "Modify an existing form").addOptions(
4141
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "ID of the form to modify", true, true),
42-
new OptionData(OptionType.STRING, "title", "Form title (shown in modal)"),
43-
new OptionData(OptionType.CHANNEL, "submit-channel", "Channel to log form submissions in"),
44-
new OptionData(OptionType.STRING, "submit-message",
42+
new OptionData(OptionType.STRING, FORM_TITLE_FIELD, "Form title (shown in modal)"),
43+
new OptionData(OptionType.CHANNEL, FORM_SUBMIT_CHANNEL_FIELD, "Channel to log form submissions in"),
44+
new OptionData(OptionType.STRING, FORM_SUBMIT_MESSAGE_FIELD,
4545
"Message displayed to the user once they submit the form"),
46-
new OptionData(OptionType.STRING, "expiration",
46+
new OptionData(OptionType.STRING, FORM_EXPIRATION_FIELD,
4747
"UTC time after which the form stops accepting submissions. - for no expiration. "
4848
+ FormInteractionManager.DATE_FORMAT_STRING),
49-
new OptionData(OptionType.BOOLEAN, "onetime",
49+
new OptionData(OptionType.BOOLEAN, FORM_ONETIME_FIELD,
5050
"If the form should only accept one submission per user. Defaults to false.")));
5151
}
5252

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/RemoveFieldFormSubcommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
public class RemoveFieldFormSubcommand extends FormSubcommand implements AutoCompletable {
2828

29+
private static final String FORM_FIELD_INDEX_FIELD = "field";
2930
private final FormsRepository formsRepo;
3031

3132
/**
@@ -39,15 +40,15 @@ public RemoveFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig)
3940
this.formsRepo = formsRepo;
4041
setCommandData(new SubcommandData("remove-field", "Removse a field from an existing form")
4142
.addOption(OptionType.INTEGER, FORM_ID_FIELD, "Form ID to add the field to", true, true)
42-
.addOption(OptionType.INTEGER, "field", "0-indexed # of the field to remove", true, true));
43+
.addOption(OptionType.INTEGER, FORM_FIELD_INDEX_FIELD, "0-indexed # of the field to remove", true, true));
4344
}
4445

4546
@Override
4647
public void execute(SlashCommandInteractionEvent event) {
4748
if (!checkForStaffRole(event)) return;
4849
event.deferReply(true).queue();
4950
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
50-
int index = event.getOption("field", OptionMapping::getAsInt);
51+
int index = event.getOption(FORM_FIELD_INDEX_FIELD, OptionMapping::getAsInt);
5152
if (formOpt.isEmpty()) {
5253
event.getHook().sendMessage("A form with this ID was not found.").queue();
5354
return;
@@ -71,7 +72,7 @@ public void execute(SlashCommandInteractionEvent event) {
7172

7273
@Override
7374
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
74-
if (!handleFormIDAutocomplete(event, target) && "field".equals(target.getName())) {
75+
if (!handleFormIDAutocomplete(event, target) && FORM_FIELD_INDEX_FIELD.equals(target.getName())) {
7576
Long formId = event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong);
7677
if (formId != null) {
7778
Optional<FormData> form = formsRepo.getForm(formId);

src/main/java/net/discordjug/javabot/systems/staff_commands/forms/commands/SubmissionsDeleteFormSubcommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
public class SubmissionsDeleteFormSubcommand extends FormSubcommand implements AutoCompletable {
2626

27+
private static final String FORM_USER_FIELD = "user";
2728
private final FormsRepository formsRepo;
2829

2930
/**
@@ -37,7 +38,7 @@ public SubmissionsDeleteFormSubcommand(FormsRepository formsRepo, BotConfig botC
3738
this.formsRepo = formsRepo;
3839
setCommandData(new SubcommandData("submissions-delete", "Deletes submissions of a user in the form").addOptions(
3940
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "The ID of a form to delete submissions from", true, true),
40-
new OptionData(OptionType.USER, "user", "User to delete submissions of", true)));
41+
new OptionData(OptionType.USER, FORM_USER_FIELD, "User to delete submissions of", true)));
4142
}
4243

4344
@Override
@@ -50,7 +51,7 @@ public void execute(SlashCommandInteractionEvent event) {
5051
return;
5152
}
5253

53-
User user = event.getOption("user", OptionMapping::getAsUser);
54+
User user = event.getOption(FORM_USER_FIELD, OptionMapping::getAsUser);
5455
FormData form = formOpt.get();
5556

5657
int count = formsRepo.deleteSubmissions(form, user);

0 commit comments

Comments
 (0)