From be02d6e4fe188faa15e9bb4327f11b92ddb4827e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Sun, 18 Aug 2024 18:44:50 +0200 Subject: [PATCH] Add message callback response message handling --- .../jda/api/interactions/InteractionHook.java | 10 ++++++ .../InteractionCallbackAction.java | 13 +++++++ .../interactions/InteractionHookImpl.java | 15 +++++++- .../DeferrableCallbackActionImpl.java | 36 +++++++++++++++++++ .../interactions/InteractionCallbackImpl.java | 4 ++- 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/interactions/InteractionHook.java b/src/main/java/net/dv8tion/jda/api/interactions/InteractionHook.java index ecc2dae922..ec218dfd4f 100644 --- a/src/main/java/net/dv8tion/jda/api/interactions/InteractionHook.java +++ b/src/main/java/net/dv8tion/jda/api/interactions/InteractionHook.java @@ -33,6 +33,7 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Arrays; import java.util.Collection; @@ -72,6 +73,15 @@ public interface InteractionHook extends WebhookClient @Nonnull Interaction getInteraction(); + /** + * The message created by interaction replies like {@link IReplyCallback#reply(String)} + * or interaction updates like {@link IMessageEditCallback#editMessage(String)}. + * + * @return {@link Message}, if available. + */ + @Nullable + Message getCallbackResponseMessage(); + /** * The unix millisecond timestamp for the expiration of this interaction hook. *
An interaction hook expires after 15 minutes of its creation. diff --git a/src/main/java/net/dv8tion/jda/api/requests/restaction/interactions/InteractionCallbackAction.java b/src/main/java/net/dv8tion/jda/api/requests/restaction/interactions/InteractionCallbackAction.java index 90d3b045bc..f0bced0bae 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/restaction/interactions/InteractionCallbackAction.java +++ b/src/main/java/net/dv8tion/jda/api/requests/restaction/interactions/InteractionCallbackAction.java @@ -57,6 +57,8 @@ enum ResponseType MODAL(9), /** Respond with the "Premium required" default Discord message for premium App subscriptions **/ PREMIUM_REQUIRED(10), + /** Placeholder for unknown types */ + UNKNOWN(-1), ; private final int raw; @@ -74,5 +76,16 @@ public int getRaw() { return raw; } + + @Nonnull + public static ResponseType fromId(int id) + { + for (ResponseType type : values()) + { + if (type.raw == id) + return type; + } + return UNKNOWN; + } } } diff --git a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionHookImpl.java b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionHookImpl.java index 07dba40bbf..eb9ff59011 100644 --- a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionHookImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionHookImpl.java @@ -55,6 +55,7 @@ public class InteractionHookImpl extends AbstractWebhookClient implemen private Exception exception; private boolean isReady; private boolean ephemeral; + private Message callbackResponseMessage; public InteractionHookImpl(@Nonnull DeferrableInteractionImpl interaction, @Nonnull JDA api) { @@ -126,6 +127,12 @@ else if (exception != null) }); } + public InteractionHookImpl setCallbackResponseMessage(Message message) + { + this.callbackResponseMessage = message; + return this; + } + @Nonnull @Override public InteractionImpl getInteraction() @@ -135,6 +142,12 @@ public InteractionImpl getInteraction() return interaction; } + @Override + public Message getCallbackResponseMessage() + { + return callbackResponseMessage; + } + @Override public long getExpirationTimestamp() { @@ -210,7 +223,7 @@ private boolean checkExpired() // Sometimes we can't resolve the channel and report an unknown type // Currently known cases where channels can't be resolved: // - InteractionHook created using id/token factory, has no interaction object to use as context - private Message buildMessage(DataObject json) + public Message buildMessage(DataObject json) { JDAImpl jda = (JDAImpl) api; MessageChannel channel = null; diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/DeferrableCallbackActionImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/DeferrableCallbackActionImpl.java index af0be7afc7..fdf016cf08 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/DeferrableCallbackActionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/DeferrableCallbackActionImpl.java @@ -19,7 +19,10 @@ import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.requests.Request; import net.dv8tion.jda.api.requests.Response; +import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.interactions.InteractionHookImpl; +import okhttp3.MediaType; +import okhttp3.ResponseBody; public abstract class DeferrableCallbackActionImpl extends InteractionCallbackImpl { @@ -41,6 +44,39 @@ protected void handleSuccess(Response response, Request request // we also need to provide the hook itself to the success callback of the RestAction, // so we override this functionality interaction.releaseHook(true); + parseOptionalBody(response); request.onSuccess(hook); } + + private void parseOptionalBody(Response response) + { + okhttp3.Response rawResponse = response.getRawResponse(); + if (rawResponse == null) + return; + + ResponseBody body = rawResponse.body(); + if (body == null) + return; + + MediaType mediaType = body.contentType(); + if (mediaType != null && mediaType.toString().startsWith("application/json")) + { + response.optObject().ifPresent(json -> + { + DataObject resource = json.getObject("resource"); + int type = resource.getInt("type", -1); + switch (ResponseType.fromId(type)) + { + case MESSAGE_UPDATE: + case DEFERRED_MESSAGE_UPDATE: + case CHANNEL_MESSAGE_WITH_SOURCE: + case DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE: + resource.optObject("message").ifPresent(message -> + hook.setCallbackResponseMessage(hook.buildMessage(message)) + ); + break; + } + }); + } + } } diff --git a/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/InteractionCallbackImpl.java b/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/InteractionCallbackImpl.java index 5e9ab4fa12..d29dd44c49 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/InteractionCallbackImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/restaction/interactions/InteractionCallbackImpl.java @@ -32,7 +32,9 @@ public abstract class InteractionCallbackImpl extends RestActionImpl imple public InteractionCallbackImpl(InteractionImpl interaction) { - super(interaction.getJDA(), Route.Interactions.CALLBACK.compile(interaction.getId(), interaction.getToken())); + super(interaction.getJDA(), + Route.Interactions.CALLBACK.compile(interaction.getId(), interaction.getToken()) + .withQueryParams("with_response", "true")); this.interaction = interaction; setErrorMapper(this::handleUnknownInteraction); }