Created
November 5, 2021 12:44
-
-
Save Fabricio20/f26d2e4ed22917d229611eea28d51311 to your computer and use it in GitHub Desktop.
JDA Inteceptor for Twilight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import okhttp3.HttpUrl; | |
import okhttp3.Interceptor; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import org.jetbrains.annotations.NotNull; | |
import java.io.IOException; | |
public class DiscordInterceptor implements Interceptor { | |
private volatile String host; | |
private volatile Integer port; | |
private volatile String scheme; | |
public DiscordInterceptor(String host) { | |
this.host = host; | |
} | |
public DiscordInterceptor(String host, int port) { | |
this.host = host; | |
this.port = port; | |
} | |
public DiscordInterceptor(String host, int port, String scheme) { | |
this.host = host; | |
this.port = port; | |
this.scheme = scheme; | |
} | |
public void setHost(String host) { | |
this.host = host; | |
} | |
public void setPort(Integer port) { | |
this.port = port; | |
} | |
public void setScheme(String scheme) { | |
this.scheme = scheme; | |
} | |
@Override | |
public @NotNull Response intercept(@NotNull Chain chain) throws IOException { | |
Request request = chain.request(); | |
if (!"discord.com".equals(request.url().host())) { | |
return chain.proceed(request); | |
} | |
HttpUrl.Builder builder = request.url() | |
.newBuilder(); | |
if (this.host != null) { | |
builder.host(this.host); | |
} | |
if (this.port != null) { | |
builder.port(this.port); | |
} | |
if (this.scheme != null) { | |
builder.scheme(scheme); | |
} | |
return chain.proceed(request.newBuilder().url(builder.build()).build()) | |
.newBuilder() | |
.removeHeader("X-RateLimit-Global") | |
.removeHeader("X-RateLimit-Limit") | |
.removeHeader("X-RateLimit-Remaining") | |
.removeHeader("X-RateLimit-Reset") | |
.removeHeader("X-RateLimit-Reset-After") | |
.removeHeader("X-RateLimit-Bucket") | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment