Skip to content

Commit 7c2b156

Browse files
committed
Extract some of the common form logic to FormSubcommand
1 parent 2a78356 commit 7c2b156

14 files changed

+90
-99
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class AddFieldFormSubcommand extends FormSubcommand implements AutoComple
3838
* @param botConfig bot configuration
3939
*/
4040
public AddFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
41-
super(botConfig);
41+
super(botConfig, formsRepo);
4242
this.formsRepo = formsRepo;
4343
setCommandData(new SubcommandData("add-field", "Adds a field to an existing form")
44-
.addOption(OptionType.INTEGER, "form-id", "Form ID to add the field to", true, true)
44+
.addOption(OptionType.INTEGER, FORM_ID_FIELD, "Form ID to add the field to", true, true)
4545
.addOption(OptionType.STRING, "label", "Field label", true)
4646
.addOption(OptionType.INTEGER, "min", "Minimum number of characters")
4747
.addOption(OptionType.INTEGER, "max", "Maximum number of characters")
@@ -56,7 +56,7 @@ public AddFieldFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
5656
public void execute(SlashCommandInteractionEvent event) {
5757
if (!checkForStaffRole(event)) return;
5858
event.deferReply(true).queue();
59-
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
59+
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
6060
if (formOpt.isEmpty()) {
6161
event.getHook().sendMessage("A form with this ID was not found.").queue();
6262
return;
@@ -74,14 +74,9 @@ public void execute(SlashCommandInteractionEvent event) {
7474

7575
@Override
7676
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
77-
switch (target.getName()) {
78-
case "form-id" -> event.replyChoices(
79-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList())
80-
.queue();
81-
case "style" ->
82-
event.replyChoices(Arrays.stream(TextInputStyle.values()).filter(t -> t != TextInputStyle.UNKNOWN)
83-
.map(style -> new Choice(style.name(), style.name())).toList()).queue();
84-
default -> {}
77+
if (!handleFormIDAutocomplete(event, target) && "style".equals(target.getName())) {
78+
event.replyChoices(Arrays.stream(TextInputStyle.values()).filter(t -> t != TextInputStyle.UNKNOWN)
79+
.map(style -> new Choice(style.name(), style.name())).toList()).queue();
8580
}
8681
}
8782

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public class AttachFormSubcommand extends FormSubcommand implements AutoCompleta
5050
* @param botConfig bot configuration
5151
*/
5252
public AttachFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
53-
super(botConfig);
53+
super(botConfig, formsRepo);
5454
this.formsRepo = formsRepo;
5555
setCommandData(new SubcommandData("attach", "Attach a form to a message").addOptions(
56-
new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true),
56+
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "ID of the form to attach", true, true),
5757
new OptionData(OptionType.STRING, "message-id", "ID of the message to attach the form to", true),
5858
new OptionData(OptionType.CHANNEL, "channel",
5959
"Channel of the message. Required if the message is in a different channel"),
@@ -67,7 +67,7 @@ public void execute(SlashCommandInteractionEvent event) {
6767
if (!checkForStaffRole(event)) return;
6868
event.deferReply().setEphemeral(true).queue();
6969

70-
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
70+
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
7171
if (formOpt.isEmpty()) {
7272
event.getHook().sendMessage("A form with this ID was not found.").queue();
7373
return;
@@ -121,15 +121,11 @@ public void execute(SlashCommandInteractionEvent event) {
121121

122122
@Override
123123
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
124-
switch (target.getName()) {
125-
case "form-id" -> event.replyChoices(
126-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList())
127-
.queue();
128-
case "button-style" -> event.replyChoices(
124+
if (!handleFormIDAutocomplete(event, target) && "button-style".equals(target.getName())) {
125+
event.replyChoices(
129126
Set.of(ButtonStyle.DANGER, ButtonStyle.PRIMARY, ButtonStyle.SECONDARY, ButtonStyle.SUCCESS).stream()
130127
.map(style -> new Choice(style.name(), style.name())).toList())
131128
.queue();
132-
default -> {}
133129
}
134130
}
135131

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
1010
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1111
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
12-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
1312
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1413
import net.dv8tion.jda.api.interactions.commands.OptionType;
1514
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@@ -37,17 +36,17 @@ public class CloseFormSubcommand extends FormSubcommand implements AutoCompletab
3736
*/
3837
public CloseFormSubcommand(FormsRepository formsRepo, FormInteractionManager interactionManager,
3938
BotConfig botConfig) {
40-
super(botConfig);
39+
super(botConfig, formsRepo);
4140
this.formsRepo = formsRepo;
4241
this.interactionManager = interactionManager;
4342
setCommandData(new SubcommandData("close", "Close an existing form")
44-
.addOptions(new OptionData(OptionType.INTEGER, "form-id", "The ID of a form to close", true, true)));
43+
.addOptions(new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "The ID of a form to close", true, true)));
4544
}
4645

4746
@Override
4847
public void execute(SlashCommandInteractionEvent event) {
4948
if (!checkForStaffRole(event)) return;
50-
long id = event.getOption("form-id", OptionMapping::getAsLong);
49+
long id = event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong);
5150
Optional<FormData> formOpt = formsRepo.getForm(id);
5251
if (formOpt.isEmpty()) {
5352
event.reply("A form with this ID was not found.").setEphemeral(true).queue();
@@ -69,8 +68,6 @@ public void execute(SlashCommandInteractionEvent event) {
6968

7069
@Override
7170
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
72-
event.replyChoices(
73-
formsRepo.getAllForms(false).stream().map(form -> new Choice(form.toString(), form.id())).toList())
74-
.queue();
71+
handleFormIDAutocomplete(event, target);
7572
}
7673
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class CreateFormSubcommand extends FormSubcommand {
3333
* @param botConfig bot configuration
3434
*/
3535
public CreateFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
36-
super(botConfig);
36+
super(botConfig, formsRepo);
3737
this.formsRepo = formsRepo;
3838
setCommandData(new SubcommandData("create", "Create a new form").addOptions(
3939
new OptionData(OptionType.STRING, "title", "Form title (shown in modal)", true),

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
99
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1010
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
11-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
1211
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1312
import net.dv8tion.jda.api.interactions.commands.OptionType;
1413
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@@ -35,16 +34,16 @@ public class DeleteFormSubcommand extends FormSubcommand implements AutoCompleta
3534
* @param botConfig bot configuration
3635
*/
3736
public DeleteFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
38-
super(botConfig);
37+
super(botConfig, formsRepo);
3938
this.formsRepo = formsRepo;
4039
setCommandData(new SubcommandData("delete", "Delete an existing form")
41-
.addOptions(new OptionData(OptionType.INTEGER, "form-id", "The ID of a form to delete", true, true)));
40+
.addOptions(new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "The ID of a form to delete", true, true)));
4241
}
4342

4443
@Override
4544
public void execute(SlashCommandInteractionEvent event) {
4645
if (!checkForStaffRole(event)) return;
47-
long id = event.getOption("form-id", OptionMapping::getAsLong);
46+
long id = event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong);
4847
Optional<FormData> formOpt = formsRepo.getForm(id);
4948
if (formOpt.isEmpty()) {
5049
event.reply("A form with this ID was not found.").setEphemeral(true).queue();
@@ -66,7 +65,6 @@ public void execute(SlashCommandInteractionEvent event) {
6665

6766
@Override
6867
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
69-
event.replyChoices(
70-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()).queue();
68+
handleFormIDAutocomplete(event, target);
7169
}
7270
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
1717
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1818
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
19-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
2019
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
2120
import net.dv8tion.jda.api.interactions.commands.OptionType;
2221
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@@ -43,18 +42,18 @@ public class DetachFormSubcommand extends FormSubcommand implements AutoCompleta
4342
* @param botConfig bot configuration
4443
*/
4544
public DetachFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
46-
super(botConfig);
45+
super(botConfig, formsRepo);
4746
this.formsRepo = formsRepo;
4847
setCommandData(new SubcommandData("detach", "Detach a form from a message")
49-
.addOptions(new OptionData(OptionType.INTEGER, "form-id", "ID of the form to attach", true, true)));
48+
.addOptions(new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "ID of the form to attach", true, true)));
5049
}
5150

5251
@Override
5352
public void execute(SlashCommandInteractionEvent event) {
5453
if (!checkForStaffRole(event)) return;
5554
event.deferReply().setEphemeral(true).queue();
5655

57-
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
56+
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
5857
if (formOpt.isEmpty()) {
5958
event.getHook().sendMessage("A form with this ID was not found.").queue();
6059
return;
@@ -74,8 +73,7 @@ public void execute(SlashCommandInteractionEvent event) {
7473

7574
@Override
7675
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
77-
event.replyChoices(
78-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()).queue();
76+
handleFormIDAutocomplete(event, target);
7977
}
8078

8179
/**

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
1212
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1313
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
14-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
1514
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1615
import net.dv8tion.jda.api.interactions.commands.OptionType;
1716
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@@ -38,17 +37,17 @@ public class DetailsFormSubcommand extends FormSubcommand implements AutoComplet
3837
* @param botConfig bot configuration
3938
*/
4039
public DetailsFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
41-
super(botConfig);
40+
super(botConfig, formsRepo);
4241
this.formsRepo = formsRepo;
4342
setCommandData(new SubcommandData("details", "Get details about a form").addOptions(
44-
new OptionData(OptionType.INTEGER, "form-id", "The ID of a form to get details for", true, true)));
43+
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "The ID of a form to get details for", true, true)));
4544
}
4645

4746
@Override
4847
public void execute(SlashCommandInteractionEvent event) {
4948
if (!checkForStaffRole(event)) return;
5049
event.deferReply().setEphemeral(false).queue();
51-
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
50+
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
5251
if (formOpt.isEmpty()) {
5352
event.getHook().sendMessage("Couldn't find a form with this id").queue();
5453
return;
@@ -66,8 +65,7 @@ public void execute(SlashCommandInteractionEvent event) {
6665

6766
@Override
6867
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
69-
event.replyChoices(
70-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()).queue();
68+
handleFormIDAutocomplete(event, target);
7169
}
7270

7371
private EmbedBuilder createFormDetailsEmbed(FormData form, Guild guild) {

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package net.discordjug.javabot.systems.staff_commands.forms.commands;
22

33
import net.discordjug.javabot.data.config.BotConfig;
4+
import net.discordjug.javabot.systems.staff_commands.forms.dao.FormsRepository;
45
import net.discordjug.javabot.util.Checks;
56
import net.discordjug.javabot.util.Responses;
7+
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
8+
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
69
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
10+
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
711
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand.Subcommand;
812

913
/**
1014
* Base abstract class containing common methods used in form subcommands.
1115
*/
1216
public abstract class FormSubcommand extends Subcommand {
1317

18+
protected static final String FORM_ID_FIELD = "form-id";
1419
private final BotConfig botConfig;
20+
private final FormsRepository formsRepository;
1521

1622
/**
1723
* The main constructor.
1824
*
25+
* @param formsRepo the forms repository
1926
* @param botConfig main bot configuration
2027
*/
21-
public FormSubcommand(BotConfig botConfig) {
28+
public FormSubcommand(BotConfig botConfig, FormsRepository formsRepository) {
2229
this.botConfig = botConfig;
30+
this.formsRepository = formsRepository;
2331
}
2432

2533
/**
@@ -36,4 +44,23 @@ protected boolean checkForStaffRole(IReplyCallback event) {
3644
}
3745
return true;
3846
}
47+
48+
/**
49+
* Tries to handle the auto completion event initiated by a user. If current
50+
* focused field's id is equal to {@link #FORM_ID_FIELD}, the method will handle
51+
* the event by replying with a list of all available form IDs,
52+
*
53+
* @param event the event to handle
54+
* @param target auto completion target
55+
* @return true if the event was handled by this method
56+
*/
57+
protected boolean handleFormIDAutocomplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
58+
if (FORM_ID_FIELD.equals(target.getName())) {
59+
event.replyChoices(
60+
formsRepository.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList())
61+
.queue();
62+
return true;
63+
}
64+
return false;
65+
}
3966
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
1111
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1212
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
13-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
1413
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1514
import net.dv8tion.jda.api.interactions.commands.OptionType;
1615
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@@ -33,10 +32,10 @@ public class ModifyFormSubcommand extends FormSubcommand implements AutoCompleta
3332
* @param botConfig bot configuration
3433
*/
3534
public ModifyFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
36-
super(botConfig);
35+
super(botConfig, formsRepo);
3736
this.formsRepo = formsRepo;
3837
setCommandData(new SubcommandData("modify", "Modify an existing form").addOptions(
39-
new OptionData(OptionType.INTEGER, "form-id", "ID of the form to modify", true, true),
38+
new OptionData(OptionType.INTEGER, FORM_ID_FIELD, "ID of the form to modify", true, true),
4039
new OptionData(OptionType.STRING, "title", "Form title (shown in modal)"),
4140
new OptionData(OptionType.CHANNEL, "submit-channel", "Channel to log form submissions in"),
4241
new OptionData(OptionType.STRING, "submit-message",
@@ -52,7 +51,7 @@ public ModifyFormSubcommand(FormsRepository formsRepo, BotConfig botConfig) {
5251
public void execute(SlashCommandInteractionEvent event) {
5352
if (!checkForStaffRole(event)) return;
5453
event.deferReply(true).queue();
55-
Optional<FormData> formOpt = formsRepo.getForm(event.getOption("form-id", OptionMapping::getAsLong));
54+
Optional<FormData> formOpt = formsRepo.getForm(event.getOption(FORM_ID_FIELD, OptionMapping::getAsLong));
5655
if (formOpt.isEmpty()) {
5756
event.getHook().sendMessage("Couldn't find a form with this ID").queue();
5857
return;
@@ -89,8 +88,7 @@ public void execute(SlashCommandInteractionEvent event) {
8988

9089
@Override
9190
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
92-
event.replyChoices(
93-
formsRepo.getAllForms().stream().map(form -> new Choice(form.toString(), form.id())).toList()).queue();
91+
handleFormIDAutocomplete(event, target);
9492
}
9593

9694
}

0 commit comments

Comments
 (0)