11package net .javadiscord .javabot .systems .qotw .commands .view ;
22
3- import java .sql .SQLException ;
4- import java .util .Comparator ;
5- import java .util .List ;
6-
7- import javax .annotation .Nonnull ;
8-
9- import org .jetbrains .annotations .NotNull ;
10-
113import com .dynxsty .dih4jda .interactions .ComponentIdBuilder ;
124import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
135import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
14-
156import net .dv8tion .jda .api .EmbedBuilder ;
167import net .dv8tion .jda .api .entities .MessageEmbed ;
178import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
2617import net .javadiscord .javabot .systems .qotw .dao .QuestionQueueRepository ;
2718import net .javadiscord .javabot .systems .qotw .model .QOTWQuestion ;
2819import net .javadiscord .javabot .util .Responses ;
20+ import org .jetbrains .annotations .NotNull ;
21+
22+ import javax .annotation .Nonnull ;
23+ import java .sql .SQLException ;
24+ import java .util .Comparator ;
25+ import java .util .List ;
2926
3027/**
3128 * Represents the `/qotw-view query` subcommand. It allows for listing filtering QOTWs.
3229 */
33- public class QOTWQuerySubcommand extends SlashCommand .Subcommand implements ButtonHandler {
30+ public class QOTWQuerySubcommand extends SlashCommand .Subcommand implements ButtonHandler {
3431
3532 private static final int MAX_BUTTON_QUERY_LENGTH = 10 ;
3633 private static final int PAGE_LIMIT = 20 ;
@@ -39,30 +36,31 @@ public class QOTWQuerySubcommand extends SlashCommand.Subcommand implements Butt
3936 * The constructor of this class, which sets the corresponding {@link SubcommandData}.
4037 */
4138 public QOTWQuerySubcommand () {
42- setSubcommandData (new SubcommandData ("list-questions" , "Lists previous questions of the week " )
39+ setSubcommandData (new SubcommandData ("list-questions" , "Lists previous 'Questions of the Week' " )
4340 .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" , false ));
41+ .addOption (OptionType .INTEGER , "page" , "The page to show, starting with 1" , false )
42+ );
4543 }
4644
4745 @ Override
4846 public void execute (SlashCommandInteractionEvent event ) {
49- if (!event .isFromGuild ()) {
47+ if (!event .isFromGuild ()) {
5048 Responses .replyGuildOnly (event ).setEphemeral (true ).queue ();
5149 return ;
5250 }
5351 String query = event .getOption ("query" , "" , OptionMapping ::getAsString );
54- int page = event .getOption ("page" , 1 , OptionMapping ::getAsInt ) -1 ;
52+ int page = event .getOption ("page" , 1 , OptionMapping ::getAsInt ) - 1 ;
5553 if (page < 0 ) {
56- Responses .error (event , "Invalid page - must be >= 1 " ).queue ();
54+ Responses .error (event , "The page must be equal to or greater than 1! " ).queue ();
5755 return ;
5856 }
5957 event .deferReply (true ).queue ();
60- DbActions .doAsyncDaoAction (QuestionQueueRepository ::new , repo -> {
58+ DbActions .doAsyncDaoAction (QuestionQueueRepository ::new , repo -> {
6159 MessageEmbed embed = buildListQuestionsEmbed (repo , event .getGuild ().getIdLong (), query , page );
6260 event .getHook ()
63- .sendMessageEmbeds (embed )
64- .addActionRows (buildPageControls (query , page , embed ))
65- .queue ();
61+ .sendMessageEmbeds (embed )
62+ .addActionRows (buildPageControls (query , page , embed ))
63+ .queue ();
6664 });
6765 }
6866
@@ -73,50 +71,47 @@ public void handleButton(@Nonnull ButtonInteractionEvent event, @Nonnull Button
7371 int page = Integer .parseInt (id [1 ]);
7472 String query = id .length == 2 ? "" : id [2 ];
7573 if (page < 0 ) {
76- Responses .error (event .getHook (), "Invalid page - must be >= 1 " ).queue ();
74+ Responses .error (event .getHook (), "The page must be equal to or greater than 1! " ).queue ();
7775 return ;
7876 }
7977 DbHelper .doDaoAction (QuestionQueueRepository ::new , repo -> {
8078 MessageEmbed embed = buildListQuestionsEmbed (repo , event .getGuild ().getIdLong (), query , page );
8179 event .getHook ()
82- .editOriginalEmbeds (embed )
83- .setActionRows (buildPageControls (query , page , embed ))
84- .queue ();
80+ .editOriginalEmbeds (embed )
81+ .setActionRows (buildPageControls (query , page , embed ))
82+ .queue ();
8583 });
8684 }
8785
8886 @ NotNull
89- private ActionRow buildPageControls (String query , int page , MessageEmbed embed ) {
87+ private ActionRow buildPageControls (@ NotNull String query , int page , MessageEmbed embed ) {
9088 if (query .length () > MAX_BUTTON_QUERY_LENGTH ) {
91- query = query .substring (0 ,MAX_BUTTON_QUERY_LENGTH );
89+ query = query .substring (0 , MAX_BUTTON_QUERY_LENGTH );
9290 }
9391 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 )
92+ Button .primary (ComponentIdBuilder .build ("qotw-list-questions" , page - 1 + "" , query ), "Previous Page" )
93+ .withDisabled (page <= 0 ),
94+ Button .primary (ComponentIdBuilder .build ("qotw-list-questions" , page + 1 + "" , query ), "Next Page" )
95+ .withDisabled (embed .getFields ().size () < PAGE_LIMIT )
9896 );
9997 }
10098
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 ()) {
99+ private @ NotNull MessageEmbed buildListQuestionsEmbed (@ NotNull QuestionQueueRepository repo , long guildId , String query , int page ) throws SQLException {
100+ List <QOTWQuestion > questions = repo .getUsedQuestionsWithQuery (guildId , query , page * PAGE_LIMIT , PAGE_LIMIT );
101+ EmbedBuilder eb = new EmbedBuilder ()
102+ .setDescription ("**Questions of the Week" + (query .isEmpty () ? "" : " matching '" + query + "'" ) + "**" )
103+ .setColor (Responses .Type .DEFAULT .getColor ())
104+ .setFooter ("Page " + (page + 1 ));
105+ questions .stream ()
106+ .sorted (Comparator .comparingInt (QOTWQuestion ::getQuestionNumber ))
107+ .map (q -> new MessageEmbed .Field ("Question #" + q .getQuestionNumber (), q .getText (), true ))
108+ .forEach (eb ::addField );
109+ if (eb .getFields ().isEmpty ()) {
111110 eb .appendDescription ("\n No questions found" );
112- if (page != 0 ) {
111+ if (page != 0 ) {
113112 eb .appendDescription (" on this page" );
114113 }
115114 }
116- eb .setFooter ("Page " + (page +1 ));
117- eb .setColor (Responses .Type .DEFAULT .getColor ());
118115 return eb .build ();
119116 }
120-
121-
122117}
0 commit comments