Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main #273

Merged
merged 32 commits into from
Feb 24, 2024
Merged

main #273

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9593b64
build(deps): bump org.jetbrains:annotations from 24.0.1 to 24.1.0
dependabot[bot] Nov 16, 2023
9985478
Merge pull request #256 from discord-jar/dependabot/maven/org.jetbrai…
seailz Nov 16, 2023
6ae9ceb
fix(gateway): fixed the "heartbeat thread stopped" spam
seailz Nov 18, 2023
a254097
Merge remote-tracking branch 'origin/main'
seailz Nov 18, 2023
2754029
build(deps): bump org.springframework.boot:spring-boot-starter-websocket
dependabot[bot] Nov 24, 2023
4061723
Merge pull request #257 from discord-jar/dependabot/maven/org.springf…
seailz Nov 24, 2023
5344577
build(deps): bump org.jsoup:jsoup from 1.16.2 to 1.17.1
dependabot[bot] Nov 27, 2023
1c097d7
Merge pull request #258 from discord-jar/dependabot/maven/org.jsoup-j…
seailz Nov 27, 2023
4aa4af9
feat(event dispatcher): added a new field to the EventMethod.java cla…
seailz Dec 9, 2023
14175a8
Merge remote-tracking branch 'origin/main'
seailz Dec 9, 2023
b2332ed
build(deps): bump org.apache.maven.plugins:maven-compiler-plugin
dependabot[bot] Dec 19, 2023
e4c4f06
Merge pull request #259 from discord-jar/dependabot/maven/org.apache.…
seailz Dec 20, 2023
7bb750c
build(deps): bump org.springframework.boot:spring-boot-starter-websocket
dependabot[bot] Dec 22, 2023
2d0bda4
Merge pull request #260 from discord-jar/dependabot/maven/org.springf…
seailz Dec 22, 2023
319de24
build(deps): bump org.apache.maven.plugins:maven-compiler-plugin
dependabot[bot] Dec 25, 2023
8b514dd
Merge pull request #261 from discord-jar/dependabot/maven/org.apache.…
seailz Dec 25, 2023
36a7cfb
build(deps): bump org.jsoup:jsoup from 1.17.1 to 1.17.2
dependabot[bot] Dec 29, 2023
fe9dafb
Merge pull request #262 from discord-jar/dependabot/maven/org.jsoup-j…
seailz Dec 29, 2023
d8f0a01
build(deps): bump joda-time:joda-time from 2.12.5 to 2.12.6
dependabot[bot] Jan 8, 2024
0bbfa39
Merge pull request #263 from discord-jar/dependabot/maven/joda-time-j…
seailz Jan 8, 2024
5de407d
build(deps): bump joda-time:joda-time from 2.12.6 to 2.12.7
dependabot[bot] Feb 5, 2024
401baf0
Merge pull request #265 from discord-jar/dependabot/maven/joda-time-j…
seailz Feb 5, 2024
c8477b4
build(deps): bump org.json:json from 20231013 to 20240205
dependabot[bot] Feb 6, 2024
1668988
Merge pull request #266 from discord-jar/dependabot/maven/org.json-js…
seailz Feb 6, 2024
4d6f2b2
build(deps): bump commons-codec:commons-codec from 1.16.0 to 1.16.1
dependabot[bot] Feb 9, 2024
b14259e
build(deps): bump org.apache.maven.plugins:maven-shade-plugin
dependabot[bot] Feb 21, 2024
d04ca4f
build(deps): bump org.springframework.boot:spring-boot-starter-websocket
dependabot[bot] Feb 23, 2024
118f5c0
removed 404 message
seailz Feb 24, 2024
4a287e0
Merge remote-tracking branch 'origin/main'
seailz Feb 24, 2024
f350405
Merge pull request #272 from discord-jar/dependabot/maven/org.springf…
seailz Feb 24, 2024
a88c89a
Merge pull request #270 from discord-jar/dependabot/maven/org.apache.…
seailz Feb 24, 2024
cc4b8ff
Merge pull request #267 from discord-jar/dependabot/maven/commons-cod…
seailz Feb 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 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.11.0</version>
<version>3.12.1</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 @@ -82,7 +82,7 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.16.0</version>
<version>1.16.1</version>
</dependency>
<!-- Used for interaction-only bot security -->
<dependency>
Expand All @@ -94,7 +94,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
<version>20240205</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -105,13 +105,13 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>3.1.5</version>
<version>3.2.3</version>
</dependency>
<!-- Used for marking methods and params -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -129,7 +129,7 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.2</version>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand All @@ -139,7 +139,7 @@
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.5</version>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>com.github.codahale</groupId>
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/seailz/discordjar/events/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public class EventDispatcher {
private static class ListenerMethodPair {
final DiscordListener listener;
final Method method;
final String customId;

ListenerMethodPair(DiscordListener listener, Method method) {
ListenerMethodPair(DiscordListener listener, Method method, String customId) {
this.listener = listener;
this.method = method;
this.customId = customId;
}
}

Expand All @@ -53,7 +55,12 @@ public void addListener(DiscordListener... listeners) {
for (Method method : listener.getClass().getMethods()) {
if (method.isAnnotationPresent(EventMethod.class)) {
Class<? extends Event> eventType = (Class<? extends Event>) method.getParameterTypes()[0];
listenersByEventType.computeIfAbsent(eventType, k -> new ArrayList<>()).add(new ListenerMethodPair(listener, method));
EventMethod eventMethod = method.getAnnotation(EventMethod.class);

String customId = null;
if (eventMethod.requireCustomId() != null && !eventMethod.requireCustomId().equals("")) customId = eventMethod.requireCustomId();

listenersByEventType.computeIfAbsent(eventType, k -> new ArrayList<>()).add(new ListenerMethodPair(listener, method, customId));
}
}
}
Expand Down Expand Up @@ -90,6 +97,18 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
}
}

