|
1 | 1 | package net.javadiscord.javabot.systems.qotw.subcommands.questions_queue; |
2 | 2 |
|
| 3 | +import net.dv8tion.jda.api.entities.Message; |
| 4 | +import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; |
3 | 5 | 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; |
6 | 13 | import net.javadiscord.javabot.command.Responses; |
| 14 | +import net.javadiscord.javabot.data.h2db.DbHelper; |
7 | 15 | import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository; |
8 | 16 | import net.javadiscord.javabot.systems.qotw.model.QOTWQuestion; |
9 | 17 | import net.javadiscord.javabot.systems.qotw.subcommands.QOTWSubcommand; |
10 | 18 |
|
11 | 19 | import java.sql.Connection; |
12 | | -import java.sql.SQLException; |
13 | 20 |
|
14 | 21 | /** |
15 | 22 | * Subcommand that allows staff-members to add question to the QOTW-Queue. |
16 | 23 | */ |
17 | 24 | public class AddQuestionSubcommand extends QOTWSubcommand { |
18 | 25 | @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 |
20 | 54 | QOTWQuestion question = new QOTWQuestion(); |
21 | | - question.setGuildId(guildId); |
| 55 | + question.setGuildId(event.getGuild().getIdLong()); |
22 | 56 | question.setCreatedBy(event.getUser().getIdLong()); |
23 | 57 | question.setPriority(0); |
24 | 58 |
|
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."); |
28 | 62 | } |
| 63 | + question.setText(textOption.getAsString()); |
29 | 64 |
|
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."); |
33 | 68 | } |
34 | | - question.setText(text); |
35 | 69 |
|
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())); |
39 | 72 | } |
40 | 73 |
|
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."); |
43 | 76 | } |
44 | 77 | } |
0 commit comments