Skip to content

Commit ddbab97

Browse files
Merge pull request #267 from Java-Discord/dynxsty/modal_questions
Made `/qotw questions-queue add` use Modals to support multi-line text input
2 parents 0e95bcf + 8b54f6e commit ddbab97

File tree

6 files changed

+59
-33
lines changed

6 files changed

+59
-33
lines changed

src/main/java/net/javadiscord/javabot/listener/InteractionListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.javadiscord.javabot.command.Responses;
99
import net.javadiscord.javabot.systems.help.HelpChannelInteractionManager;
1010
import net.javadiscord.javabot.systems.moderation.ReportCommand;
11+
import net.javadiscord.javabot.systems.qotw.subcommands.questions_queue.AddQuestionSubcommand;
1112
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionInteractionManager;
1213
import net.javadiscord.javabot.systems.staff.self_roles.SelfRoleInteractionManager;
1314
import net.javadiscord.javabot.util.InteractionUtils;
@@ -31,6 +32,7 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event) {
3132
switch (id[0]) {
3233
case "self-role" -> SelfRoleInteractionManager.handleModalSubmit(event, id);
3334
case "report" -> new ReportCommand().handleModalSubmit(event, id);
35+
case "qotw-add-question" -> AddQuestionSubcommand.handleModalSubmit(event).queue();
3436
default -> Responses.error(event.getHook(), "Unknown Interaction").queue();
3537
}
3638
}

src/main/java/net/javadiscord/javabot/systems/qotw/subcommands/QOTWSubcommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.javadiscord.javabot.systems.qotw.subcommands;
22

33
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
4-
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
4+
import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction;
55
import net.javadiscord.javabot.Bot;
66
import net.javadiscord.javabot.command.Responses;
77
import net.javadiscord.javabot.command.interfaces.SlashCommand;
@@ -16,7 +16,7 @@
1616
*/
1717
public abstract class QOTWSubcommand implements SlashCommand {
1818
@Override
19-
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
19+
public InteractionCallbackAction<?> handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
2020
if (event.getGuild() == null) {
2121
return Responses.warning(event, "This command can only be used in the context of a guild.");
2222
}
@@ -31,5 +31,5 @@ public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteraction
3131
}
3232
}
3333

34-
protected abstract ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException;
34+
protected abstract InteractionCallbackAction<?> handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException;
3535
}
Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,77 @@
11
package net.javadiscord.javabot.systems.qotw.subcommands.questions_queue;
22

3+
import net.dv8tion.jda.api.entities.Message;
4+
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
35
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
4-
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
5-
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
6+
import net.dv8tion.jda.api.interactions.components.ActionRow;
7+
import net.dv8tion.jda.api.interactions.components.Modal;
8+
import net.dv8tion.jda.api.interactions.components.text.TextInput;
9+
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
10+
import net.dv8tion.jda.api.interactions.modals.ModalMapping;
11+
import net.dv8tion.jda.api.requests.restaction.WebhookMessageAction;
12+
import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction;
613
import net.javadiscord.javabot.command.Responses;
14+
import net.javadiscord.javabot.data.h2db.DbHelper;
715
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
816
import net.javadiscord.javabot.systems.qotw.model.QOTWQuestion;
917
import net.javadiscord.javabot.systems.qotw.subcommands.QOTWSubcommand;
1018

1119
import java.sql.Connection;
12-
import java.sql.SQLException;
1320

