Skip to content

Commit

Permalink
Bump JDA, add logger hackaround
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 4, 2021
1 parent ffd340f commit a9593c5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
11 changes: 6 additions & 5 deletions pom.xml
Expand Up @@ -14,7 +14,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<BUILD_NUMBER>Unknown</BUILD_NUMBER>
<bukkit.version>1.17-R0.1-SNAPSHOT</bukkit.version>
<bukkit.version>1.17.1-R0.1-SNAPSHOT</bukkit.version>
<denizen.version>1.2.1-SNAPSHOT</denizen.version>
</properties>

Expand All @@ -32,10 +32,11 @@
<id>everything</id>
<url>https://repo.citizensnpcs.co</url>
</repository>

<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>

Expand All @@ -58,7 +59,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_227</version>
<version>4.3.0_305</version>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -174,6 +174,10 @@ else if (!scriptEntry.hasObject("message")) {
}
}

static {
DiscordConnectCommand.fixJDALogger();
}

public static void errorMessage(ScriptQueue queue, String message) {
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenDiscordBot.instance, () -> Debug.echoError(queue, message), 0);
}
Expand Down
Expand Up @@ -14,18 +14,21 @@
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.bukkit.Bukkit;
import org.slf4j.Logger;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.io.*;
import java.lang.invoke.MethodHandle;
import java.util.*;
import java.util.stream.Collectors;

public class DiscordConnectCommand extends AbstractCommand implements Holdable {
Expand Down Expand Up @@ -98,6 +101,49 @@ else if (!scriptEntry.hasObject("intents")
}
}

public static boolean loggerIsFixed = false;

public static PrintStream altLogger = new PrintStream(new ByteArrayOutputStream()) {
@Override
public void println(String s) {
Debug.log("JDA", s);
}
};

/**
* This method is a dirty hack to minimize the amount of broken output from JDA.
*/
public static void fixJDALogger() {
if (loggerIsFixed) {
return;
}
loggerIsFixed = true;
// Dirty hack step 1: break System.err so Paper won't complain when JDALogger's static init whines into System.err
PrintStream currentErr = System.err;
System.setErr(altLogger);
Logger defaultLogger = null;
try {
// Force JDALogger to init now, which will do that spam, and get a SimpleLogger instance while we're at it.
defaultLogger = JDALogger.getLog(DiscordConnectCommand.class);
}
finally {
// Fix the logger back, with a try/finally to avoid breaking it.
System.setErr(currentErr);
}
try {
// Dirty hack step 2: use that SimpleLogger instance to modify the class and redirect its log path to one that won't get complained about by Paper.
MethodHandle streamSetter = ReflectionHelper.getFinalSetter(defaultLogger.getClass(), "TARGET_STREAM");
streamSetter.invoke(altLogger);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

static {
fixJDALogger();
}

public static class DiscordConnectThread extends Thread {

public String code;
Expand All @@ -116,14 +162,23 @@ public void run() {
try {
try {
// Try with intents
JDA jda = JDABuilder.createDefault(code)
.enableCache(Arrays.stream(CacheFlag.values()).filter(f -> f.getRequiredIntent() == null || intents.contains(f.getRequiredIntent())).collect(Collectors.toList()))
.enableIntents(intents)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setAutoReconnect(true)
.setLargeThreshold(100000)
.setChunkingFilter(ChunkingFilter.ALL)
.build();
JDA jda;
// Hack to bypass Paper whining about JDA whining into System.err
PrintStream currentErr = System.err;
System.setErr(altLogger);
try {
jda = JDABuilder.createDefault(code)
.enableCache(Arrays.stream(CacheFlag.values()).filter(f -> f.getRequiredIntent() == null || intents.contains(f.getRequiredIntent())).collect(Collectors.toList()))
.enableIntents(intents)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setAutoReconnect(true)
.setLargeThreshold(100000)
.setChunkingFilter(ChunkingFilter.ALL)
.build();
}
finally {
System.setErr(currentErr);
}
conn.client = jda;
jda.awaitReady();
}
Expand Down

0 comments on commit a9593c5

Please sign in to comment.