33import lombok .RequiredArgsConstructor ;
44import lombok .extern .slf4j .Slf4j ;
55import net .dv8tion .jda .api .EmbedBuilder ;
6- import net .dv8tion .jda .api .entities .*;
6+ import net .dv8tion .jda .api .entities .Member ;
7+ import net .dv8tion .jda .api .entities .Message ;
8+ import net .dv8tion .jda .api .entities .MessageEmbed ;
9+ import net .dv8tion .jda .api .entities .MessageType ;
10+ import net .dv8tion .jda .api .entities .User ;
711import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
12+ import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
813import net .dv8tion .jda .api .events .interaction .component .ButtonInteractionEvent ;
914import net .dv8tion .jda .api .interactions .InteractionHook ;
1015import net .dv8tion .jda .api .interactions .components .ActionRow ;
1116import net .dv8tion .jda .api .interactions .components .buttons .Button ;
17+ import net .dv8tion .jda .api .requests .RestAction ;
1218import net .dv8tion .jda .api .requests .restaction .WebhookMessageCreateAction ;
1319import net .javadiscord .javabot .data .config .guild .QOTWConfig ;
1420import net .javadiscord .javabot .systems .notification .NotificationService ;
2430import org .springframework .transaction .annotation .Transactional ;
2531
2632import java .time .Instant ;
27- import java .util .*;
33+ import java .util .ArrayList ;
34+ import java .util .Comparator ;
35+ import java .util .HashMap ;
36+ import java .util .List ;
37+ import java .util .Map ;
38+ import java .util .Optional ;
39+ import java .util .concurrent .CompletableFuture ;
2840import java .util .concurrent .ExecutorService ;
2941import java .util .function .Consumer ;
42+ import java .util .stream .Collectors ;
3043
3144/**
3245 * Handles & manages QOTW Submissions by using Discords {@link ThreadChannel}s.
@@ -106,7 +119,7 @@ public List<QOTWSubmission> getActiveSubmissions() {
106119 /**
107120 * Handles the "Delete Submission" Button.
108121 *
109- * @param event The {@link ButtonInteractionEvent} that is fired upon use.
122+ * @param event The {@link ButtonInteractionEvent} that is fired upon use.
110123 */
111124 public void handleThreadDeletion (@ NotNull ButtonInteractionEvent event ) {
112125 config .getSubmissionChannel ().getThreadChannels ()
@@ -150,9 +163,9 @@ private boolean canCreateSubmissions(Member member) {
150163 /**
151164 * Accepts a submission.
152165 *
153- * @param hook The {@link net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent} that was fired.
154- * @param thread The submission's {@link ThreadChannel}.
155- * @param author The submissions' author.
166+ * @param hook The {@link net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent} that was fired.
167+ * @param thread The submission's {@link ThreadChannel}.
168+ * @param author The submissions' author.
156169 * @param bestAnswer Whether the submission is among the best answers for this week.
157170 */
158171 public void acceptSubmission (InteractionHook hook , @ NotNull ThreadChannel thread , @ NotNull User author , boolean bestAnswer ) {
@@ -166,11 +179,13 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
166179 .stream ().max (Comparator .comparing (ThreadChannel ::getTimeCreated ));
167180 if (newestPostOptional .isPresent ()) {
168181 ThreadChannel newestPost = newestPostOptional .get ();
169- for (Message message : getSubmissionContent (thread )) {
170- WebhookUtil .ensureWebhookExists (newestPost .getParentChannel ().asStandardGuildMessageChannel (), wh -> {
171- WebhookUtil .mirrorMessageToWebhook (wh , message , message .getContentRaw (), newestPost .getIdLong ());
172- });
173- }
182+ getMessagesByUser (thread , author ).thenAccept (messages -> {
183+ for (Message message : messages ) {
184+ if (message .getAuthor ().isBot () || message .getType () != MessageType .DEFAULT ) continue ;
185+ WebhookUtil .ensureWebhookExists (newestPost .getParentChannel ().asForumChannel (), wh ->
186+ WebhookUtil .mirrorMessageToWebhook (wh , message , message .getContentRaw (), newestPost .getIdLong ()));
187+ }
188+ }).thenAccept (v -> newestPost .sendMessageEmbeds (buildAuthorEmbed (author )).queue ());
174189 }
175190 thread .getManager ().setLocked (true ).setArchived (true ).queue ();
176191 }
@@ -191,19 +206,17 @@ public void declineSubmission(InteractionHook hook, @NotNull ThreadChannel threa
191206 thread .getManager ().setLocked (true ).setArchived (true ).queue ();
192207 }
193208
194- private @ NotNull List <Message > getSubmissionContent (@ NotNull ThreadChannel thread ) {
195- List <Message > messages = new ArrayList <>();
196- int count = thread .getMessageCount ();
197- while (count > 0 ) {
198- List <Message > retrieved = thread .getHistory ().retrievePast (Math .min (count , 100 )).complete ()
199- .stream ()
200- .filter (m -> !m .getAuthor ().isBot ())
201- .toList ();
202- messages .addAll (retrieved );
203- count -= Math .min (count , 100 );
204- }
205- Collections .reverse (messages );
206- return messages ;
209+ private CompletableFuture <List <Message >> getMessagesByUser (@ NotNull ThreadChannel channel , User user ) {
210+ return channel .getIterableHistory ()
211+ .reverse ()
212+ .takeAsync (channel .getMessageCount ())
213+ .thenApply (list -> list .stream ().filter (m -> m .getAuthor ().equals (user )).toList ());
214+ }
215+
216+ private @ NotNull MessageEmbed buildAuthorEmbed (User user ) {
217+ return new EmbedBuilder ()
218+ .setAuthor ("Submission from " + user .getAsTag (), null , user .getAvatarUrl ())
219+ .build ();
207220 }
208221
209222 private @ NotNull MessageEmbed buildSubmissionThreadEmbed (@ NotNull User createdBy , @ NotNull QOTWQuestion question , @ NotNull QOTWConfig config ) {
0 commit comments