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 Sep 23, 2023
2 parents 736a35c + 8adb3ac commit b817ad8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 6 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<version>b-1.1</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
</properties>

<build>
Expand Down Expand Up @@ -59,18 +59,11 @@
</resource>
</resources>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- TODO: REMOVE -->
<repository>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -122,8 +115,8 @@
<!-- Database API used for Linked Roles/OAuth2 token refreshing -->
<dependency>
<groupId>com.github.seailz</groupId>
<artifactId>databaseapi</artifactId>
<version>2.3</version>
<artifactId>Database4J</artifactId>
<version>02941e7bef</version>
</dependency>
<!-- Used for creating transcripts of channels -->
<dependency>
Expand All @@ -144,7 +137,7 @@
<dependency>
<groupId>com.github.codahale</groupId>
<artifactId>xsalsa20poly1305</artifactId>
<version>v0.10.1 </version>
<version>v0.10.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -139,14 +141,29 @@ 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<File> files) {
this.getReply().addFiles(files);
return this;
}

public InteractionMessageResponse getReply() {
return reply;
}

public Response<InteractionHandler> run() {
Response<InteractionHandler> response = new Response<>();
try {
new DiscordRequest(
DiscordRequest req = new DiscordRequest(
getReply().compile(),
new HashMap<>(),
URLS.POST.INTERACTIONS.FOLLOWUP
Expand All @@ -155,7 +172,16 @@ public Response<InteractionHandler> run() {
discordJar,
URLS.POST.INTERACTIONS.FOLLOWUP,
RequestMethod.POST
).invoke();
);

if (getReply().useFiles()) {
List<File> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void addListener(DiscordListener... listeners) {
*/
public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar djv) {
if (event == null) return;
new Thread(() -> {
DiscordJarThreadAllocator.requestVirtualThread(() -> {
long start = System.currentTimeMillis();
List<ListenerMethodPair> listenersForEventType = listenersByEventType.get(type);
if (listenersForEventType == null) {
Expand All @@ -91,7 +91,7 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
}

method.setAccessible(true);
DiscordJarThreadAllocator.requestThread(() -> {
DiscordJarThreadAllocator.requestVirtualThread(() -> {
try {
method.invoke(listenerMethodPair.listener, event);
} catch (IllegalAccessException | ArrayIndexOutOfBoundsException e) {
Expand All @@ -104,6 +104,6 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
}
}, "djar--EventDispatcher-inner");
}
}, "djar--EventDispatcher").start();
}, "djar--EventDispatcher");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ 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;
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.");
return requestThread(runnable, name);
}

Thread virtualThread = Thread.ofVirtual()
.start(runnable);
virtualThread.setName(name);
return virtualThread;
}

private static void printThreads() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

Expand Down

0 comments on commit b817ad8

Please sign in to comment.