Skip to content

Commit 7920d0c

Browse files
Merge pull request #341 from Java-Discord/dynxsty/qotw-view-mini-cleanup
Small Cleanup of `/qotw-view`
2 parents 55a2d3b + e0fa9e4 commit 7920d0c

File tree

4 files changed

+100
-102
lines changed

4 files changed

+100
-102
lines changed
Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package net.javadiscord.javabot.systems.qotw.commands.view;
22

3-
import java.util.List;
4-
import java.util.stream.Collectors;
5-
63
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
7-
84
import net.dv8tion.jda.api.EmbedBuilder;
95
import net.dv8tion.jda.api.entities.TextChannel;
106
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -18,20 +14,39 @@
1814
import net.javadiscord.javabot.systems.qotw.submissions.model.QOTWSubmission;
1915
import net.javadiscord.javabot.systems.qotw.submissions.subcommands.MarkBestAnswerSubcommand;
2016
import net.javadiscord.javabot.util.Responses;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
import java.util.List;
20+
import java.util.stream.Collectors;
2121

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 {
26+
/**
27+
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
28+
*/
2629
public QOTWListAnswersSubcommand() {
2730
setSubcommandData(new SubcommandData("list-answers", "Lists answers to (previous) questions of the week")
28-
.addOption(OptionType.INTEGER, "question", "The question number",true));
31+
.addOption(OptionType.INTEGER, "question", "The question number", true)
32+
);
33+
}
34+
35+
/**
36+
* Checks whether a submission is visible to a specific user.
37+
*
38+
* @param submission the {@link QOTWSubmission} to be checked
39+
* @param viewerId the user to check against
40+
* @return {@code true} if the submission is visible, else {@code false}}
41+
*/
42+
public static boolean isSubmissionVisible(@NotNull QOTWSubmission submission, long viewerId) {
43+
return submission.getStatus() == SubmissionStatus.ACCEPTED || submission.getAuthorId() == viewerId;
2944
}
3045

