Skip to content

Commit f307da9

Browse files
committedJul 24, 2022
Added Webhook button (doesnt work atm)
1 parent b9c242d commit f307da9

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
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:682d3dee98")
4545

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

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
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;
56
import net.dv8tion.jda.api.entities.*;
67
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
78
import net.dv8tion.jda.api.hooks.ListenerAdapter;
8-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
99
import net.dv8tion.jda.api.requests.RestAction;
1010
import net.javadiscord.javabot.util.ExceptionLogger;
11-
import net.javadiscord.javabot.util.InteractionUtils;
12-
import net.javadiscord.javabot.util.Responses;
1311
import net.javadiscord.javabot.util.WebhookUtil;
1412
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1514

15+
import java.net.MalformedURLException;
16+
import java.net.URL;
1617
import java.util.Arrays;
1718
import java.util.Optional;
1819
import java.util.regex.Matcher;
@@ -33,12 +34,20 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
3334
Optional<RestAction<Message>> optional = parseMessageUrl(matcher.group(), event.getJDA());
3435
optional.ifPresent(action -> action.queue(
3536
m -> WebhookUtil.ensureWebhookExists(event.getChannel().asTextChannel(),
36-
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw() + "\n\n" + m.getJumpUrl(), 0)
37+
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), 0, ActionRow.of(Button.link(toURL(m.getJumpUrl()), "Jump to Message")))
3738
), ExceptionLogger::capture
3839
));
3940
}
4041
}
4142

43+
private @Nullable URL toURL(String url) {
44+
try {
45+
return new URL(url);
46+
} catch (MalformedURLException e) {
47+
return null;
48+
}
49+
}
50+
4251
/**
4352
* Tries to parse a Discord Message Link to the corresponding Message object.
4453
*

‎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)
Please sign in to comment.