Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Jun 10, 2023
2 parents 7c6c76a + 1b65168 commit 35851b5
Show file tree
Hide file tree
Showing 44 changed files with 453 additions and 296 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,8 @@ https://discord.gg/tmvS8A57J4

## Getting Started

Before we start - please note that discord.jar is still a work in progress and there are some risks of deploying it in a production enviroment.

### Prerequisites

<b>You'll need to add discord.jar to your project's dependencies. We are currently using
Expand Down
87 changes: 36 additions & 51 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Expand Up @@ -18,6 +18,7 @@
import com.seailz.discordjar.events.EventDispatcher;
import com.seailz.discordjar.gateway.GatewayFactory;
import com.seailz.discordjar.http.HttpOnlyApplication;
import com.seailz.discordjar.model.api.APIRelease;
import com.seailz.discordjar.model.application.Application;
import com.seailz.discordjar.model.application.Intent;
import com.seailz.discordjar.model.channel.*;
Expand All @@ -39,9 +40,8 @@
import com.seailz.discordjar.utils.rest.DiscordResponse;
import com.seailz.discordjar.utils.rest.RequestQueueHandler;
import com.seailz.discordjar.utils.permission.Permission;
import com.seailz.discordjar.utils.rest.Response;
import com.seailz.discordjar.utils.rest.ratelimit.Bucket;
import com.seailz.discordjar.utils.version.APIVersion;
import com.seailz.discordjar.model.api.version.APIVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
Expand Down Expand Up @@ -132,39 +132,39 @@ public class DiscordJar {
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, false, -1, -1);
this(token, intents, version, false, null, false, -1, -1, APIRelease.STABLE);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean debug) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, debug, -1, -1);
this(token, intents, version, false, null, debug, -1, -1, APIRelease.STABLE);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version) throws ExecutionException, InterruptedException {
this(token, EnumSet.of(Intent.ALL), version, false, null, false, -1, -1);
this(token, EnumSet.of(Intent.ALL), version, false, null, false, -1, -1, APIRelease.STABLE);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), version, httpOnly, httpOnlyInfo, false, -1, -1);
this(token, EnumSet.noneOf(Intent.class), version, httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), APIVersion.getLatest(), httpOnly, httpOnlyInfo, false, -1, -1);
this(token, EnumSet.noneOf(Intent.class), APIVersion.getLatest(), httpOnly, httpOnlyInfo, false, -1, -1, APIRelease.STABLE);
}

