From 27e965e9788d200a6a8dbd634bdf56c510ea416c Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 14:50:49 +0100 Subject: [PATCH 1/8] build: updated all dependencies to work with java 21 & updated project to java 21 --- pom.xml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 0deec66b..4437c366 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,9 @@ b-1.1 - 17 - 17 - 17 + 21 + 21 + 21 @@ -65,12 +65,6 @@ jitpack.io https://jitpack.io - - - dv8tion - m2-dv8tion - https://m2.dv8tion.net/releases - @@ -122,8 +116,8 @@ com.github.seailz - databaseapi - 2.3 + Database4J + 02941e7bef @@ -144,7 +138,7 @@ com.github.codahale xsalsa20poly1305 - v0.10.1 + v0.10.1 From 53b98226390233161f6903e319a6784099663b12 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 14:51:20 +0100 Subject: [PATCH 2/8] feat(threading): added support for java 21's virtual threads. --- .../utils/thread/DiscordJarThreadAllocator.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java b/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java index f87b7ec8..1b92c722 100644 --- a/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java +++ b/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java @@ -31,6 +31,21 @@ public static Thread requestThread(Runnable runnable, String name) { return thread; } + public static Thread requestVirtualThread(Runnable runnable, String name) { + // First, let's check if we can actually use a virtual thread due to the JDK version. + int javaRelease = Integer.parseInt(System.getProperty("java.specification.version")); + if (javaRelease < 21) { + Logger.getLogger("discord.jar-threading") + .warning("discord.jar virtual threads are only supported on JDK 21 and above, falling back to normal threads. It's recommended, if possible, that you upgrade your JDK to 21 or above."); + return requestThread(runnable, name); + } + + Thread virtualThread = Thread.ofVirtual() + .start(runnable); + virtualThread.setName(name); + return virtualThread; + } + private static void printThreads() { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); From 82469f4c07820faf77d59a03576133c84c8342a0 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 14:56:25 +0100 Subject: [PATCH 3/8] feat(threading): added a fallback if discord.jar can't parse the java version --- .../utils/thread/DiscordJarThreadAllocator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java b/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java index 1b92c722..49f8accd 100644 --- a/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java +++ b/src/main/java/com/seailz/discordjar/utils/thread/DiscordJarThreadAllocator.java @@ -33,7 +33,14 @@ public static Thread requestThread(Runnable runnable, String name) { public static Thread requestVirtualThread(Runnable runnable, String name) { // First, let's check if we can actually use a virtual thread due to the JDK version. - int javaRelease = Integer.parseInt(System.getProperty("java.specification.version")); + int javaRelease; + try { + javaRelease = Integer.parseInt(System.getProperty("java.specification.version")); + } catch (NumberFormatException ex) { + Logger.getLogger("discord.jar-threading") + .warning("Could not parse java.specification.version, falling back to normal threads. This is usually a bug, so please report the following string to the discord.jar developers: " + System.getProperty("java.specification.version")); + return requestThread(runnable, name); + } if (javaRelease < 21) { Logger.getLogger("discord.jar-threading") .warning("discord.jar virtual threads are only supported on JDK 21 and above, falling back to normal threads. It's recommended, if possible, that you upgrade your JDK to 21 or above."); From f7aaa77f7b88aea179544342e6b590c29422b0b2 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 15:07:51 +0100 Subject: [PATCH 4/8] feat(gateway): implemented virtual threads in most gateway locations --- .../com/seailz/discordjar/events/EventDispatcher.java | 8 ++++---- .../com/seailz/discordjar/gateway/GatewayFactory.java | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java index f74eb770..a7d13666 100644 --- a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java +++ b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java @@ -69,7 +69,7 @@ public void addListener(DiscordListener... listeners) { */ public void dispatchEvent(Event event, Class type, DiscordJar djv) { if (event == null) return; - new Thread(() -> { + DiscordJarThreadAllocator.requestVirtualThread(() -> { long start = System.currentTimeMillis(); List listenersForEventType = listenersByEventType.get(type); if (listenersForEventType == null) { @@ -91,7 +91,7 @@ public void dispatchEvent(Event event, Class type, DiscordJar d } method.setAccessible(true); - DiscordJarThreadAllocator.requestThread(() -> { + DiscordJarThreadAllocator.requestVirtualThread(() -> { try { method.invoke(listenerMethodPair.listener, event); } catch (IllegalAccessException | ArrayIndexOutOfBoundsException e) { @@ -102,8 +102,8 @@ public void dispatchEvent(Event event, Class type, DiscordJar d System.out.println(method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " threw an exception while being invoked."); e.getCause().printStackTrace(); } - }, "djar--EventDispatcher-inner").start(); + }, "djar--EventDispatcher-inner"); } - }, "djar--EventDispatcher").start(); + }, "djar--EventDispatcher"); } } \ No newline at end of file diff --git a/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java b/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java index 1a506540..a0e0bd1d 100644 --- a/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java +++ b/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java @@ -13,6 +13,7 @@ import com.seailz.discordjar.utils.URLS; import com.seailz.discordjar.utils.rest.DiscordRequest; import com.seailz.discordjar.utils.rest.DiscordResponse; +import com.seailz.discordjar.utils.thread.DiscordJarThreadAllocator; import com.seailz.discordjar.voice.model.VoiceServerUpdate; import com.seailz.discordjar.voice.model.VoiceState; import com.seailz.discordjar.ws.ExponentialBackoffLogic; @@ -109,7 +110,7 @@ public GatewayFactory(DiscordJar discordJar, boolean debug, int shardId, int num ExponentialBackoffLogic backoffReconnectLogic = new ExponentialBackoffLogic(); socket.setReEstablishConnection(backoffReconnectLogic.getFunction()); backoffReconnectLogic.setAttemptReconnect((c) -> { - new Thread(discordJar::clearMemberCaches, "djar--clearing-member-caches").start(); + DiscordJarThreadAllocator.requestVirtualThread(discordJar::clearMemberCaches, "djar--clearing-member-caches"); return !shouldResume; }); @@ -352,7 +353,7 @@ private void handleDispatched(JSONObject payload) { } if (eventClass.equals(CommandInteractionEvent.class)) return; - new Thread(() -> { + DiscordJarThreadAllocator.requestVirtualThread(() -> { Event event; try { event = eventClass.getConstructor(DiscordJar.class, long.class, JSONObject.class) @@ -373,7 +374,7 @@ private void handleDispatched(JSONObject payload) { if (debug) { logger.info("[DISCORD.JAR - DEBUG] Event dispatched: " + eventClass.getName()); } - }, "djar--event-dispatch-gw").start(); + }, "djar--event-dispatch-gw"); if (Objects.requireNonNull(DispatchedEvents.getEventByName(payload.getString("t"))) == DispatchedEvents.READY) { this.sessionId = payload.getJSONObject("d").getString("session_id"); From 7585ebbed4b756b5321ae707b74931b53da07276 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 15:13:40 +0100 Subject: [PATCH 5/8] chore(jitpack.yml): updated jitpack.yml to java 21 --- jitpack.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jitpack.yml b/jitpack.yml index 2125e583..fb967c48 100644 --- a/jitpack.yml +++ b/jitpack.yml @@ -1,5 +1,5 @@ before_install: - - sdk install java 17.0.4-open - - sdk use java 17.0.4-open + - sdk install java 21-open + - sdk use java 21-open jdk: - - openjdk17 + - openjdk21 From f8d253f4f1e7ab8bea8e4e66ba1b5208f8166a1d Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 15:14:26 +0100 Subject: [PATCH 6/8] chore(codeql.yml): updated codeql to java 21 --- .github/workflows/codeql.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 659eb6af..f0c28feb 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -43,7 +43,7 @@ jobs: - name: Setup Java Version uses: actions/setup-java@v1 with: - java-version: 17 + java-version: 21 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL From 5fccdcfa57dec41af816e7bcc6d8a681e086a1c5 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 20:47:07 +0100 Subject: [PATCH 7/8] feat(gateway): implemented virtual threads in most gateway locations --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4437c366..bd3da7cc 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,6 @@ - jitpack.io From 25fca2d72c9ac848265b873d5a52c3646006d836 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 21:54:50 +0100 Subject: [PATCH 8/8] feat(followup): fixed followup action and files --- .../followup/InteractionFollowupAction.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/seailz/discordjar/action/interaction/followup/InteractionFollowupAction.java b/src/main/java/com/seailz/discordjar/action/interaction/followup/InteractionFollowupAction.java index e2a6fcf0..52c3215f 100644 --- a/src/main/java/com/seailz/discordjar/action/interaction/followup/InteractionFollowupAction.java +++ b/src/main/java/com/seailz/discordjar/action/interaction/followup/InteractionFollowupAction.java @@ -1,6 +1,7 @@ package com.seailz.discordjar.action.interaction.followup; import com.seailz.discordjar.DiscordJar; +import com.seailz.discordjar.action.interaction.MessageInteractionCallbackAction; import com.seailz.discordjar.model.component.DisplayComponent; import com.seailz.discordjar.model.embed.Embeder; import com.seailz.discordjar.model.interaction.callback.InteractionHandler; @@ -12,6 +13,7 @@ import com.seailz.discordjar.utils.rest.Response; import org.springframework.web.bind.annotation.RequestMethod; +import java.io.File; import java.util.HashMap; import java.util.List; @@ -139,6 +141,21 @@ public InteractionFollowupAction addEmbed(Embeder embed) { return this; } + public InteractionFollowupAction addFile(File file) { + this.getReply().addFile(file); + return this; + } + + public InteractionFollowupAction addFiles(File... files) { + this.getReply().addFiles(files); + return this; + } + + public InteractionFollowupAction addFiles(List files) { + this.getReply().addFiles(files); + return this; + } + public InteractionMessageResponse getReply() { return reply; } @@ -146,7 +163,7 @@ public InteractionMessageResponse getReply() { public Response run() { Response response = new Response<>(); try { - new DiscordRequest( + DiscordRequest req = new DiscordRequest( getReply().compile(), new HashMap<>(), URLS.POST.INTERACTIONS.FOLLOWUP @@ -155,7 +172,16 @@ public Response run() { discordJar, URLS.POST.INTERACTIONS.FOLLOWUP, RequestMethod.POST - ).invoke(); + ); + + if (getReply().useFiles()) { + List files = getReply().getFiles(); + File[] filesArray = new File[files.size()]; + filesArray = files.toArray(filesArray); + req.invokeWithFiles(filesArray); + } else { + req.invoke(); + } response.complete(InteractionHandler.from(token, id, discordJar)); } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { response.completeError(new Response.Error(e.getCode(), e.getMessage(), e.getBody()));