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/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..e8ee87f5c4 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,32 @@ 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"); + if (resource.getInt("type", -1) == 4) + { + DataObject message = resource.getObject("message"); + hook.setCallbackResponseMessage(hook.buildMessage(message)); + } + }); + } + } } 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); }