英文:
How to use discord4j in a spigot plugin
问题
这是我翻译好的代码部分:
package nz.laspruca.tcplugin.util;
import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.object.entity.Member;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.qrl.tcplugin.TCPlugin.*;
public class Discord {
public GatewayDiscordClient gateway;
private boolean goBrr = true;
public Discord(String token) {
DiscordClient client = DiscordClient.builder(token).build();
gateway = client.login().block();
if (gateway == null) {
logger.warning("Unable to connect to discord, oh well");
goBrr = false;
}
}
public void exitDiscord() {
if (goBrr) {
gateway.logout();
}
}
public boolean logedIn() {
return this.goBrr;
}
public List<String> getMembers() throws IllegalStateException {
if (goBrr)
return Objects.requireNonNull(Objects.requireNonNull(gateway
.getGuilds()
.collect(Collectors.toList())
.block())
.get(0)
.getMembers()
.collect(Collectors.toList())
.block())
.stream()
.map(Member::getDisplayName)
.collect(Collectors.toList());
else
throw new IllegalStateException("No valid connection to discord");
}
}
请注意,代码中的部分符号(如"
)在翻译后已被正确处理。如果需要进一步帮助,请随时提问。
英文:
I am making a plugin for a friends minecraft server, they also have a discord server for this server. I am trying to add some integrations between the discord server and the plugin using discord4j. The plugin compiles fine, when I try to load the plugin, I get one of two exceptions:
- The first time I load the plugin
java.lang.NoSuchMethodError: 'boolean io.netty.handler.ssl.SslProvider.isAlpnSupported(io.netty.handler.ssl.SslProvider)'
at reactor.netty.http.client.HttpClientSecure.<clinit>(HttpClientSecure.java:79) ~[?:?]
at reactor.netty.http.client.HttpClient.secure(HttpClient.java:1073) ~[?:?]
at discord4j.common.ReactorResources.lambda$static$0(ReactorResources.java:41) ~[?:?]
at discord4j.common.ReactorResources.<init>(ReactorResources.java:54) ~[?:?]
at discord4j.rest.RestClientBuilder.initReactorResources(RestClientBuilder.java:245) ~[?:?]
at discord4j.rest.RestClientBuilder.build(RestClientBuilder.java:226) ~[?:?]
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:85) ~[?:?]
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:73) ~[?:?]
at nz.laspruca.tcplugin.util.Discord.<init>(Discord.java:18) ~[?:?]
at nz.laspruca.tcplugin.Plugin.onEnable(Plugin.java:14) ~[?:?]
at org.qrl.tcplugin.TCPlugin.onEnable(TCPlugin.java:18) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugin(CraftServer.java:494) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugins(CraftServer.java:408) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.loadWorld(MinecraftServer.java:435) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.DedicatedServer.init(DedicatedServer.java:216) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:808) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at java.lang.Thread.run(Thread.java:834) [?:?]
- When I reload the plugin
java.lang.ExceptionInInitializerError: null
at reactor.netty.http.client.HttpClient.followRedirect(HttpClient.java:870) ~[?:?]
at reactor.netty.http.client.HttpClient.followRedirect(HttpClient.java:806) ~[?:?]
at discord4j.common.ReactorResources.lambda$static$0(ReactorResources.java:41) ~[?:?]
at discord4j.common.ReactorResources.<init>(ReactorResources.java:54) ~[?:?]
at discord4j.rest.RestClientBuilder.initReactorResources(RestClientBuilder.java:245) ~[?:?]
at discord4j.rest.RestClientBuilder.build(RestClientBuilder.java:226) ~[?:?]
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:85) ~[?:?]
at discord4j.core.DiscordClientBuilder.build(DiscordClientBuilder.java:73) ~[?:?]
at nz.laspruca.tcplugin.util.Discord.<init>(Discord.java:18) ~[?:?]
at nz.laspruca.tcplugin.Plugin.onEnable(Plugin.java:14) ~[?:?]
at org.qrl.tcplugin.TCPlugin.onEnable(TCPlugin.java:18) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugin(CraftServer.java:494) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugins(CraftServer.java:408) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.reload(CraftServer.java:876) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.Bukkit.reload(Bukkit.java:642) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchCommand(CraftServer.java:758) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchServerCommand(CraftServer.java:743) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.DedicatedServer.handleCommandQueue(DedicatedServer.java:381) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.DedicatedServer.b(DedicatedServer.java:350) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.a(MinecraftServer.java:1007) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:846) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.IllegalArgumentException: 'httpClientConf' is already in use
at io.netty.util.ConstantPool.createOrThrow(ConstantPool.java:113) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at io.netty.util.ConstantPool.newInstance(ConstantPool.java:95) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at io.netty.util.AttributeKey.newInstance(AttributeKey.java:55) ~[spigot-1.16.3.jar:git-Spigot-57bbdd8-55a78ff]
at reactor.netty.http.client.HttpClientConfiguration.<clinit>(HttpClientConfiguration.java:51) ~[?:?]
... 28 more
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.qrl</groupId>
<artifactId>tcplugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TCPlugin</name>
<description>The offical TCPlugin</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>qrl.nz</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>jitpack</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.github.DevLeoko</groupId>
<artifactId>AdvancedBan</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</project>
And the class responsable for the discord interactions
package nz.laspruca.tcplugin.util;
import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.object.entity.Member;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.qrl.tcplugin.TCPlugin.*;
public class Discord {
public GatewayDiscordClient gateway;
private boolean goBrr = true;
public Discord(String token) {
DiscordClient client = DiscordClient.builder(token).build();
gateway = client.login().block();
if (gateway == null) {
logger.warning("Unable to connect to discord, oh well");
goBrr = false;
}
}
public void exitDiscord() {
if (goBrr) {
gateway.logout();
}
}
public boolean logedIn() {
return this.goBrr;
}
public List<String> getMembers() throws IllegalStateException {
if (goBrr)
return Objects.requireNonNull(Objects.requireNonNull(gateway
.getGuilds()
.collect(Collectors.toList())
.block())
.get(0)
.getMembers()
.collect(Collectors.toList())
.block())
.stream()
.map(Member::getDisplayName)
.collect(Collectors.toList());
else
throw new IllegalStateException("No valid connection to discord");
}
}
答案1
得分: 2
以下是翻译好的内容:
你现在可能已经解决了这个问题,但对其他人可能会有帮助。
我花了一些时间,但这对我有效。似乎 spigot 使用了 netty 的某种版本冲突。 (猜测)
注意 <relocations>
标签。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>你的主类</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>io</pattern>
<shadedPattern>io.netty</shadedPattern>
<includes>
<include>io.netty.*</include>
</includes>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
英文:
You might have solved the issue by now, but it might come in handy for others.
It took me a while, but this works for me. It seems that there seems to be some kind of version conflict with netty that spigot uses. (a guess)
Mind the <relocations> tag.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your.main.class</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>io</pattern>
<shadedPattern>io.netty</shadedPattern>
<includes>
<include>io.netty.*</include>
</includes>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论