diff --git a/README.md b/README.md index 84ef0c5b..f27faba5 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,14 @@ https://discord.gg/tmvS8A57J4 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. +## Example Usages +- [Tune](https://github.com/seailz/Tune) An example Discord Music bot built using discord.jar & LavaPlayer in just 1 hour, that's how simple it is! + +## Tools built for Discord.jar + + ### Prerequisites You'll need to add discord.jar to your project's dependencies. We are currently using @@ -189,9 +197,6 @@ To contribute to the `/examples` module, please see [here](https://github.com/di ## License License info can be found [here](https://github.com/discord-jar/discord.jar/blob/main/LICENSE). This project is licensed under GNU General Public License V3 -## Example Usages -- [Tune](https://github.com/seailz/Tune) An example Discord Music bot built using discord.jar & LavaPlayer in just 1 hour, that's how simple it is! - ## Contact Our official Discord server: diff --git a/pom.xml b/pom.xml index ca74f170..619c1c81 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.12.1 + 3.13.0 ${java.version} ${java.version} @@ -28,7 +28,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.5.1 + 3.5.2 package @@ -94,7 +94,7 @@ org.json json - 20240205 + 20240303 com.google.code.gson @@ -105,7 +105,7 @@ org.springframework.boot spring-boot-starter-websocket - 3.2.3 + 3.2.4 diff --git a/src/main/java/com/seailz/discordjar/command/Command.java b/src/main/java/com/seailz/discordjar/command/Command.java index ecee0993..f8ecbfb4 100644 --- a/src/main/java/com/seailz/discordjar/command/Command.java +++ b/src/main/java/com/seailz/discordjar/command/Command.java @@ -89,21 +89,21 @@ public static Command decompile(JSONObject obj) { boolean canUseInDms = true; boolean nsfw = false; - if (obj.has("name_localizations")) { + if (obj.has("name_localizations") && !obj.isNull("name_localizations")) { JSONObject nameLocalesJson = obj.getJSONObject("name_localizations"); for (String locale : nameLocalesJson.keySet()) { nameLocales.put(locale, nameLocalesJson.getString(locale)); } } - if (obj.has("description_localizations")) { + if (obj.has("description_localizations") && !obj.isNull("description_localizations")) { JSONObject descriptionLocalesJson = obj.getJSONObject("description_localizations"); for (String locale : descriptionLocalesJson.keySet()) { descriptionLocales.put(locale, descriptionLocalesJson.getString(locale)); } } - if (obj.has("default_member_permissions")) { + if (obj.has("default_member_permissions") && !obj.isNull("default_member_permissions")) { int permissions = obj.getInt("default_member_permissions"); BitwiseUtil util = new BitwiseUtil<>(); EnumSet permissionsList = util.get(permissions, Permission.class); diff --git a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java index b1e9d890..b1509501 100644 --- a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java +++ b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java @@ -55,7 +55,15 @@ public void addListener(DiscordListener... listeners) { for (DiscordListener listener : listeners) { for (Method method : listener.getClass().getMethods()) { if (method.isAnnotationPresent(EventMethod.class)) { - Class eventType = (Class) method.getParameterTypes()[0]; + Class maybeEventType = method.getParameterTypes()[0]; + + if (!Event.class.isAssignableFrom(maybeEventType)) + throw new IllegalArgumentException(String.format("%s first arg is not of Event", method)); + else if (method.getParameterTypes().length != 1) + throw new IllegalArgumentException(String.format("%s#%s is an invalid listener", method.getDeclaringClass(), method.getName())); + + @SuppressWarnings("unchecked") + Class eventType = (Class) maybeEventType; EventMethod eventMethod = method.getAnnotation(EventMethod.class); String customId = null; diff --git a/src/main/java/com/seailz/discordjar/model/application/Application.java b/src/main/java/com/seailz/discordjar/model/application/Application.java index dd516cc3..7e95cb2e 100644 --- a/src/main/java/com/seailz/discordjar/model/application/Application.java +++ b/src/main/java/com/seailz/discordjar/model/application/Application.java @@ -78,11 +78,16 @@ public record Application( InstallParams installParams, String roleConnectionsVerificationUrl, int approximateGuildCount, + HashMap integrationTypes, DiscordJar discordJar ) implements Compilerable, Snowflake { @Override public JSONObject compile() { + JSONObject integrationTypes = new JSONObject(); + this.integrationTypes.forEach((key, val) -> { + integrationTypes.put(String.valueOf(key.code), val.compile()); + }); return new JSONObject() .put("id", id) .put("name", name) @@ -106,11 +111,12 @@ public JSONObject compile() { .put("custom_install_url", customInstallUrl) .put("role_connections_verification_url", roleConnectionsVerificationUrl) .put("approximate_guild_count", approximateGuildCount) + .put("integration_types_config", integrationTypes) .put("guild", guild.compile()); } public static Application decompile(JSONObject obj, DiscordJar discordJar) { - if (obj == null) return new Application(null, null, null, null, null, false, false, null, null, null, null, null, null, null, null, null, null, null, null, 0, null, null, null, null, 0, discordJar); + if (obj == null) return new Application(null, null, null, null, null, false, false, null, null, null, null, null, null, null, null, null, null, null, null, 0, null, null, null, null, 0, null, discordJar); String id; String name; String iconUrl; @@ -136,6 +142,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) { InstallParams installParams; String roleConnectionsVerificationUrl; int approximateGuildCount; + HashMap integrationTypesConfiguration = null; try { id = obj.getString("id"); @@ -282,6 +289,14 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) { guild = null; } + if (obj.has("integration_types_config")) { + integrationTypesConfiguration = new HashMap<>(); + JSONObject integrationTypesConfig = obj.getJSONObject("integration_types_config"); + for (String code : integrationTypesConfig.keySet()) { + integrationTypesConfiguration.put(IntegrationTypes.getByCode(Integer.parseInt(code)), IntegrationTypeConfiguration.decompile(integrationTypesConfig.getJSONObject(code))); + } + } + return new Application( id, name, @@ -308,6 +323,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) { installParams, roleConnectionsVerificationUrl, approximateGuildCount, + integrationTypesConfiguration, discordJar ); } @@ -348,7 +364,7 @@ public List getRoleConnections() { * * @param roleConnections The list of role connection metadata objects to update. * - * @throws com.seailz.discordjar.utils.Checker.NullArgumentException if the list is null. + * @throws Checker.NullArgumentException if the list is null. * @throws IllegalArgumentException if the list has more than 5 elements. */ public void setRoleConnections(@NotNull List roleConnections) { @@ -425,4 +441,49 @@ public int id() { return id; } } + + + public enum IntegrationTypes { + + GUILD_INSTALL(0), + USER_INSTALL(1), + UNKNOWN(-1) + ; + + private final int code; + + IntegrationTypes(int code) { + this.code = code; + } + + public int getCode() { + return code; + } + + public static IntegrationTypes getByCode(int code) { + for (IntegrationTypes value : values()) { + if (value.getCode() == code) return value; + } + return UNKNOWN; + } + } + + public record IntegrationTypeConfiguration( + InstallParams installParams + ) implements Compilerable { + + @Override + public JSONObject compile() { + return new JSONObject() + .put("oauth2_install_params", installParams == null ? JSONObject.NULL : installParams.compile()); + } + + public static IntegrationTypeConfiguration decompile(JSONObject obj) { + InstallParams oauth2InstallParams = null; + if (obj.has("oauth2_install_params")) { + oauth2InstallParams = InstallParams.decompile(obj.getJSONObject("oauth2_install_params")); + } + return new IntegrationTypeConfiguration(oauth2InstallParams); + } + } } diff --git a/src/main/java/com/seailz/discordjar/model/guild/Guild.java b/src/main/java/com/seailz/discordjar/model/guild/Guild.java index a33e75be..1ac4e39d 100644 --- a/src/main/java/com/seailz/discordjar/model/guild/Guild.java +++ b/src/main/java/com/seailz/discordjar/model/guild/Guild.java @@ -1138,6 +1138,7 @@ public List roles() { } if (res == null) { System.out.println(response.code() + " " + (response.body() == null ? "null" : response.body().toString())); + return List.of(); } res.forEach(o -> roles.add(Role.decompile((JSONObject) o))); @@ -1232,7 +1233,7 @@ public StringFormatter formatter() { *

* This action is irreversible! */ - public void delete() throws IllegalAccessException { + public void delete() { DiscordResponse response; try { response = new DiscordRequest( diff --git a/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java b/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java index c7aadb17..3c40b38d 100644 --- a/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java +++ b/src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java @@ -240,9 +240,6 @@ private DiscordResponse invoke(String contentType, boolean auth) throws Unhandle if (auth) { requestBuilder.addHeader("Authorization", "Bot " + djv.getToken()); } - if (contentType == null) { - requestBuilder.addHeader("Content-Type", "application/json"); - } if (contentType != null) { requestBuilder.addHeader("Content-Type", contentType); }