/**
Expand All @@ -190,12 +190,12 @@ public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) thr
* @deprecated Use {@link DiscordJarBuilder} instead. This constructor will be set to protected in the future.
*/
@Deprecated
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo, boolean debug, int shardId, int numShards) throws ExecutionException, InterruptedException {
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo, boolean debug, int shardId, int numShards, APIRelease release) throws ExecutionException, InterruptedException {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
new RequestQueueHandler(this);
this.token = token;
this.intents = intents;
new URLS(version);
new URLS(release, version);
logger = Logger.getLogger("DISCORD.JAR");
this.commandDispatcher = new CommandDispatcher();
this.queuedRequests = new ArrayList<>();
Expand Down Expand Up @@ -479,14 +479,15 @@ public User getUserById(long id) {
* @return A {@link Channel} object
*/
@Nullable
public Channel getChannelById(String id) throws IllegalArgumentException {
public Channel getChannelById(String id) {
Checker.isSnowflake(id, "Given id is not a snowflake");
Cache<Channel> cc = getChannelCache();
Channel res;
try {
res = cc.getById(id);
} catch (Exception e) {
throw new IllegalArgumentException("Couldn't retrieve channel.");
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return res;
}
Expand All @@ -507,11 +508,7 @@ public MessagingChannel getTextChannelById(String id) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
try {
return MessagingChannel.decompile(raw, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return MessagingChannel.decompile(raw, this);
}

/**
Expand All @@ -530,11 +527,7 @@ public com.seailz.discordjar.model.channel.thread.Thread getThreadById(String id
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
try {
return com.seailz.discordjar.model.channel.thread.Thread.decompile(raw, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return com.seailz.discordjar.model.channel.thread.Thread.decompile(raw, this);
}

/**
Expand Down Expand Up @@ -572,11 +565,7 @@ public ForumChannel getForumChannelById(String id) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
try {
return ForumChannel.decompile(raw, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return ForumChannel.decompile(raw, this);
}

/**
Expand All @@ -595,11 +584,7 @@ public Category getCategoryById(String id) {
if (e.getHttpCode() == 404) return null;
throw new DiscordRequest.DiscordAPIErrorException(e);
}
try {
return Category.decompile(raw, this);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return Category.decompile(raw, this);
}

/**
Expand Down Expand Up @@ -815,23 +800,19 @@ public void registerCommands(CommandListener... listeners) {
boolean canUseInDms = (ann instanceof SlashCommandInfo) ? ((SlashCommandInfo) ann).canUseInDms() : ((ContextCommandInfo) ann).canUseInDms();
boolean nsfw = (ann instanceof SlashCommandInfo) ? ((SlashCommandInfo) ann).nsfw() : ((ContextCommandInfo) ann).nsfw();
new Thread(() -> {
try {
registerCommand(
new Command(
name,
listener.getType(),
description,
(listener instanceof SlashCommandListener) ? ((SlashCommandListener) listener).getOptions() : new ArrayList<>(),
nameLocales,
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
)
);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
registerCommand(
new Command(
name,
listener.getType(),
description,
(listener instanceof SlashCommandListener) ? ((SlashCommandListener) listener).getOptions() : new ArrayList<>(),
nameLocales,
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
)
);
}).start();
commandDispatcher.registerCommand(name, listener);

Expand All @@ -849,7 +830,7 @@ public void registerCommands(CommandListener... listeners) {
}
}

protected void registerCommand(Command command) throws DiscordRequest.UnhandledDiscordAPIErrorException {
protected void registerCommand(Command command) {
Checker.check(!(command.name().length() > 1 && command.name().length() < 32), "Command name must be within 1 and 32 characters!");
Checker.check(!Objects.equals(command.description(), "") && !(command.description().length() > 1 && command.description().length() < 100), "Command description must be within 1 and 100 characters!");
Checker.check(command.options().size() > 25, "Application commands can only have up to 25 options!");
Expand All @@ -874,7 +855,11 @@ protected void registerCommand(Command command) throws DiscordRequest.UnhandledD
this,
URLS.BASE_URL,
RequestMethod.POST);
commandReq.invoke();
try {
commandReq.invoke();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/seailz/discordjar/DiscordJarBuilder.java
@@ -1,11 +1,12 @@
package com.seailz.discordjar;

import com.seailz.discordjar.model.api.APIRelease;
import com.seailz.discordjar.model.application.Intent;
import com.seailz.discordjar.utils.HTTPOnlyInfo;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import com.seailz.discordjar.utils.version.APIVersion;
import com.seailz.discordjar.model.api.version.APIVersion;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;

Expand All @@ -24,6 +25,7 @@ public class DiscordJarBuilder {
private final String token;
private EnumSet<Intent> intents;
private APIVersion apiVersion = APIVersion.getLatest();
private APIRelease apiRelease = APIRelease.STABLE;
private boolean httpOnly;
private HTTPOnlyInfo httpOnlyInfo;
private boolean debug;
Expand Down Expand Up @@ -76,6 +78,10 @@ public DiscordJarBuilder setAPIVersion(APIVersion apiVersion) {
return this;
}

public void setAPIRelease(APIRelease apiRelease) {
this.apiRelease = apiRelease;
}

public DiscordJarBuilder setHTTPOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
return this;
Expand Down Expand Up @@ -123,7 +129,7 @@ public DiscordJar build() {
if (intents == null) defaultIntents();
if (httpOnly && httpOnlyInfo == null) throw new IllegalStateException("HTTPOnly is enabled but no HTTPOnlyInfo was provided.");
try {
return new DiscordJar(token, intents, apiVersion, httpOnly, httpOnlyInfo, debug, shardId, numShards);
return new DiscordJar(token, intents, apiVersion, httpOnly, httpOnlyInfo, debug, shardId, numShards, apiRelease);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -186,7 +186,7 @@ public CompletableFuture<AutomodRule> run() {
try {
return AutomodRule.decompile(request.invoke().body(), discordJar);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new RuntimeException(e);
throw new DiscordRequest.DiscordAPIErrorException(e);
}
});
Checker.notNull(name, "name");
Expand Down
Expand Up @@ -192,7 +192,7 @@ public CompletableFuture<AutomodRule> run() {
try {
return AutomodRule.decompile(request.invoke().body(), discordJar);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new RuntimeException(e);
throw new DiscordRequest.DiscordAPIErrorException(e);
}
});
Checker.notNull(name, "name");
Expand Down
Expand Up @@ -78,7 +78,7 @@ public int limit() {
*
* @return {@link List A list of} <b>partial</b> {@link Guild guilds}
*/
public List<Guild> run() throws DiscordRequest.UnhandledDiscordAPIErrorException {
public List<Guild> run() {
String url = URLS.GET.GUILDS.GET_CURRENT_USER_GUILDS;
if (before != null || after != null || limit != 0) {
url += "?";
Expand All @@ -90,14 +90,19 @@ public List<Guild> run() throws DiscordRequest.UnhandledDiscordAPIErrorException
url += "limit=" + limit + "&";
}

DiscordResponse response = new DiscordRequest(
new JSONObject(),
new HashMap<>(),
url,
discordJar,
URLS.GET.GUILDS.GET_CURRENT_USER_GUILDS,
RequestMethod.GET
).invoke();
DiscordResponse response = null;
try {
response = new DiscordRequest(
new JSONObject(),
new HashMap<>(),
url,
discordJar,
URLS.GET.GUILDS.GET_CURRENT_USER_GUILDS,
RequestMethod.GET
).invoke();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}

List<Guild> returnGuilds = new ArrayList<>();
response.arr().forEach(guild -> returnGuilds.add(Guild.decompile((JSONObject) guild, discordJar)));
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/com/seailz/discordjar/cache/Cache.java
Expand Up @@ -134,9 +134,9 @@ public T getById(String id) throws DiscordRequest.UnhandledDiscordAPIErrorExcept
if (returnObject.get() == null) {
// request from discord
DiscordResponse response;
response = new DiscordRequest(
discordRequest.body(), discordRequest.headers(), discordRequest.url().replaceAll("%s", id), discordJar, discordRequest.url(), RequestMethod.GET
).invoke();
response = new DiscordRequest(
discordRequest.body(), discordRequest.headers(), discordRequest.url().replaceAll("%s", id), discordJar, discordRequest.url(), RequestMethod.GET
).invoke();
Method decompile;
try {
decompile = clazz.getMethod("decompile", JSONObject.class, DiscordJar.class);
Expand Down Expand Up @@ -168,10 +168,15 @@ public T getById(String id) throws DiscordRequest.UnhandledDiscordAPIErrorExcept
return returnObject.get() == null ? null : (T) returnObject.get();
}

public JSONObject getFresh(String id) throws DiscordRequest.UnhandledDiscordAPIErrorException {
DiscordResponse response = new DiscordRequest(
discordRequest.body(), discordRequest.headers(), discordRequest.url().replaceAll("%s", id), discordJar, discordRequest.url(), RequestMethod.GET
).invoke();
public JSONObject getFresh(String id) {
DiscordResponse response = null;
try {
response = new DiscordRequest(
discordRequest.body(), discordRequest.headers(), discordRequest.url().replaceAll("%s", id), discordJar, discordRequest.url(), RequestMethod.GET
).invoke();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
return response.body();
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/seailz/discordjar/cache/JsonCache.java
Expand Up @@ -87,9 +87,13 @@ default void reset(int interval) {
*
* @see #howToUpdateFresh()
*/
default void updateFresh() throws DiscordRequest.UnhandledDiscordAPIErrorException {
default void updateFresh() {
if (howToUpdateFresh() == null) return;
update(howToUpdateFresh().invoke().body());
try {
update(howToUpdateFresh().invoke().body());
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
}

/**
Expand Down
Expand Up @@ -30,7 +30,7 @@ public AutomodRule.Action getAction() {
* The rule that was triggered.
*/
@Nullable
public AutomodRule getRule() throws DiscordRequest.UnhandledDiscordAPIErrorException {
public AutomodRule getRule() {
if (getGuild() == null) return null;
return getGuild().getAutomodRuleById(
getJson().getJSONObject("d").getString("rule_id")
Expand All @@ -50,7 +50,7 @@ public GuildChannel getChannel() {
}

@Nullable
public User getUser() throws DiscordRequest.UnhandledDiscordAPIErrorException {
public User getUser() {
return getBot().getUserById(getJson().getJSONObject("d").getString("user_id"));
}

Expand Down
Expand Up @@ -14,7 +14,7 @@ public AutoModRuleEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObj
}

@NotNull
public AutomodRule getRule() throws DiscordRequest.UnhandledDiscordAPIErrorException {
public AutomodRule getRule() {
return AutomodRule.decompile(getJson().getJSONObject("d"), getBot());
}
}
Expand Up @@ -15,7 +15,7 @@ public GuildEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject da
}

@Nullable
public Guild getGuild() throws DiscordRequest.UnhandledDiscordAPIErrorException {
public Guild getGuild() {
return getBot().getGuildById(getJson().getJSONObject("d").getString("guild_id"));
}

Expand Down

0 comments on commit 35851b5

Please sign in to comment.