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

Create an exception when receiving UNKNOWN_WEBHOOK in interaction hooks #2621

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 15 additions & 3 deletions src/main/java/net/dv8tion/jda/api/requests/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,27 @@ public void onFailure(Response response)
{
if (response.code == 429)
{
onFailure(new RateLimitedException(route, response.retryAfter));
onFailure(createRateLimitedException(response));
}
else
{
onFailure(ErrorResponseException.create(
ErrorResponse.fromJSON(response.optObject().orElse(null)), response));
onFailure(createErrorResponseException(response));
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Nonnull
public RateLimitedException createRateLimitedException(@Nonnull Response response)
{
return new RateLimitedException(route, response.retryAfter);
}

@Nonnull
public ErrorResponseException createErrorResponseException(@Nonnull Response response)
{
return ErrorResponseException.create(
ErrorResponse.fromJSON(response.optObject().orElse(null)), response);
}

public void onFailure(Throwable failException)
{
if (done)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.entities.sticker.StickerItem;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.exceptions.PermissionException;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.*;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
import net.dv8tion.jda.api.requests.restaction.ThreadChannelAction;
Expand All @@ -51,6 +50,7 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.interactions.InteractionHookImpl;
import net.dv8tion.jda.internal.requests.CompletedRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl;
Expand Down Expand Up @@ -774,7 +774,19 @@ public AuditableRestAction<Void> delete()
if (isWebhookRequest())
{
Route.CompiledRoute route = Route.Webhooks.EXECUTE_WEBHOOK_DELETE.compile(webhook.getId(), webhook.getToken(), getId());
return new AuditableRestActionImpl<>(getJDA(), route);
return new AuditableRestActionImpl<Void>(getJDA(), route)
{
@Override
protected void handleErrorResponse(Response response, Request<Void> request, ErrorResponseException exception)
{
if (webhook instanceof InteractionHookImpl
&& !((InteractionHookImpl) webhook).isAck()
&& exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK)
request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration"));
freya022 marked this conversation as resolved.
Show resolved Hide resolved
else
super.handleErrorResponse(response, request, exception);
}
};
}

SelfUser self = getJDA().getSelfUser();
Expand Down Expand Up @@ -839,7 +851,19 @@ public AuditableRestAction<Void> suppressEmbeds(boolean suppressed)
newFlags &= ~suppressionValue;
DataObject body = DataObject.empty().put("flags", newFlags);

return new AuditableRestActionImpl<>(api, route, body);
return new AuditableRestActionImpl<Void>(api, route, body)
{
@Override
protected void handleErrorResponse(Response response, Request<Void> request, ErrorResponseException exception)
{
if (webhook instanceof InteractionHookImpl
&& !((InteractionHookImpl) webhook).isAck()
&& exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK)
request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration"));
else
super.handleErrorResponse(response, request, exception);
}
};
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ public void handleResponse(Response response, Request<T> request)
{
if (response.isOk())
handleSuccess(response, request);
else if (response.isRateLimit())
request.onFailure(request.createRateLimitedException(response));
else
request.onFailure(response);
handleErrorResponse(response, request, request.createErrorResponseException(response));
}

protected void handleSuccess(Response response, Request<T> request)
Expand All @@ -289,6 +291,11 @@ protected void handleSuccess(Response response, Request<T> request)
request.onSuccess(handler.apply(response, request));
}

protected void handleErrorResponse(Response response, Request<T> request, ErrorResponseException exception)
{
request.onFailure(exception);
}

private long getDeadline()
{
return deadline > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import net.dv8tion.jda.api.entities.WebhookClient;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.requests.ErrorResponse;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.Route;
Expand All @@ -32,6 +34,7 @@
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.entities.ReceivedMessage;
import net.dv8tion.jda.internal.interactions.InteractionHookImpl;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.utils.message.MessageEditBuilderMixin;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -103,6 +106,17 @@ protected void handleSuccess(Response response, Request<Message> request)
request.onSuccess(message.withHook(webhook));
}

@Override
protected void handleErrorResponse(Response response, Request<Message> request, ErrorResponseException exception)
{
if (webhook instanceof InteractionHookImpl
&& !((InteractionHookImpl) webhook).isAck()
&& exception.getErrorResponse() == ErrorResponse.UNKNOWN_WEBHOOK)
request.onFailure(new IllegalStateException("Sending a webhook request requires the interaction to be acknowledged before expiration"));
else
super.handleErrorResponse(response, request, exception);
}

@Nonnull
@Override
public MessageEditAction setCheck(BooleanSupplier checks)
Expand Down
Loading