Skip to content

Commit 8d49837

Browse files
Made some final cleanup
1 parent 2415423 commit 8d49837

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

src/main/java/net/javadiscord/javabot/data/config/guild/QOTWConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
77
import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel;
88
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
9+
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
910
import net.javadiscord.javabot.data.config.GuildConfigItem;
1011

12+
import java.util.List;
13+
1114
/**
1215
* Configuration for the guild's qotw system.
1316
*/
@@ -19,6 +22,7 @@ public class QOTWConfig extends GuildConfigItem {
1922
private long submissionsForumChannelId;
2023
private long questionRoleId;
2124
private long qotwReviewRoleId;
25+
private String submissionForumOngoingReviewTagName = "";
2226

2327
public NewsChannel getQuestionChannel() {
2428
return this.getGuild().getNewsChannelById(this.questionChannelId);
@@ -39,4 +43,16 @@ public Role getQOTWRole() {
3943
public Role getQOTWReviewRole() {
4044
return this.getGuild().getRoleById(this.qotwReviewRoleId);
4145
}
46+
47+
/**
48+
* Gets a {@link ForumTag} based on the specified name.
49+
*
50+
* @return The specified {@link ForumTag} or null if there is no tag matching the specified name.
51+
*/
52+
public ForumTag getSubmissionsForumOngoingReviewTag() {
53+
ForumChannel forumChannel = getSubmissionsForumChannel();
54+
if (forumChannel == null) return null;
55+
List<ForumTag> tags = forumChannel.getAvailableTagsByName(this.submissionForumOngoingReviewTagName, true);
56+
return tags.stream().findFirst().orElse(null);
57+
}
4258
}

src/main/java/net/javadiscord/javabot/systems/notification/QOTWNotificationService.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.jetbrains.annotations.NotNull;
1313
import org.springframework.dao.DataAccessException;
1414

15-
import javax.annotation.Nonnull;
1615
import java.time.Instant;
1716

1817
/**
@@ -49,8 +48,8 @@ public void sendAccountIncrementedNotification() {
4948
notificationService.withUser(user).sendDirectMessage(c -> c.sendMessageEmbeds(buildAccountIncrementEmbed(account.getPoints())));
5049
}
5150

52-
public void sendSubmissionDeclinedEmbed(@Nonnull String reason) {
53-
notificationService.withUser(user).sendDirectMessage(c -> c.sendMessageEmbeds(buildSubmissionDeclinedEmbed(reason)));
51+
public void sendSubmissionDeclinedEmbed() {
52+
notificationService.withUser(user).sendDirectMessage(c -> c.sendMessageEmbeds(buildSubmissionDeclinedEmbed()));
5453
}
5554

5655
private @NotNull EmbedBuilder buildQOTWNotificationEmbed() {
@@ -81,16 +80,14 @@ public void sendSubmissionDeclinedEmbed(@Nonnull String reason) {
8180
.build();
8281
}
8382

84-
private @NotNull MessageEmbed buildSubmissionDeclinedEmbed(String reasons) {
83+
private @NotNull MessageEmbed buildSubmissionDeclinedEmbed() {
8584
return this.buildQOTWNotificationEmbed()
8685
.setColor(Responses.Type.ERROR.getColor())
8786
.setDescription(String.format("""
8887
Hey %s,
89-
Your QOTW-Submission was **declined** for the following reasons:
90-
**`%s`**
91-
88+
Your QOTW-Submission was **declined**.
9289
However, you can try your luck again next week!""",
93-
user.getAsMention(), reasons))
90+
user.getAsMention()))
9491
.build();
9592
}
9693
}

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWCloseSubmissionsJob.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import net.dv8tion.jda.api.entities.Message;
99
import net.dv8tion.jda.api.entities.MessageEmbed;
1010
import net.dv8tion.jda.api.entities.MessageHistory;
11-
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
1211
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
1312
import net.dv8tion.jda.api.interactions.components.ActionRow;
1413
import net.dv8tion.jda.api.interactions.components.buttons.Button;
14+
import net.dv8tion.jda.api.requests.restaction.ForumPostAction;
1515
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
1616
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
1717
import net.javadiscord.javabot.data.config.BotConfig;
@@ -37,7 +37,7 @@
3737
@Service
3838
@RequiredArgsConstructor
3939
public class QOTWCloseSubmissionsJob {
40-
private static final String SUBMISSION_PENDING = "\uD83D\uDD52";
40+
private static final String SUBMISSION_PENDING = "\uD83D\uDD52 ";
4141

4242
private final JDA jda;
4343
private final NotificationService notificationService;
@@ -65,11 +65,11 @@ public void execute() throws SQLException {
6565
questionMessage.editMessageComponents(ActionRow.of(Button.secondary("qotw-submission:closed", "Submissions closed").asDisabled())).queue();
6666
notificationService.withGuild(guild)
6767
.sendToModerationLog(log ->
68-
log.sendMessageFormat("%s%nIt's review time! There are %s threads to review:\n%s",
68+
log.sendMessageFormat("%s%nIt's review time! There are **%s** threads to review:%n%n%s",
6969
qotwConfig.getQOTWReviewRole().getAsMention(),
7070
qotwConfig.getSubmissionChannel().getThreadChannels().size(),
7171
qotwConfig.getSubmissionChannel().getThreadChannels().stream()
72-
.map(ThreadChannel::getAsMention)
72+
.map(t -> "> %s (%d message(s))".formatted(t.getAsMention(), t.getMessageCount() - 2)) // NOTE: Subtract 2 messages which are send by the bot
7373
.collect(Collectors.joining("\n")))
7474
);
7575
qotwConfig.getSubmissionChannel().getThreadChannels().forEach(t ->
@@ -82,11 +82,14 @@ public void execute() throws SQLException {
8282
QOTWQuestion question = questionOptional.get();
8383
try (MessageCreateData data = new MessageCreateBuilder()
8484
.setEmbeds(buildQuestionEmbed(question)).build()) {
85-
qotwConfig.getSubmissionsForumChannel()
86-
.createForumPost(String.format("#%s — %s", question.getQuestionNumber(), question.getText()), data)
87-
.queue(f -> f.getThreadChannel().getManager().setPinned(true).queue());
85+
ForumPostAction action = qotwConfig.getSubmissionsForumChannel()
86+
.createForumPost(String.format("Week %s — %s", question.getQuestionNumber(), question.getText().replace("*", "")), data);
87+
if (qotwConfig.getSubmissionsForumOngoingReviewTag() != null) {
88+
action.setTags(qotwConfig.getSubmissionsForumOngoingReviewTag());
8889
}
90+
action.queue(f -> f.getThreadChannel().getManager().setPinned(true).queue());
8991
}
92+
}
9093
});
9194
}
9295
}

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWUserReminderJob.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@ public class QOTWUserReminderJob {
3737
/**
3838
* Checks that there's a question in the QOTW queue ready for posting soon.
3939
*/
40-
@Scheduled(cron = "* 0 16 * * 5")//Friday 16:00
40+
@Scheduled(cron = "0 0 16 * * 5")//Friday 16:00
4141
public void execute() {
4242
for (Guild guild : jda.getGuilds()) {
4343
QOTWConfig config = botConfig.get(guild).getQotwConfig();
4444
List<QOTWSubmission> submissions = new SubmissionManager(config, pointsService, questionQueueRepository, notificationService, asyncPool).getActiveSubmissions();
4545
for (QOTWSubmission submission : submissions) {
46-
UserPreference preference = userPreferenceService.getOrCreate(submission.getAuthor().getIdLong(), Preference.QOTW_REMINDER);
47-
if (Boolean.parseBoolean(preference.getState()) && submission.getThread().getMessageCount() <= 1) {
48-
submission.getThread()
49-
.sendMessageFormat("**Question of the Week Reminder**\nHey %s! You still have some time left to submit your answer!", submission.getAuthor().getAsMention())
50-
.queue();
51-
}
46+
submission.retrieveAuthor(author -> {
47+
UserPreference preference = userPreferenceService.getOrCreate(author.getIdLong(), Preference.QOTW_REMINDER);
48+
if (Boolean.parseBoolean(preference.getState()) && submission.getThread().getMessageCount() <= 2) {
49+
submission.getThread()
50+
.sendMessageFormat("**Question of the Week Reminder**\nHey %s! You still have some time left to submit your answer!", submission.getAuthor().getAsMention())
51+
.queue();
52+
}
53+
});
54+
5255
}
5356
}
5457
}

src/main/java/net/javadiscord/javabot/systems/qotw/commands/questions_queue/AddQuestionSubcommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,17 @@ public AddQuestionSubcommand(QuestionQueueRepository questionQueueRepository, Ex
4242
}
4343

4444
@Override
45-
protected InteractionCallbackAction<?> handleCommand(SlashCommandInteractionEvent event, long guildId) {
45+
protected InteractionCallbackAction<?> handleCommand(@NotNull SlashCommandInteractionEvent event, long guildId) {
4646
return event.replyModal(buildQuestionModal());
4747
}
4848

49-
private Modal buildQuestionModal() {
49+
private @NotNull Modal buildQuestionModal() {
5050
TextInput priorityField = TextInput.create("priority", "Priority (Leave blank for default)", TextInputStyle.SHORT)
5151
.setRequired(false)
5252
.setValue("0")
5353
.build();
54-
5554
TextInput questionField = TextInput.create("question", "Question Text", TextInputStyle.PARAGRAPH)
56-
.setMaxLength(1024)
55+
.setMaxLength(85)
5756
.build();
5857

5958
return Modal.create("qotw-add-question", "Create QOTW Question")

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
import net.dv8tion.jda.api.entities.MessageType;
1010
import net.dv8tion.jda.api.entities.User;
1111
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
12-
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
1312
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1413
import net.dv8tion.jda.api.interactions.InteractionHook;
1514
import net.dv8tion.jda.api.interactions.components.ActionRow;
1615
import net.dv8tion.jda.api.interactions.components.buttons.Button;
17-
import net.dv8tion.jda.api.requests.RestAction;
1816
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
1917
import net.javadiscord.javabot.data.config.guild.QOTWConfig;
2018
import net.javadiscord.javabot.systems.notification.NotificationService;
@@ -30,7 +28,6 @@
3028
import org.springframework.transaction.annotation.Transactional;
3129

3230
import java.time.Instant;
33-
import java.util.ArrayList;
3431
import java.util.Comparator;
3532
import java.util.HashMap;
3633
import java.util.List;
@@ -39,7 +36,6 @@
3936
import java.util.concurrent.CompletableFuture;
4037
import java.util.concurrent.ExecutorService;
4138
import java.util.function.Consumer;
42-
import java.util.stream.Collectors;
4339

4440
/**
4541
* Handles & manages QOTW Submissions by using Discords {@link ThreadChannel}s.
@@ -185,7 +181,8 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
185181
WebhookUtil.ensureWebhookExists(newestPost.getParentChannel().asForumChannel(), wh ->
186182
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong()));
187183
}
188-
}).thenAccept(v -> newestPost.sendMessageEmbeds(buildAuthorEmbed(author)).queue());
184+
newestPost.sendMessageEmbeds(buildAuthorEmbed(author, bestAnswer)).queue();
185+
});
189186
}
190187
thread.getManager().setLocked(true).setArchived(true).queue();
191188
}
@@ -199,8 +196,7 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
199196
*/
200197
public void declineSubmission(InteractionHook hook, @NotNull ThreadChannel thread, User author) {
201198
thread.getManager().setName(SUBMISSION_DECLINED + thread.getName().substring(1)).queue();
202-
// TODO: fix reason
203-
notificationService.withQOTW(thread.getGuild(), author).sendSubmissionDeclinedEmbed("EMPTY_REASON");
199+
notificationService.withQOTW(thread.getGuild(), author).sendSubmissionDeclinedEmbed();
204200
Responses.success(hook, "Submission Declined", "Successfully declined submission by " + author.getAsMention()).queue();
205201
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(author, getOrRetrieveSubmission(thread), SubmissionStatus.DECLINE);
206202
thread.getManager().setLocked(true).setArchived(true).queue();
@@ -213,9 +209,10 @@ private CompletableFuture<List<Message>> getMessagesByUser(@NotNull ThreadChanne
213209
.thenApply(list -> list.stream().filter(m -> m.getAuthor().equals(user)).toList());
214210
}
215211

216-
private @NotNull MessageEmbed buildAuthorEmbed(User user) {
212+
private @NotNull MessageEmbed buildAuthorEmbed(@NotNull User user, boolean bestAnswer) {
217213
return new EmbedBuilder()
218-
.setAuthor("Submission from " + user.getAsTag(), null, user.getAvatarUrl())
214+
.setAuthor((bestAnswer ? "\u2B50 " : "") + "Submission from " + user.getAsTag(), null, user.getAvatarUrl())
215+
.setColor(bestAnswer ? Responses.Type.WARN.getColor() : Responses.Type.DEFAULT.getColor())
219216
.build();
220217
}
221218

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/subcommands/QOTWReviewSubcommand.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.jetbrains.annotations.NotNull;
1919
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
2020

21+
import java.util.Comparator;
22+
import java.util.Optional;
2123
import java.util.concurrent.ExecutorService;
2224

2325
/**
@@ -82,6 +84,14 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
8284
} else {
8385
manager.declineSubmission(event.getHook(), submissionThread, author);
8486
}
87+
if (qotwConfig.getSubmissionChannel().getThreadChannels().size() - 1 <= 0) {
88+
Optional<ThreadChannel> newestPostOptional = qotwConfig.getSubmissionsForumChannel().getThreadChannels()
89+
.stream().max(Comparator.comparing(ThreadChannel::getTimeCreated));
90+
newestPostOptional.ifPresent(p -> {
91+
p.getManager().setAppliedTags().queue();
92+
notificationService.withGuild(qotwConfig.getGuild()).sendToModerationLog(log -> log.sendMessageFormat("All submissions have been reviewed!"));
93+
});
94+
}
8595
});
8696
}
8797
}

0 commit comments

Comments
 (0)