Skip to content

Commit

Permalink
Merge pull request #273 from discord-jar/main
Browse files Browse the repository at this point in the history
main
  • Loading branch information
seailz authored Feb 24, 2024
2 parents 0dfc7e0 + cc4b8ff commit 2a2db7e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
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,7 +68,6 @@ public void start() {
Thread thread = new Thread(() -> {
while (true) {
if (!running) {
System.out.println("Heartbeat thread stopped.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
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 @@ -352,8 +352,8 @@ private DiscordResponse invoke(String contentType, boolean auth) throws Unhandle
}
} 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

0 comments on commit 2a2db7e

Please sign in to comment.