Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message callback response message handling #2715

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;

Expand All @@ -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;
}
}
}
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,39 @@ 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");
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;
}
});
}
}
}
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
Loading