1421
/**
1522
* Subcommand that allows staff-members to add question to the QOTW-Queue.
1623
*/
1724
public class AddQuestionSubcommand extends QOTWSubcommand {
1825
@Override
19-
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
26+
protected InteractionCallbackAction<?> handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) {
27+
return event.replyModal(this.buildQuestionModal());
28+
}
29+
30+
private Modal buildQuestionModal() {
31+
TextInput priorityField = TextInput.create("priority", "Priority (Leave blank for default)", TextInputStyle.SHORT)
32+
.setRequired(false)
33+
.setValue("0")
34+
.build();
35+
36+
TextInput questionField = TextInput.create("question", "Question Text", TextInputStyle.PARAGRAPH)
37+
.setMaxLength(1024)
38+
.build();
39+
40+
return Modal.create("qotw-add-question", "Create QOTW Question")
41+
.addActionRows(ActionRow.of(questionField), ActionRow.of(priorityField))
42+
.build();
43+
}
44+
45+
/**
46+
* Handles the Modal, that pops up when executing <code>/qotw question-queue add</code>.
47+
*
48+
* @param event The {@link ModalInteractionEvent} that was fired upon submitting the modal.
49+
* @return A {@link WebhookMessageAction}, that needs to be queued.
50+
*/
51+
public static WebhookMessageAction<Message> handleModalSubmit(ModalInteractionEvent event) {
52+
event.deferReply(true).queue();
53+
// Create question
2054
QOTWQuestion question = new QOTWQuestion();
21-
question.setGuildId(guildId);
55+
question.setGuildId(event.getGuild().getIdLong());
2256
question.setCreatedBy(event.getUser().getIdLong());
2357
question.setPriority(0);
2458

25-
OptionMapping textOption = event.getOption("question");
26-
if (textOption == null) {
27-
return Responses.warning(event, "Missing required arguments.");
59+
ModalMapping textOption = event.getValue("question");
60+
if (textOption == null || textOption.getAsString().isEmpty()) {
61+
return Responses.warning(event.getHook(), "Invalid question text. Must not be blank, and must be less than 1024 characters.");
2862
}
63+
question.setText(textOption.getAsString());
2964

30-
String text = textOption.getAsString();
31-
if (text.isBlank() || text.length() > 1024) {
32-
return Responses.warning(event, "Invalid question text. Must not be blank, and must be less than 1024 characters.");
65+
ModalMapping priorityOption = event.getValue("priority");
66+
if (priorityOption == null || !priorityOption.getAsString().matches("\\d+")) {
67+
return Responses.error(event.getHook(), "Invalid priority value. Must be a numeric value.");
3368
}
34-
question.setText(text);
3569

36-
OptionMapping priorityOption = event.getOption("priority");
37-
if (priorityOption != null) {
38-
question.setPriority((int) priorityOption.getAsLong());
70+
if (!priorityOption.getAsString().isEmpty()) {
71+
question.setPriority(Integer.parseInt(priorityOption.getAsString()));
3972
}
4073

41-
new QuestionQueueRepository(con).save(question);
42-
return Responses.success(event, "Question Added", "Your question has been added to the queue. Its id is `" + question.getId() + "`.");
74+
DbHelper.doDaoAction(QuestionQueueRepository::new, dao -> dao.save(question));
75+
return Responses.success(event.getHook(), "Question Added", "Your question has been added to the queue.");
4376
}
4477
}

src/main/java/net/javadiscord/javabot/systems/qotw/subcommands/questions_queue/ListQuestionsSubcommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
6-
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
6+
import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction;
77
import net.javadiscord.javabot.Bot;
88
import net.javadiscord.javabot.command.Responses;
99
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
@@ -19,7 +19,7 @@
1919
*/
2020
public class ListQuestionsSubcommand extends QOTWSubcommand {
2121
@Override
22-
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
22+
protected InteractionCallbackAction<?> handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
2323
var repository = new QuestionQueueRepository(con);
2424
OptionMapping pageOption = event.getOption("page");
2525
int page = 0;

src/main/java/net/javadiscord/javabot/systems/qotw/subcommands/questions_queue/RemoveQuestionSubcommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.interactions.commands.Command;
66
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
7-
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
7+
import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction;
88
import net.javadiscord.javabot.Bot;
99
import net.javadiscord.javabot.command.Responses;
1010
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
@@ -21,7 +21,7 @@
2121
*/
2222
public class RemoveQuestionSubcommand extends QOTWSubcommand {
2323
@Override
24-
protected ReplyCallbackAction handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
24+
protected InteractionCallbackAction<?> handleCommand(SlashCommandInteractionEvent event, Connection con, long guildId) throws SQLException {
2525
OptionMapping idOption = event.getOption("id");
2626
if (idOption == null) {
2727
return Responses.warning(event, "Missing required arguments.");

src/main/resources/commands/slash/qotw.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@
2222
# /qotw questions-queue add
2323
- name: add
2424
description: Add a question to the queue.
25-
options:
26-
- name: question
27-
description: The question to add.
28-
required: true
29-
type: STRING
30-
- name: priority
31-
description: The priority to give the question. Questions with higher priority show up first.
32-
required: false
33-
type: INTEGER
3425

3526
# /qotw questions-queue remove
3627
- name: remove

0 commit comments

Comments
 (0)