Skip to content

Commit de290ac

Browse files
committed
pagination in QOTW question list
1 parent d111e40 commit de290ac

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

src/main/java/net/javadiscord/javabot/Bot.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.javadiscord.javabot.systems.moderation.report.ReportManager;
2828
import net.javadiscord.javabot.systems.moderation.server_lock.ServerLockManager;
2929
import net.javadiscord.javabot.systems.qotw.commands.questions_queue.AddQuestionSubcommand;
30+
import net.javadiscord.javabot.systems.qotw.commands.view.QOTWQuerySubcommand;
3031
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionInteractionManager;
3132
import net.javadiscord.javabot.systems.staff_commands.self_roles.SelfRoleInteractionManager;
3233
import net.javadiscord.javabot.systems.staff_commands.embeds.AddEmbedFieldSubcommand;
@@ -167,7 +168,8 @@ private static void addComponentHandler(@NotNull DIH4JDA dih4jda) {
167168
List.of("resolve-report"), new ReportManager(),
168169
List.of("self-role"), new SelfRoleInteractionManager(),
169170
List.of("qotw-submission"), new SubmissionInteractionManager(),
170-
List.of("help-channel", "help-thank"), new HelpChannelInteractionManager()
171+
List.of("help-channel", "help-thank"), new HelpChannelInteractionManager(),
172+
List.of("qotw-list-questions"),new QOTWQuerySubcommand()
171173
));
172174
dih4jda.addModalHandlers(Map.of(
173175
List.of("qotw-add-question"), new AddQuestionSubcommand(),

src/main/java/net/javadiscord/javabot/systems/qotw/commands/view/QOTWListAnswersSubcommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Represents the `/qotw-view list-answers` subcommand. It allows for listing answers to a specific QOTW.
2424
*/
25-
public class QOTWListAnswersSubcommand extends SlashCommand.Subcommand {
25+
public class QOTWListAnswersSubcommand extends SlashCommand.Subcommand{
2626
public QOTWListAnswersSubcommand() {
2727
setSubcommandData(new SubcommandData("list-answers", "Lists answers to (previous) questions of the week")
2828
.addOption(OptionType.INTEGER, "question", "The question number",true));
@@ -36,7 +36,7 @@ public void execute(SlashCommandInteractionEvent event) {
3636
}
3737
OptionMapping questionNumOption = event.getOption("question");
3838
if (questionNumOption==null) {
39-
Responses.error(event, "Missing question number").setEphemeral(true).queue();
39+
Responses.replyMissingArguments(event);
4040
return;
4141
}
4242
int questionNum = questionNumOption.getAsInt();
@@ -51,8 +51,8 @@ public void execute(SlashCommandInteractionEvent event) {
5151
.stream()
5252
.filter(submission -> isSubmissionVisible(submission, event.getUser().getIdLong()))
5353
.filter(submission -> submission.getQuestionNumber() == questionNum)
54-
.map(s -> (isBestAnswer(submissionChannel,s)?
55-
"best ":s.getStatus() == SubmissionStatus.ACCEPTED?"accepted ":"")+
54+
.map(s -> (isBestAnswer(submissionChannel,s) ?
55+
"best " : s.getStatus() == SubmissionStatus.ACCEPTED?"accepted ":"")+
5656
"Answer by <@"+s.getAuthorId()+">")
5757
.collect(Collectors.joining("\n"));
5858
if (allAnswers.isEmpty()) {
Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
package net.javadiscord.javabot.systems.qotw.commands.view;
22

3+
import java.sql.SQLException;
34
import java.util.Comparator;
45
import java.util.List;
56

7+
import javax.annotation.Nonnull;
8+
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import com.dynxsty.dih4jda.interactions.ComponentIdBuilder;
612
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
13+
import com.dynxsty.dih4jda.interactions.components.ButtonHandler;
714

815
import net.dv8tion.jda.api.EmbedBuilder;
916
import net.dv8tion.jda.api.entities.MessageEmbed;
1017
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
18+
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1119
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1220
import net.dv8tion.jda.api.interactions.commands.OptionType;
1321
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
22+
import net.dv8tion.jda.api.interactions.components.ActionRow;
23+
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1424
import net.javadiscord.javabot.data.h2db.DbActions;
25+
import net.javadiscord.javabot.data.h2db.DbHelper;
1526
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
1627
import net.javadiscord.javabot.systems.qotw.model.QOTWQuestion;
1728
import net.javadiscord.javabot.util.Responses;
1829

1930
/**
2031
* Represents the `/qotw-view query` subcommand. It allows for listing filtering QOTWs.
2132
*/
22-
public class QOTWQuerySubcommand extends SlashCommand.Subcommand {
33+
public class QOTWQuerySubcommand extends SlashCommand.Subcommand implements ButtonHandler{
34+
35+
private static final int MAX_BUTTON_QUERY_LENGTH = 10;
36+
private static final int PAGE_LIMIT = 20;
37+
38+
/**
39+
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
40+
*/
2341
public QOTWQuerySubcommand() {
2442
setSubcommandData(new SubcommandData("list-questions", "Lists previous questions of the week")
25-
.addOption(OptionType.STRING, "query", "Only queries questions that contain a specific query", false));
43+
.addOption(OptionType.STRING, "query", "Only queries questions that contain a specific query", false)
44+
.addOption(OptionType.INTEGER, "page", "The page to show, starting with 1"));
2645
}
2746

2847
@Override
@@ -32,17 +51,72 @@ public void execute(SlashCommandInteractionEvent event) {
3251
return;
3352
}
3453
String query = event.getOption("query", ()->"", OptionMapping::getAsString);
54+
int page = event.getOption("page",() -> 1, OptionMapping::getAsInt)-1;
55+
if (page < 0) {
56+
Responses.error(event, "Invalid page - must be >= 1").queue();
57+
return;
58+
}
3559
event.deferReply(true).queue();
3660
DbActions.doAsyncDaoAction(QuestionQueueRepository::new, repo->{
37-
List<QOTWQuestion> questions = repo.getUsedQuestionsWithQuery(event.getGuild().getIdLong(), query, 0, 20);
38-
EmbedBuilder eb = new EmbedBuilder();
39-
eb.setDescription("Questions of the week"+(query.isEmpty()?"":" matching '"+query+"'"));
40-
questions
41-
.stream()
42-
.sorted(Comparator.comparingInt(QOTWQuestion::getQuestionNumber))
43-
.map(q -> new MessageEmbed.Field("Question #" + q.getQuestionNumber(), q.getText(), true))
44-
.forEach(eb::addField);
45-
event.getHook().sendMessageEmbeds(eb.build()).queue();
61+
MessageEmbed embed = buildListQuestionsEmbed(repo, event.getGuild().getIdLong(), query, page);
62+
event.getHook()
63+
.sendMessageEmbeds(embed)
64+
.addActionRows(buildPageControls(query, page, embed))
65+
.queue();
66+
});
67+
}
68+
69+
@Override
70+
public void handleButton(@Nonnull ButtonInteractionEvent event, @Nonnull Button button) {
71+
event.deferEdit().queue();
72+
String[] id = ComponentIdBuilder.split(event.getComponentId());
73+
int page = Integer.parseInt(id[1]);
74+
String query = id.length == 2 ? "" : id[2];
75+
if (page < 0) {
76+
Responses.error(event.getHook(), "Invalid page - must be >= 1").queue();
77+
return;
78+
}
79+
DbHelper.doDaoAction(QuestionQueueRepository::new, repo -> {
80+
MessageEmbed embed = buildListQuestionsEmbed(repo, event.getGuild().getIdLong(), query, page);
81+
event.getHook()
82+
.editOriginalEmbeds(embed)
83+
.setActionRows(buildPageControls(query, page, embed))
84+
.queue();
4685
});
4786
}
87+
88+
@NotNull
89+
private ActionRow buildPageControls(String query, int page, MessageEmbed embed) {
90+
if (query.length() > MAX_BUTTON_QUERY_LENGTH) {
91+
query = query.substring(0,MAX_BUTTON_QUERY_LENGTH);
92+
}
93+
return ActionRow.of(
94+
Button.primary(ComponentIdBuilder.build("qotw-list-questions", page-1+"", query), "Previous Page")
95+
.withDisabled(page <= 0),
96+
Button.primary(ComponentIdBuilder.build("qotw-list-questions", page+1+"", query), "Next Page")
97+
.withDisabled(embed.getFields().size() < PAGE_LIMIT)
98+
);
99+
}
100+
101+
private MessageEmbed buildListQuestionsEmbed(QuestionQueueRepository repo, long guildId, String query, int page) throws SQLException {
102+
List<QOTWQuestion> questions = repo.getUsedQuestionsWithQuery(guildId, query, page*PAGE_LIMIT, PAGE_LIMIT);
103+
EmbedBuilder eb = new EmbedBuilder();
104+
eb.setDescription("**Questions of the week"+(query.isEmpty()?"":" matching '"+query+"'")+"**");
105+
questions
106+
.stream()
107+
.sorted(Comparator.comparingInt(QOTWQuestion::getQuestionNumber))
108+
.map(q -> new MessageEmbed.Field("Question #" + q.getQuestionNumber(), q.getText(), true))
109+
.forEach(eb::addField);
110+
if(eb.getFields().isEmpty()) {
111+
eb.appendDescription("\nNo questions found");
112+
if(page != 0) {
113+
eb.appendDescription(" on this page");
114+
}
115+
}
116+
eb.setFooter("Page "+(page+1));
117+
eb.setColor(Responses.Type.DEFAULT.getColor());
118+
return eb.build();
119+
}
120+
121+
48122
}

src/main/java/net/javadiscord/javabot/systems/qotw/commands/view/QOTWViewAnswerSubcommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.jetbrains.annotations.NotNull;
44

55
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
6-
76
import net.dv8tion.jda.api.EmbedBuilder;
87
import net.dv8tion.jda.api.entities.MessageEmbed;
98
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -39,7 +38,7 @@ public void execute(SlashCommandInteractionEvent event) {
3938
}
4039
OptionMapping questionOption = event.getOption("question");
4140
if (questionOption == null) {
42-
Responses.error(event, "The question option is missing.").queue();
41+
Responses.replyMissingArguments(event);
4342
return;
4443
}
4544
OptionMapping answerOwnerOption = event.getOption("answerer");
@@ -77,6 +76,7 @@ public void execute(SlashCommandInteractionEvent event) {
7776
.setTitle("Answer to Question of the Week #"+submission.getQuestionNumber())
7877
.setDescription("Answer by <@"+submission.getAuthorId()+">")
7978
.setFooter("Requested by "+requester)
79+
.setColor(Responses.Type.DEFAULT.getColor())
8080
.build();
8181
}
8282

0 commit comments

Comments
 (0)