如何在Spigot插件中使用discord4j。

huangapple go评论80阅读模式
英文:

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");
    }
}

请注意,代码中的部分符号(如&quot;)在翻译后已被正确处理。如果需要进一步帮助,请随时提问。

英文:

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:

  1. The first time I load the plugin
java.lang.NoSuchMethodError: &#39;boolean io.netty.handler.ssl.SslProvider.isAlpnSupported(io.netty.handler.ssl.SslProvider)&#39;
at reactor.netty.http.client.HttpClientSecure.&lt;clinit&gt;(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.&lt;init&gt;(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.&lt;init&gt;(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) [?:?]
  1. 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.&lt;init&gt;(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.&lt;init&gt;(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: &#39;httpClientConf&#39; 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.&lt;clinit&gt;(HttpClientConfiguration.java:51) ~[?:?]
... 28 more

Here is my pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.qrl&lt;/groupId&gt;
    &lt;artifactId&gt;tcplugin&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;name&gt;TCPlugin&lt;/name&gt;

    &lt;description&gt;The offical TCPlugin&lt;/description&gt;
    &lt;properties&gt;
        &lt;java.version&gt;1.8&lt;/java.version&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
    &lt;url&gt;qrl.nz&lt;/url&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;${java.version}&lt;/source&gt;
                    &lt;target&gt;${java.version}&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.2.4&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;filters&gt;
                        &lt;filter&gt;
                            &lt;artifact&gt;*:*&lt;/artifact&gt;
                            &lt;excludes&gt;
                                &lt;exclude&gt;module-info.class&lt;/exclude&gt;
                                &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
                                &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
                                &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
                            &lt;/excludes&gt;
                        &lt;/filter&gt;
                    &lt;/filters&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;shade&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
        &lt;resources&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
                &lt;filtering&gt;true&lt;/filtering&gt;
            &lt;/resource&gt;
        &lt;/resources&gt;
    &lt;/build&gt;

    &lt;repositories&gt;
        &lt;repository&gt;
            &lt;id&gt;jitpack&lt;/id&gt;
            &lt;url&gt;https://jitpack.io&lt;/url&gt;
        &lt;/repository&gt;
        
        &lt;repository&gt;
            &lt;snapshots&gt;
                &lt;enabled&gt;false&lt;/enabled&gt;
            &lt;/snapshots&gt;
            &lt;id&gt;central&lt;/id&gt;
            &lt;name&gt;bintray&lt;/name&gt;
            &lt;url&gt;https://jcenter.bintray.com&lt;/url&gt;
        &lt;/repository&gt;

        &lt;repository&gt;
            &lt;id&gt;spigotmc-repo&lt;/id&gt;
            &lt;url&gt;https://hub.spigotmc.org/nexus/content/repositories/snapshots/&lt;/url&gt;
        &lt;/repository&gt;

        &lt;repository&gt;
            &lt;id&gt;sonatype&lt;/id&gt;
            &lt;url&gt;https://oss.sonatype.org/content/groups/public/&lt;/url&gt;
        &lt;/repository&gt;
    &lt;/repositories&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.spigotmc&lt;/groupId&gt;
            &lt;artifactId&gt;spigot-api&lt;/artifactId&gt;
            &lt;version&gt;1.16.3-R0.1-SNAPSHOT&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.discord4j&lt;/groupId&gt;
            &lt;artifactId&gt;discord4j-core&lt;/artifactId&gt;
            &lt;version&gt;3.1.1&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.github.DevLeoko&lt;/groupId&gt;
            &lt;artifactId&gt;AdvancedBan&lt;/artifactId&gt;
            &lt;version&gt;2.2.1&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

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(&quot;Unable to connect to discord, oh well&quot;);
			goBrr = false;
		}
	}

	public void exitDiscord() {
		if (goBrr) {
			gateway.logout();
		}
	}

	public boolean logedIn() {
		return this.goBrr;
	}

	public List&lt;String&gt; 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(&quot;No valid connection to discord&quot;);
	}
}

答案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 &lt;relocations&gt; tag.

&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
&lt;version&gt;3.2.4&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;phase&gt;package&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;shade&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;transformers&gt;
&lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
&lt;mainClass&gt;your.main.class&lt;/mainClass&gt;
&lt;/transformer&gt;
&lt;/transformers&gt;
&lt;filters&gt;
&lt;filter&gt;
&lt;artifact&gt;*:*&lt;/artifact&gt;
&lt;excludes&gt;
&lt;exclude&gt;META-INF/maven/**&lt;/exclude&gt;
&lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;
&lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;
&lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;
&lt;/excludes&gt;
&lt;/filter&gt;
&lt;/filters&gt;
&lt;relocations&gt;
&lt;relocation&gt;
&lt;pattern&gt;io&lt;/pattern&gt;
&lt;shadedPattern&gt;io.netty&lt;/shadedPattern&gt;
&lt;includes&gt;
&lt;include&gt;io.netty.*&lt;/include&gt;
&lt;/includes&gt;
&lt;/relocation&gt;
&lt;/relocations&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年10月26日 03:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64528129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定