if (listenerMethodPair.customId != null) {
if (event instanceof CustomIdable) {
if (((CustomIdable) event).getCustomId() == null) {
continue;
}

if (!((CustomIdable) event).getCustomId().matches(listenerMethodPair.customId)) {
continue;
}
}
}

method.setAccessible(true);
DiscordJarThreadAllocator.requestThread(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import java.lang.annotation.Target;

/**
* <strike>This annotation is used to mark methods that should be called when an event is fired.
* If a listener method isn't marked with this annotation, it will not be called.</strike>
*
* <b>THIS ANNOTATION IS NO LONGER REQUIRED. It exists purely for backwards compatibility.</b>
* This annotation is used to mark methods that should be called when an event is fired.
* <br>If a listener method isn't marked with this annotation, it will not be called.
*
* @author Seailz
* @see com.seailz.discordjar.events.DiscordListener
Expand All @@ -19,4 +17,26 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventMethod {


/**
* If this field is set, then the event will only run if the customid of the event == the value of this field.
* <br>Example:
* <br>
* <code>
* <br>@EventMethod(requireCustomId="my_custom_id)
* <br>public void onModalInteractionEvent(@NotNull ModalInteractionEvent event) {
* <br> // This will only run if the custom id of the event is "my_custom_id"
* <br> event.reply("The custom ID is my_custom_id!").run();
* <br>}
* </code>
* <p></p>
* It's also a regex, so you can do things like this: <code>@RequireCustomId("my_custom_id|my_custom_id2")</code> which will match both "my_custom_id" and "my_custom_id2" ,
* or <code>@RequireCustomId("my_custom_id-.*")</code> which will match any custom id that starts with "my_custom_id-".
* <p></p>
* @author Seailz
* @since 1.0
*/
String requireCustomId() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@
Thread thread = new Thread(() -> {
while (true) {
if (!running) {
System.out.println("Heartbeat thread stopped.");
try {
Thread.sleep(1000);

Check warning on line 72 in src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Busy wait

Call to `Thread.sleep()` in a loop, probably busy-waiting
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -81,7 +80,7 @@
WSPayloads.HEARBEAT.fill(lastSequence == -1 ? JSONObject.NULL : lastSequence).toString()
);
Gateway.lastHeartbeatSent = new Date();
Thread.sleep(interval);

Check warning on line 83 in src/main/java/com/seailz/discordjar/gateway/heartbeat/HeartLogic.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Busy wait

Call to `Thread.sleep()` in a loop, probably busy-waiting
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.seailz.discordjar.utils.annotation;

import com.seailz.discordjar.events.annotation.EventMethod;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -23,9 +25,11 @@
* <p></p>
* Using this annotation, however, will cause some performance loss, so use it sparingly or if time is not a concern.
* @author Seailz
* @deprecated Use the {@link EventMethod#requireCustomId()} field instead.
* @since 1.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Deprecated(since = "1.0")
@Target({java.lang.annotation.ElementType.METHOD})
public @interface RequireCustomId {
String value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@
headers.forEach(requestBuilder::addHeader);

Request request = requestBuilder.build();
Response response = client.newCall(request).execute();

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

AutoCloseable used without 'try'-with-resources

'Response' used without 'try'-with-resources statement

int responseCode = response.code();
String sb = response.body().string();

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `string` may produce `NullPointerException`
HashMap<String, String> headers = new HashMap<>();
Headers responseHeaders = response.headers();
for (String name : responseHeaders.names()) {
Expand Down Expand Up @@ -183,7 +183,7 @@
if (canRequest.get(url) != null && !canRequest.get(url)) {
// We can't request, so we'll wait our turn.
while (!canRequest.get(url)) {
Thread.sleep(1);

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Busy wait

Call to `Thread.sleep()` in a loop, probably busy-waiting
}
}
canRequest.put(url, false);
Expand Down Expand Up @@ -240,20 +240,20 @@
if (auth) {
requestBuilder.addHeader("Authorization", "Bot " + djv.getToken());
}
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 `false`
requestBuilder.addHeader("Content-Type", "application/json");
}
if (contentType != null) {

Check warning on line 246 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);
}
headers.forEach((key, value) -> requestBuilder.addHeader(key, value));

Request request = requestBuilder.build();
Response response = client.newCall(request).execute();

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

AutoCloseable used without 'try'-with-resources

'Response' used without 'try'-with-resources statement
canRequest.put(url, true);

int responseCode = response.code();
String sb = response.body().string();

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `string` may produce `NullPointerException`
if (djv.isDebug()) {
System.out.println(uuid + " " + request.method() + " " + request.url() + " with " + (this.body == null ? this.aBody : this.body) + " returned " + responseCode + " with " + sb);
}
Expand All @@ -267,10 +267,10 @@
// If the bucket doesn't exist, it will be created.
if (responseHeaders.get("X-RateLimit-Bucket") != null) {
String id = responseHeaders.get("X-RateLimit-Bucket");
int limit = Integer.parseInt(responseHeaders.get("X-RateLimit-Limit"));

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `responseHeaders.get("X-RateLimit-Limit")` might be null
int remaining = Integer.parseInt(responseHeaders.get("X-RateLimit-Remaining"));

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `responseHeaders.get("X-RateLimit-Remaining")` might be null
BigDecimal reset = new BigDecimal(responseHeaders.get("X-RateLimit-Reset"));

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `responseHeaders.get("X-RateLimit-Reset")` might be null
float resetAfter = Float.parseFloat(responseHeaders.get("X-RateLimit-Reset-After"));

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Argument `responseHeaders.get("X-RateLimit-Reset-After")` might be null

if (djv.isDebug()) {
System.out.println("Updating bucket " + id + " with limit " + limit + ", remaining " + remaining + ", reset " + reset + ", resetAfter " + resetAfter);
Expand Down Expand Up @@ -348,12 +348,12 @@
try {
JSONObject json = new JSONObject(sb);
if (json.has("message")) {
message = json.getString("message");

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused assignment

The value `json.getString("message")` assigned to `message` is never used

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

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused assignment

The value `json.getString("message")` assigned to `message` is never used
}
} catch (JSONException ignored) {
}
Logger.getLogger("DiscordJar")
.warning("[REST] 404: " + message);
// Logger.getLogger("DiscordJar")
// .warning("[REST] 404: " + message);
return new DiscordResponse(404, null, null, null);
}

Expand Down
Loading