Skip to content

Commit 1aefd52

Browse files
Merge pull request #381 from Java-Discord/dynxsty/fix_qotw_embeds
Attach Embed to Submission Webhook Mirror
2 parents 71b91d1 + 7b5ec4d commit 1aefd52

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

src/main/java/net/javadiscord/javabot/listener/HugListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private char copyCase(String original, int index, char newChar) {
9696
}
9797

9898
private void sendWebhookMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId) {
99-
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId)
99+
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId, null, null)
100100
.thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> {
101101
log.error("replacing the content 'fuck' with 'hug' in an incoming message failed", e);
102102
return null;

src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.jetbrains.annotations.NotNull;
1616

1717
import java.util.Arrays;
18+
import java.util.List;
1819
import java.util.Optional;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
@@ -34,7 +35,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
3435
Optional<RestAction<Message>> optional = parseMessageUrl(matcher.group(), event.getJDA());
3536
optional.ifPresent(action -> action.queue(
3637
m -> WebhookUtil.ensureWebhookExists(event.getChannel().asTextChannel(),
37-
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), 0, ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message")))
38+
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), 0, List.of(ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message"))), null)
3839
), e -> ExceptionLogger.capture(e, getClass().getSimpleName())
3940
));
4041
}

src/main/java/net/javadiscord/javabot/systems/moderation/report/ReportManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void handleMessageReport(ModalInteractionEvent event, String messageId)
159159
MessageChannel reportChannel = config.getModerationConfig().getReportChannel();
160160
reportChannel.sendMessageEmbeds(embed.build()).queue(m -> createReportThread(m, target.getAuthor().getIdLong(), config.getModerationConfig(), thread->{
161161
WebhookUtil.ensureWebhookExists(thread.getParentChannel().asStandardGuildMessageChannel(), wh->{
162-
WebhookUtil.mirrorMessageToWebhook(wh, target, target.getContentRaw(), thread.getIdLong());
162+
WebhookUtil.mirrorMessageToWebhook(wh, target, target.getContentRaw(), thread.getIdLong(), null, null);
163163
});
164164
}));
165165
embed.setDescription("Successfully reported " + "`" + target.getAuthor().getAsTag() + "`!\nYour report has been send to our Moderators");

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread
148148
WebhookUtil.ensureWebhookExists(newestPost.getParentChannel().asForumChannel(), wh ->
149149
getMessagesByUser(thread, author).thenAccept(messages -> {
150150
for (Message message : messages) {
151+
boolean lastMessage = messages.indexOf(message) + 1 == messages.size();
151152
if (message.getAuthor().isBot() || message.getType() != MessageType.DEFAULT) continue;
152153
if (message.getContentRaw().length() > 2000) {
153-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(0, 2000), newestPost.getIdLong());
154-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(2000), newestPost.getIdLong());
154+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(0, 2000), newestPost.getIdLong(), null, null);
155+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw().substring(2000), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, bestAnswer)) : null);
155156
} else {
156-
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong());
157+
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, bestAnswer)) : null);
157158
}
158159
}
159-
newestPost.sendMessageEmbeds(buildAuthorEmbed(author, bestAnswer)).queue();
160160
}));
161161
}
162162
thread.getManager().setLocked(true).setArchived(true).queue();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void copyMessagesToNewThread(StandardGuildMessageChannel targetCha
9292
thread -> WebhookUtil.ensureWebhookExists(targetChannel, wh->{
9393
CompletableFuture<ReadonlyMessage> future = CompletableFuture.completedFuture(null);
9494
for (Message m : messages) {
95-
future = future.thenCompose(unused -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong()));
95+
future = future.thenCompose(unused -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong(), null, null));
9696
}
9797
future.thenAccept(unused -> onFinish.accept(thread));
9898
})

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import club.minnced.discord.webhook.external.JDAWebhookClient;
55
import club.minnced.discord.webhook.receive.ReadonlyMessage;
66
import club.minnced.discord.webhook.send.AllowedMentions;
7+
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
78
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
89
import club.minnced.discord.webhook.send.component.LayoutComponent;
910
import net.dv8tion.jda.api.entities.Message;
1011
import net.dv8tion.jda.api.entities.Message.Attachment;
12+
import net.dv8tion.jda.api.entities.MessageEmbed;
1113
import net.dv8tion.jda.api.entities.Webhook;
1214
import net.dv8tion.jda.api.entities.channel.attribute.IWebhookContainer;
1315
import org.jetbrains.annotations.NotNull;
16+
import org.jetbrains.annotations.Nullable;
1417

1518
import java.util.List;
1619
import java.util.Optional;
@@ -28,7 +31,7 @@ private WebhookUtil() {
2831
* Makes sure that a writable webhook exists in a specific channel. if no
2932
* suitable webhook is found, one is created.
3033
*
31-
* @param channel the {@link StandardGuildMessageChannel} the webhook should exist in
34+
* @param channel the {@link IWebhookContainer} the webhook should exist in
3235
* @param callback an action that is executed once a webhook is
3336
* found/created
3437
*/
@@ -40,7 +43,7 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
4043
* Makes sure that a writable webhook exists in a specific channel. if no
4144
* suitable webhook is found, one is created.
4245
*
43-
* @param channel the {@link StandardGuildMessageChannel} the webhook should exist in
46+
* @param channel the {@link IWebhookContainer} the webhook should exist in
4447
* @param callback an action that is executed once a webhook is
4548
* found/created
4649
* @param failureCallback an action that is executed if the webhook
@@ -68,20 +71,24 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
6871
* @param newMessageContent the new (custom) content
6972
* @param threadId the thread to send the message in or {@code 0} if the
7073
* message should be sent directly
71-
* @param components An optional array of {@link LayoutComponent}s.
74+
* @param components A nullable list of {@link LayoutComponent}s.
75+
* @param embeds A nullable list of {@link MessageEmbed}s.
7276
* @return a {@link CompletableFuture} representing the action of sending
7377
* the message
7478
*/
75-
public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, LayoutComponent @NotNull ... components) {
79+
public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, @Nullable List<LayoutComponent> components, @Nullable List<MessageEmbed> embeds) {
7680
JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken())
7781
.setThreadId(threadId).buildJDA();
7882
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent)
7983
.setAllowedMentions(AllowedMentions.none())
8084
.setAvatarUrl(originalMessage.getMember().getEffectiveAvatarUrl())
8185
.setUsername(originalMessage.getMember().getEffectiveName());
82-
if (components.length > 0) {
86+
if (components != null && !components.isEmpty()) {
8387
message.addComponents(components);
8488
}
89+
if (embeds != null && !embeds.isEmpty()) {
90+
message.addEmbeds(embeds.stream().map(e -> WebhookEmbedBuilder.fromJDA(e).build()).toList());
91+
}
8592
List<Attachment> attachments = originalMessage.getAttachments();
8693
@SuppressWarnings("unchecked")
8794
CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()];

0 commit comments

Comments
 (0)