Skip to content

Commit c6f4415

Browse files
committed
fix order of webhook messages for qotw answer threads
1 parent ca34cbc commit c6f4415

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/main/java/net/javadiscord/javabot/util/MessageActionUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.jetbrains.annotations.NotNull;
99

10+
import club.minnced.discord.webhook.receive.ReadonlyMessage;
1011
import net.dv8tion.jda.api.entities.Message;
1112
import net.dv8tion.jda.api.entities.MessageEmbed;
1213
import net.dv8tion.jda.api.entities.StandardGuildMessageChannel;
@@ -89,9 +90,10 @@ public static void copyMessagesToNewThread(StandardGuildMessageChannel targetCha
8990
message -> message.createThreadChannel(newThreadName).queue(
9091
thread -> {
9192
WebhookUtil.ensureWebhookExists(targetChannel, wh->{
92-
messages.forEach(m -> {
93-
WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong());
94-
});
93+
CompletableFuture<ReadonlyMessage> future = CompletableFuture.completedFuture(null);
94+
for (Message m : messages) {
95+
future = future.thenCompose(unused -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong()));
96+
}
9597
});
9698
onFinish.accept(thread);
9799
}

src/main/java/net/javadiscord/javabot/util/WebhookUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import club.minnced.discord.webhook.WebhookClientBuilder;
44
import club.minnced.discord.webhook.external.JDAWebhookClient;
5+
import club.minnced.discord.webhook.receive.ReadonlyMessage;
56
import club.minnced.discord.webhook.send.AllowedMentions;
67
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
78
import club.minnced.discord.webhook.send.component.LayoutComponent;
@@ -72,7 +73,7 @@ public static void ensureWebhookExists(@NotNull StandardGuildMessageChannel chan
7273
* @return a {@link CompletableFuture} representing the action of sending
7374
* the message
7475
*/
75-
public static CompletableFuture<Void> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, LayoutComponent @NotNull ... components) {
76+
public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, LayoutComponent @NotNull ... components) {
7677
JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken())
7778
.setThreadId(threadId).buildJDA();
7879
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent)
@@ -90,7 +91,7 @@ public static CompletableFuture<Void> mirrorMessageToWebhook(@NotNull Webhook we
9091
futures[i] = attachment.getProxy().download().thenAccept(
9192
is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is));
9293
}
93-
return CompletableFuture.allOf(futures).thenAccept(unused -> client.send(message.build()))
94+
return CompletableFuture.allOf(futures).thenCompose(unused -> client.send(message.build()))
9495
.whenComplete((result, err) -> client.close());
9596
}
9697
}

0 commit comments

Comments
 (0)