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 Apr 3, 2024
2 parents 557decb + 71cce0c commit ec727c1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<ul>
<li><a href="https://github.com/discord-jar/discript">Discript</a> - a scripting language to simplify bot development.</li>
</ul>

### Prerequisites

<b>You'll need to add discord.jar to your project's dependencies. We are currently using
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -28,7 +28,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240205</version>
<version>20240303</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -105,7 +105,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>3.2.3</version>
<version>3.2.4</version>
</dependency>
<!-- Used for marking methods and params -->
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/seailz/discordjar/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Permission> util = new BitwiseUtil<>();
EnumSet<Permission> permissionsList = util.get(permissions, Permission.class);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/seailz/discordjar/events/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Event> eventType = (Class<? extends Event>) 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<? extends Event> eventType = (Class<? extends Event>) maybeEventType;
EventMethod eventMethod = method.getAnnotation(EventMethod.class);

String customId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ public record Application(
InstallParams installParams,
String roleConnectionsVerificationUrl,
int approximateGuildCount,
HashMap<IntegrationTypes, IntegrationTypeConfiguration> 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)
Expand All @@ -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;
Expand All @@ -136,6 +142,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) {
InstallParams installParams;
String roleConnectionsVerificationUrl;
int approximateGuildCount;
HashMap<IntegrationTypes, IntegrationTypeConfiguration> integrationTypesConfiguration = null;

try {
id = obj.getString("id");
Expand Down Expand Up @@ -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,
Expand All @@ -308,6 +323,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) {
installParams,
roleConnectionsVerificationUrl,
approximateGuildCount,
integrationTypesConfiguration,
discordJar
);
}
Expand Down Expand Up @@ -348,7 +364,7 @@ public List<ApplicationRoleConnectionMetadata> 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<ApplicationRoleConnectionMetadata> roleConnections) {
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/seailz/discordjar/model/guild/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,7 @@ public List<Role> 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)));
Expand Down Expand Up @@ -1232,7 +1233,7 @@ public StringFormatter formatter() {
* <p/>
* <b>This action is irreversible!</b>
*/
public void delete() throws IllegalAccessException {
public void delete() {
DiscordResponse response;
try {
response = new DiscordRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 243 in src/main/java/com/seailz/discordjar/utils/rest/DiscordRequest.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Constant values

Condition `contentType != null` is always `true`
requestBuilder.addHeader("Content-Type", contentType);
}
Expand Down

0 comments on commit ec727c1

Please sign in to comment.