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 Mar 29, 2024
2 parents c0562f7 + e4947d8 commit 71cce0c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 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
11 changes: 10 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 Expand Up @@ -94,6 +102,7 @@ private List<ListenerMethodPair> findListenersForEvent(@NotNull Class<? extends
*/
public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar djv) {
if (event == null) return;
event.setType(type);
new Thread(() -> {
long start = System.currentTimeMillis();
List<ListenerMethodPair> listenersForEventType = findListenersForEvent(type);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/seailz/discordjar/events/model/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.seailz.discordjar.events.model;

import com.seailz.discordjar.DiscordJar;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;

/**
Expand All @@ -21,6 +23,13 @@ public class Event {
private final DiscordJar bot;
private final long sequence;
private final JSONObject json;
/**
* -- SETTER --
* Not intended for use by the end user.
*/
@Setter
@Nullable
private Class<? extends Event> type;

public Event(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
this.bot = bot;
Expand Down Expand Up @@ -54,4 +63,12 @@ public JSONObject getJson() {
return json;
}

/**
* Returns the <i>type</i> of the event, such as {@link com.seailz.discordjar.events.model.message.MessageCreateEvent MessageCreateEvent}. This is nullable!
*/
@Nullable
public Class<? extends Event> getType() {
return type;
}

}
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 71cce0c

Please sign in to comment.