Skip to content

Commit

Permalink
Add message callback response message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 18, 2024
1 parent a8bf714 commit dfb781e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -72,6 +73,15 @@ public interface InteractionHook extends WebhookClient<Message>
@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.
* <br>An interaction hook expires after 15 minutes of its creation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class InteractionHookImpl extends AbstractWebhookClient<Message> implemen
private Exception exception;
private boolean isReady;
private boolean ephemeral;
private Message callbackResponseMessage;

public InteractionHookImpl(@Nonnull DeferrableInteractionImpl interaction, @Nonnull JDA api)
{
Expand Down Expand Up @@ -126,6 +127,12 @@ else if (exception != null)
});
}

public InteractionHookImpl setCallbackResponseMessage(Message message)
{
this.callbackResponseMessage = message;
return this;
}

@Nonnull
@Override
public InteractionImpl getInteraction()
Expand All @@ -135,6 +142,12 @@ public InteractionImpl getInteraction()
return interaction;
}

@Override
public Message getCallbackResponseMessage()
{
return callbackResponseMessage;
}

@Override
public long getExpirationTimestamp()
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InteractionHook>
{
Expand All @@ -41,6 +44,32 @@ protected void handleSuccess(Response response, Request<InteractionHook> 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));
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public abstract class InteractionCallbackImpl<T> extends RestActionImpl<T> 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);
}
Expand Down

0 comments on commit dfb781e

Please sign in to comment.