Skip to content

Commit f011b27

Browse files
authored
Merge pull request #399 from danthe1st/fix-empty-webhooks
fix embeds for webhooks
2 parents 32d1e9b + 30327db commit f011b27

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
88
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
99
import club.minnced.discord.webhook.send.component.LayoutComponent;
10+
import net.dv8tion.jda.api.entities.Member;
1011
import net.dv8tion.jda.api.entities.Message;
1112
import net.dv8tion.jda.api.entities.Message.Attachment;
1213
import net.dv8tion.jda.api.entities.MessageEmbed;
@@ -19,6 +20,7 @@
1920
import java.util.Optional;
2021
import java.util.concurrent.CompletableFuture;
2122
import java.util.function.Consumer;
23+
import java.util.function.Function;
2224

2325
/**
2426
* Contains utility methods for dealing with Discord Webhooks.
@@ -90,14 +92,16 @@ public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull
9092
.setThreadId(threadId).buildJDA();
9193
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent)
9294
.setAllowedMentions(AllowedMentions.none())
93-
.setAvatarUrl(originalMessage.getMember().getEffectiveAvatarUrl())
94-
.setUsername(originalMessage.getMember().getEffectiveName());
95+
.setAvatarUrl(transformOrNull(originalMessage.getMember(), Member::getEffectiveAvatarUrl))
96+
.setUsername(transformOrNull(originalMessage.getMember(), Member::getEffectiveName));
9597
if (components != null && !components.isEmpty()) {
9698
message.addComponents(components);
9799
}
98-
if (embeds != null && !embeds.isEmpty()) {
99-
message.addEmbeds(embeds.stream().map(e -> WebhookEmbedBuilder.fromJDA(e).build()).toList());
100+
101+
if (embeds == null || embeds.isEmpty()) {
102+
embeds = originalMessage.getEmbeds();
100103
}
104+
message.addEmbeds(embeds.stream().map(e -> WebhookEmbedBuilder.fromJDA(e).build()).toList());
101105
List<Attachment> attachments = originalMessage.getAttachments();
102106
@SuppressWarnings("unchecked")
103107
CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()];
@@ -106,12 +110,23 @@ public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull
106110
futures[i] = attachment.getProxy().download().thenAccept(
107111
is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is));
108112
}
109-
return CompletableFuture.allOf(futures).thenCompose(unused -> client.send(message.build()))
113+
return CompletableFuture.allOf(futures).thenCompose(unused -> sendMessage(client, message))
110114
.whenComplete((result, err) -> {
111115
client.close();
112116
if( err != null) {
113117
ExceptionLogger.capture(err, WebhookUtil.class.getSimpleName());
114118
}
115119
});
116120
}
121+
122+
private static <T, R> R transformOrNull(T toTransform, Function<T, R> transformer) {
123+
return toTransform == null ? null : transformer.apply(toTransform);
124+
}
125+
126+
private static @NotNull CompletableFuture<ReadonlyMessage> sendMessage(JDAWebhookClient client, WebhookMessageBuilder message) {
127+
if(message.isEmpty()) {
128+
message.setContent("<empty message>");
129+
}
130+
return client.send(message.build());
131+
}
117132
}

0 commit comments

Comments
 (0)