3146
@Override
32-
public void execute(SlashCommandInteractionEvent event) {
47+
public void execute(@NotNull SlashCommandInteractionEvent event) {
3348
if (!event.isFromGuild()) {
34-
Responses.replyGuildOnly(event).setEphemeral(true).queue();
49+
Responses.replyGuildOnly(event).queue();
3550
return;
3651
}
3752
OptionMapping questionNumOption = event.getOption("question");
@@ -45,18 +60,18 @@ public void execute(SlashCommandInteractionEvent event) {
4560
DbActions.doAsyncDaoAction(QOTWSubmissionRepository::new, repo -> {
4661
List<QOTWSubmission> submissions = repo.getSubmissionsByQuestionNumber(event.getGuild().getIdLong(), questionNum);
4762
EmbedBuilder eb = new EmbedBuilder()
48-
.setDescription("**Answers of Question of the week #" + questionNum + "**\n")
49-
.setColor(Responses.Type.DEFAULT.getColor())
50-
.setFooter("Results may not be accurate due to historic data.");
63+
.setTitle("Answers of Question of the Week #" + questionNum)
64+
.setColor(Responses.Type.DEFAULT.getColor())
65+
.setFooter("Results may not be accurate due to historic data.");
5166
TextChannel submissionChannel = Bot.getConfig().get(event.getGuild()).getQotwConfig().getSubmissionChannel();
5267
String allAnswers = submissions
53-
.stream()
54-
.filter(submission -> isSubmissionVisible(submission, event.getUser().getIdLong()))
55-
.filter(submission -> submission.getQuestionNumber() == questionNum)
56-
.map(s -> (isBestAnswer(submissionChannel,s) ?
57-
"best " : s.getStatus() == SubmissionStatus.ACCEPTED ? "accepted " : "")+
58-
"Answer by <@" + s.getAuthorId() + ">")
59-
.collect(Collectors.joining("\n"));
68+
.stream()
69+
.filter(submission -> isSubmissionVisible(submission, event.getUser().getIdLong()))
70+
.filter(submission -> submission.getQuestionNumber() == questionNum)
71+
.map(s -> (isBestAnswer(submissionChannel,s) ?
72+
"**Best** " : s.getStatus() == SubmissionStatus.ACCEPTED ? "Accepted " : "") +
73+
"Answer by <@" + s.getAuthorId() + ">")
74+
.collect(Collectors.joining("\n"));
6075
if (allAnswers.isEmpty()) {
6176
allAnswers = "No accepted answers found.";
6277
}
@@ -65,23 +80,13 @@ public void execute(SlashCommandInteractionEvent event) {
6580
});
6681
}
6782

68-
/**
69-
* Checks whether a submission is visible to a specific user.
70-
* @param submission the {@link QOTWSubmission} to be checked
71-
* @param viewerID the user to check against
72-
* @return {@code true} if the submission is visible, else {@code false}}
73-
*/
74-
public static boolean isSubmissionVisible(QOTWSubmission submission, long viewerID) {
75-
return submission.getStatus() == SubmissionStatus.ACCEPTED || submission.getAuthorId() == viewerID;
76-
}
77-
7883
private boolean isBestAnswer(TextChannel submissionChannel, QOTWSubmission submission) {
7984
return submissionChannel
80-
.retrieveArchivedPrivateThreadChannels()
81-
.stream()
82-
.filter(t -> t.getIdLong() == submission.getThreadId())
83-
.findAny()
84-
.map(t -> MarkBestAnswerSubcommand.isSubmissionThreadABestAnswer(t))
85-
.orElse(false);
85+
.retrieveArchivedPrivateThreadChannels()
86+
.stream()
87+
.filter(t -> t.getIdLong() == submission.getThreadId())
88+
.findAny()
89+
.map(MarkBestAnswerSubcommand::isSubmissionThreadABestAnswer)
90+
.orElse(false);
8691
}
8792
}
Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package 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-
113
import com.dynxsty.dih4jda.interactions.ComponentIdBuilder;
124
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
135
import com.dynxsty.dih4jda.interactions.components.ButtonHandler;
14-
156
import net.dv8tion.jda.api.EmbedBuilder;
167
import net.dv8tion.jda.api.entities.MessageEmbed;
178
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@@ -26,11 +17,17 @@
2617
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
2718
import net.javadiscord.javabot.systems.qotw.model.QOTWQuestion;
2819
import 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("\nNo 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
}

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.javadiscord.javabot.systems.qotw.commands.view;
22

3-
import org.jetbrains.annotations.NotNull;
4-
53
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
64
import net.dv8tion.jda.api.EmbedBuilder;
75
import net.dv8tion.jda.api.entities.MessageEmbed;
@@ -15,23 +13,25 @@
1513
import net.javadiscord.javabot.systems.qotw.submissions.model.QOTWSubmission;
1614
import net.javadiscord.javabot.util.MessageActionUtils;
1715
import net.javadiscord.javabot.util.Responses;
16+
import org.jetbrains.annotations.NotNull;
1817

1918
/**
2019
* Represents the `/qotw-view answer` subcommand. It allows for viewing an answer to a QOTW.
2120
*/
22-
public class QOTWViewAnswerSubcommand extends SlashCommand.Subcommand{
21+
public class QOTWViewAnswerSubcommand extends SlashCommand.Subcommand {
2322

2423
/**
2524
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
2625
*/
2726
public QOTWViewAnswerSubcommand() {
2827
setSubcommandData(new SubcommandData("answer", "Views the content of an answer to the Question of the Week")
2928
.addOption(OptionType.INTEGER, "question", "The question number the answer has been submitted to", true)
30-
.addOption(OptionType.USER, "answerer", "The user who answered the question", true));
29+
.addOption(OptionType.USER, "answerer", "The user who answered the question", true)
30+
);
3131
}
3232

3333
@Override
34-
public void execute(SlashCommandInteractionEvent event) {
34+
public void execute(@NotNull SlashCommandInteractionEvent event) {
3535
if (!event.isFromGuild()) {
3636
Responses.replyGuildOnly(event).setEphemeral(true).queue();
3737
return;
@@ -54,24 +54,22 @@ public void execute(SlashCommandInteractionEvent event) {
5454
return;
5555
}
5656
Bot.getConfig().get(event.getGuild()).getQotwConfig()
57-
.getSubmissionChannel().retrieveArchivedPrivateThreadChannels().queue(threadChannels->{
58-
threadChannels
59-
.stream()
60-
.filter(c->c.getIdLong() == submission.getThreadId())
61-
.findAny()
62-
.ifPresentOrElse(submissionChannel->
63-
submissionChannel.getHistoryFromBeginning(100).queue(history->
64-
MessageActionUtils.copyMessagesToNewThread(event.getGuildChannel().asStandardGuildMessageChannel(),
65-
buildQOTWInfoEmbed(submission, event.getMember()==null?event.getUser().getName():event.getMember().getEffectiveName()),
66-
"QOTW #"+submission.getQuestionNumber(),
67-
history.getRetrievedHistory(),
68-
() -> Responses.success(event.getHook(), "View Answer", "Answer copied successfully").setEphemeral(true))),
69-
() -> Responses.error(event.getHook(), "The QOTW submission thread was not found.").queue());
70-
});
57+
.getSubmissionChannel().retrieveArchivedPrivateThreadChannels().queue(threadChannels -> threadChannels
58+
.stream()
59+
.filter(c -> c.getIdLong() == submission.getThreadId())
60+
.findAny()
61+
.ifPresentOrElse(submissionChannel ->
62+
submissionChannel.getHistoryFromBeginning(100).queue(history ->
63+
MessageActionUtils.copyMessagesToNewThread(event.getGuildChannel().asStandardGuildMessageChannel(),
64+
buildQOTWInfoEmbed(submission, event.getMember() == null ? event.getUser().getName() : event.getMember().getEffectiveName()),
65+
"QOTW #" + submission.getQuestionNumber(),
66+
history.getRetrievedHistory(),
67+
() -> Responses.success(event.getHook(), "View Answer", "Answer copied successfully"))),
68+
() -> Responses.error(event.getHook(), "The QOTW-Submission thread was not found.").queue()));
7169
});
7270
}
7371

74-
private @NotNull MessageEmbed buildQOTWInfoEmbed(QOTWSubmission submission, String requester) {
72+
private @NotNull MessageEmbed buildQOTWInfoEmbed(@NotNull QOTWSubmission submission, String requester) {
7573
return new EmbedBuilder()
7674
.setTitle("Answer to Question of the Week #" + submission.getQuestionNumber())
7775
.setDescription("Answer by <@" + submission.getAuthorId() + ">")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package net.javadiscord.javabot.systems.qotw.commands.view;
22

33
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
4-
54
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
65
import net.dv8tion.jda.api.interactions.commands.build.Commands;
76

87
/**
98
* Represents the `/qotw-view` command.
109
* It allows to view previous QOTWs and their answers.
1110
*/
12-
public class QOTWViewCommand extends SlashCommand{
11+
public class QOTWViewCommand extends SlashCommand {
1312
/**
1413
* This classes constructor which sets the {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData} and
1514
* adds the corresponding {@link net.dv8tion.jda.api.interactions.commands.Command.SubcommandGroup}s.
1615
*/
1716
public QOTWViewCommand() {
18-
setSlashCommandData(Commands.slash("qotw-view", "Query questions of the week and their answers")
17+
setSlashCommandData(Commands.slash("qotw-view", "Query 'Questions of the Week' and their answers")
1918
.setDefaultPermissions(DefaultMemberPermissions.ENABLED)
20-
.setGuildOnly(true));
19+
.setGuildOnly(true)
20+
);
2121
addSubcommands(new QOTWQuerySubcommand(), new QOTWListAnswersSubcommand(), new QOTWViewAnswerSubcommand());
2222

2323
}

0 commit comments

Comments
 (0)