From 3d6c7f72de7ff208f311ae6466c58de3ea0a7424 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 18 Jun 2023 14:00:02 +0300 Subject: [PATCH] Fix webhook support --- build.gradle | 3 ++- .../discordsrv/common/discord/api/DiscordAPIImpl.java | 10 ++++++---- .../channel/AbstractDiscordGuildMessageChannel.java | 6 +++++- .../api/entity/channel/DiscordThreadChannelImpl.java | 3 ++- .../api/entity/message/ReceivedDiscordMessageImpl.java | 2 +- settings.gradle | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 0e657d97..16a22784 100644 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,8 @@ subprojects { includeGroup 'me.scarsz' includeGroup 'me.minecraftauth' - includeGroup 'org.spongepowered' // Configurate yamlbranch + includeGroup 'org.spongepowered' // SpongePowered/Configurate feature/yaml-improvements branch + includeGroup 'net.dv8tion' // DiscordSRV/JDA v5-webhooks branch } } maven { diff --git a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java index e283f31f..a2f72e81 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java @@ -56,6 +56,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.concurrent.*; import java.util.function.BiConsumer; @@ -523,10 +524,11 @@ public class DiscordAPIImpl implements DiscordAPI { return textChannel.createWebhook("DSRV").submit(); }).thenApply(webhook -> - WebhookClientBuilder.fromJDA(webhook) - .setHttpClient(jda.getHttpClient()) - .setExecutorService(discordSRV.scheduler().scheduledExecutorService()) - .build() + WebhookClient.createClient( + webhook.getJDA(), + webhook.getId(), + Objects.requireNonNull(webhook.getToken()) + ) ); } } diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/AbstractDiscordGuildMessageChannel.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/AbstractDiscordGuildMessageChannel.java index df79522d..6ef90cdb 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/AbstractDiscordGuildMessageChannel.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/AbstractDiscordGuildMessageChannel.java @@ -91,7 +91,11 @@ public abstract class AbstractDiscordGuildMessageChannel createRequest; if (message.isWebhookMessage()) { - createRequest = queryWebhookClient().thenApply(client -> (R) client.sendMessage(createData)); + createRequest = queryWebhookClient() + .thenApply(client -> (R) client.sendMessage(createData) + .setUsername(message.getWebhookUsername()) + .setAvatarUrl(message.getWebhookAvatarUrl()) + ); } else { createRequest = CompletableFuture.completedFuture(((R) channel.sendMessage(createData))); } diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/DiscordThreadChannelImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/DiscordThreadChannelImpl.java index 69b41bc4..fbd8803c 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/DiscordThreadChannelImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/channel/DiscordThreadChannelImpl.java @@ -29,6 +29,7 @@ import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.attribute.IThreadContainer; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.internal.requests.IncomingWebhookClient; import org.jetbrains.annotations.NotNull; import java.util.concurrent.CompletableFuture; @@ -52,7 +53,7 @@ public class DiscordThreadChannelImpl extends AbstractDiscordGuildMessageChannel public CompletableFuture> queryWebhookClient() { return discordSRV.discordAPI() .queryWebhookClient(getParentChannel().getId()) - .thenApply(client -> null/* client.onThread(getId())*/); // TODO + .thenApply(client -> ((IncomingWebhookClient) client).withThreadId(Long.toUnsignedString(getId()))); } @Override diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java index 7d4ef528..2ea9cc79 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java @@ -76,7 +76,7 @@ public class ReceivedDiscordMessageImpl implements ReceivedDiscordMessage { ); if (clientFuture != null) { - long clientId = clientFuture.join().getId(); // TODO + long clientId = clientFuture.join().getIdLong(); self = clientId == user.getId(); } } else { diff --git a/settings.gradle b/settings.gradle index 1dee920a..0834adf6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -56,7 +56,7 @@ dependencyResolutionManagement { library('findbugs-annotations', 'com.google.code.findbugs', 'jsr305').version('3.0.2') // JDA - library('jda', 'net.dv8tion', 'JDA').version('5.0.0-beta.10') + library('jda', 'net.dv8tion', 'JDA').version('5.0.0-beta.10.webhooks_2') library('okhttp', 'com.squareup.okhttp3', 'okhttp').version { prefer '3.14.9' reject '[4,)' // Kotlin