Skip to content

Commit 0d51e76

Browse files
Merge pull request #321 from Java-Discord/dynxsty/webhook_message_links
User Webhook Message Links
2 parents ebfe7d0 + 2e525d6 commit 0d51e76

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141
implementation("org.quartz-scheduler:quartz:2.3.2")
4242

4343
// Webhooks
44-
implementation("club.minnced:discord-webhooks:0.8.0")
44+
implementation("com.github.DynxstyGIT:discord-webhooks:74301a46a0")
4545

4646
// Lombok Annotations
4747
compileOnly("org.projectlombok:lombok:1.18.24")

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

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package net.javadiscord.javabot.listener;
22

3-
import net.dv8tion.jda.api.EmbedBuilder;
3+
import club.minnced.discord.webhook.send.component.ActionRow;
4+
import club.minnced.discord.webhook.send.component.Button;
45
import net.dv8tion.jda.api.JDA;
5-
import net.dv8tion.jda.api.entities.*;
6+
import net.dv8tion.jda.api.entities.Guild;
7+
import net.dv8tion.jda.api.entities.GuildChannel;
8+
import net.dv8tion.jda.api.entities.Message;
9+
import net.dv8tion.jda.api.entities.MessageChannel;
610
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
711
import net.dv8tion.jda.api.hooks.ListenerAdapter;
8-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
912
import net.dv8tion.jda.api.requests.RestAction;
10-
import net.javadiscord.javabot.util.InteractionUtils;
11-
import net.javadiscord.javabot.util.Responses;
13+
import net.javadiscord.javabot.util.ExceptionLogger;
14+
import net.javadiscord.javabot.util.WebhookUtil;
1215
import org.jetbrains.annotations.NotNull;
1316

1417
import java.util.Arrays;
@@ -28,35 +31,23 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
2831
if (event.getAuthor().isBot() || event.getAuthor().isSystem()) return;
2932
Matcher matcher = MESSAGE_URL_PATTERN.matcher(event.getMessage().getContentRaw());
3033
if (matcher.find()) {
31-
Optional<RestAction<Message>> optional = this.parseMessageUrl(matcher.group(), event.getJDA());
34+
Optional<RestAction<Message>> optional = parseMessageUrl(matcher.group(), event.getJDA());
3235
optional.ifPresent(action -> action.queue(
33-
m -> event.getMessage().replyEmbeds(this.buildUrlEmbed(m))
34-
.setActionRow(Button.secondary(InteractionUtils.DELETE_ORIGINAL_TEMPLATE, "\uD83D\uDDD1️"), Button.link(m.getJumpUrl(), "View Original"))
35-
.queue(),
36-
e -> {}
36+
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+
), ExceptionLogger::capture
3739
));
3840
}
3941
}
4042

41-
private MessageEmbed buildUrlEmbed(Message m) {
42-
User author = m.getAuthor();
43-
return new EmbedBuilder()
44-
.setAuthor(author.getAsTag(), m.getJumpUrl(), author.getEffectiveAvatarUrl())
45-
.setColor(Responses.Type.DEFAULT.getColor())
46-
.setDescription(m.getContentRaw())
47-
.setTimestamp(m.getTimeCreated())
48-
.setFooter("#" + m.getChannel().getName())
49-
.build();
50-
}
51-
5243
/**
5344
* Tries to parse a Discord Message Link to the corresponding Message object.
5445
*
5546
* @param url The Message Link.
5647
* @param jda The {@link JDA} instance.
5748
* @return An {@link Optional} containing the {@link RestAction} which retrieves the corresponding Message.
5849
*/
59-
private Optional<RestAction<Message>> parseMessageUrl(String url, JDA jda) {
50+
private Optional<RestAction<Message>> parseMessageUrl(@NotNull String url, @NotNull JDA jda) {
6051
RestAction<Message> optional = null;
6152
String[] arr = url.split("/");
6253
String[] segments = Arrays.copyOfRange(arr, 4, arr.length);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import club.minnced.discord.webhook.external.JDAWebhookClient;
55
import club.minnced.discord.webhook.send.AllowedMentions;
66
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
7+
import club.minnced.discord.webhook.send.component.Button;
8+
import club.minnced.discord.webhook.send.component.LayoutComponent;
79
import net.dv8tion.jda.api.entities.Message;
810
import net.dv8tion.jda.api.entities.Message.Attachment;
911
import net.dv8tion.jda.api.entities.TextChannel;
@@ -70,14 +72,16 @@ public static void ensureWebhookExists(@NotNull TextChannel channel, Consumer<?
7072
* @return a {@link CompletableFuture} representing the action of sending
7173
* the message
7274
*/
73-
public static CompletableFuture<Void> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId) {
75+
public static CompletableFuture<Void> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, LayoutComponent... components) {
7476
JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken())
7577
.setThreadId(threadId).buildJDA();
7678
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent)
7779
.setAllowedMentions(AllowedMentions.none())
7880
.setAvatarUrl(originalMessage.getMember().getEffectiveAvatarUrl())
7981
.setUsername(originalMessage.getMember().getEffectiveName());
80-
82+
if (components.length > 0) {
83+
message.addComponents(components);
84+
}
8185
List<Attachment> attachments = originalMessage.getAttachments();
8286
@SuppressWarnings("unchecked")
8387
CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()];

0 commit comments

Comments
 (0)