Skip to content

Commit 361aab9

Browse files
committed
QOTW sample answers
1 parent b5c9859 commit 361aab9

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class QOTWConfig extends GuildConfigItem {
2323
private long questionRoleId;
2424
private long qotwReviewRoleId;
2525
private long qotwChampionRoleId;
26+
private long qotwSampleAnswerUserId;
2627
private String submissionForumOngoingReviewTagName = "";
2728

2829
public NewsChannel getQuestionChannel() {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@
2323
import net.javadiscord.javabot.data.config.GuildConfig;
2424
import net.javadiscord.javabot.data.config.SystemsConfig;
2525
import net.javadiscord.javabot.data.config.guild.QOTWConfig;
26+
import net.javadiscord.javabot.systems.notification.NotificationService;
27+
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
2628
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
2729
import net.javadiscord.javabot.systems.qotw.model.QOTWQuestion;
2830
import net.javadiscord.javabot.systems.qotw.model.QOTWSubmission;
31+
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionManager;
2932
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionStatus;
3033
import org.jetbrains.annotations.NotNull;
3134
import org.springframework.scheduling.annotation.Scheduled;
3235
import org.springframework.stereotype.Service;
3336
import xyz.dynxsty.dih4jda.util.ComponentIdBuilder;
3437

3538
import javax.annotation.Nonnull;
39+
3640
import java.sql.SQLException;
3741
import java.util.Collections;
3842
import java.util.List;
@@ -50,6 +54,8 @@ public class QOTWCloseSubmissionsJob {
5054
private final JDA jda;
5155
private final QuestionQueueRepository questionQueueRepository;
5256
private final ExecutorService asyncPool;
57+
private final QOTWPointsService pointsService;
58+
private final NotificationService notificationService;
5359
private final BotConfig botConfig;
5460

5561
/**
@@ -83,10 +89,15 @@ public void execute() throws SQLException {
8389
final QOTWSubmission s = new QOTWSubmission(submission);
8490
s.retrieveAuthor(author -> {
8591
submission.removeThreadMember(author).queue();
86-
thread
87-
.sendMessage("%s by %s".formatted(submission.getAsMention(), author.getAsMention()))
88-
.addActionRow(buildSubmissionSelectMenu(jda, submission.getIdLong()))
89-
.queue();
92+
if (author.getIdLong() == qotwConfig.getQotwSampleAnswerUserId()) {
93+
SubmissionManager manager = new SubmissionManager(botConfig.get(guild).getQotwConfig(), pointsService, questionQueueRepository, notificationService, asyncPool);
94+
manager.copySampleAnswerSubmission(submission, author);
95+
} else {
96+
thread
97+
.sendMessage("%s by %s".formatted(submission.getAsMention(), author.getAsMention()))
98+
.addActionRow(buildSubmissionSelectMenu(jda, submission.getIdLong()))
99+
.queue();
100+
}
90101
});
91102
for (Member member : guild.getMembersWithRoles(qotwConfig.getQOTWReviewRole())) {
92103
thread.addThreadMember(member).queue();

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.javadiscord.javabot.systems.qotw.submissions;
22

3+
import lombok.Getter;
34
import lombok.RequiredArgsConstructor;
45
import lombok.extern.slf4j.Slf4j;
56
import net.dv8tion.jda.api.EmbedBuilder;
@@ -29,6 +30,7 @@
2930
import org.jetbrains.annotations.NotNull;
3031
import org.springframework.transaction.annotation.Transactional;
3132

33+
import java.awt.Color;
3234
import java.time.Instant;
3335
import java.util.Comparator;
3436
import java.util.List;
@@ -198,6 +200,14 @@ public void acceptSubmission(@NotNull ThreadChannel thread, @NotNull User author
198200
notificationService.withQOTW(thread.getGuild(), author).sendBestAnswerNotification();
199201
}
200202
notificationService.withQOTW(thread.getGuild()).sendSubmissionActionNotification(reviewedBy.getUser(), new QOTWSubmission(thread), bestAnswer ? SubmissionStatus.ACCEPT_BEST : SubmissionStatus.ACCEPT);
203+
sendToQOTWAnswerArchive(thread, author, bestAnswer?AcceptedAnswerType.BEST_ANSWER:AcceptedAnswerType.ACCEPTED_ANSWER);
204+
}
205+
206+
public void copySampleAnswerSubmission(@NotNull ThreadChannel thread, @NotNull User author) {
207+
sendToQOTWAnswerArchive(thread, author, AcceptedAnswerType.SAMPLE_ANSWER);
208+
}
209+
210+
private void sendToQOTWAnswerArchive(ThreadChannel thread, User author, AcceptedAnswerType type) {
201211
Optional<ThreadChannel> newestPostOptional = config.getSubmissionsForumChannel().getThreadChannels()
202212
.stream().max(Comparator.comparing(ThreadChannel::getTimeCreated));
203213
if (newestPostOptional.isPresent()) {
@@ -209,9 +219,9 @@ public void acceptSubmission(@NotNull ThreadChannel thread, @NotNull User author
209219
if (message.getAuthor().isBot() || message.getType() != MessageType.DEFAULT) continue;
210220
if (message.getContentRaw().length() > 2000) {
211221
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(0, 2000), newestPost.getIdLong(), null, null).join();
212-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(2000), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, bestAnswer)) : null).join();
222+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(2000), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, type)) : null).join();
213223
} else {
214-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, bestAnswer)) : null).join();
224+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, type)) : null).join();
215225
}
216226
}
217227
}).exceptionally(err->{
@@ -222,6 +232,15 @@ public void acceptSubmission(@NotNull ThreadChannel thread, @NotNull User author
222232
thread.getManager().setLocked(true).setArchived(true).queue();
223233
}
224234

235+
@RequiredArgsConstructor
236+
@Getter
237+
private enum AcceptedAnswerType{
238+
SAMPLE_ANSWER(Responses.Type.DEFAULT.getColor(), "\uD83D\uDCD6 Sample answer"), ACCEPTED_ANSWER(Responses.Type.DEFAULT.getColor(),"Submission"), BEST_ANSWER(Responses.Type.WARN.getColor(), "\u2B50 Submission");
239+
240+
private final Color color;
241+
private final String prefix;
242+
}
243+
225244
/**
226245
* Declines a submission.
227246
*
@@ -244,10 +263,10 @@ private CompletableFuture<List<Message>> getMessagesByUser(@NotNull ThreadChanne
244263
.thenApply(list -> list.stream().filter(m -> m.getAuthor().equals(user)).toList());
245264
}
246265

247-
private @NotNull MessageEmbed buildAuthorEmbed(@NotNull User user, boolean bestAnswer) {
266+
private @NotNull MessageEmbed buildAuthorEmbed(@NotNull User user, AcceptedAnswerType answerType) {
248267
return new EmbedBuilder()
249-
.setAuthor((bestAnswer ? "\u2B50 " : "") + "Submission from " + UserUtils.getUserTag(user), null, user.getAvatarUrl())
250-
.setColor(bestAnswer ? Responses.Type.WARN.getColor() : Responses.Type.DEFAULT.getColor())
268+
.setAuthor(answerType.getPrefix() + " from " + UserUtils.getUserTag(user), null, user.getAvatarUrl())
269+
.setColor(answerType.getColor())
251270
.build();
252271
}
253272

0 commit comments

Comments
 (0)