I am currently following this Guide to create my first discord bot in Java. I have a Quarkus Application (V3.6.6) set up with the mvn wrapper. I have added the discord4j package to my pom.xml
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.2.6</version>
</dependency>
I have created an Interface to be able to access my application properties inside my code
import io.smallrye.config.ConfigMapping;
@ConfigMapping(prefix = "discord")
public interface IdiscordBot {
String token();
String clientId();
String clientSecret();
}
my application.properties look like this:
discord.token=${DISCORD_API_TOKEN}
discord.client-id=${YOUR_DISCORD_CLIENT_ID}
discord.client-secret=${YOUR_DISCORD_CLIENT_SECRET}
My Actual Bot look like this
package bots;
import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.message.MessageCreateEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import io.quarkus.runtime.StartupEvent;
@ApplicationScoped
@Path("/discord")
public class DiscordBot {
@Inject
IdiscordBot discordBot;
void onStart(@Observes StartupEvent ev)
{
DiscordClient client = DiscordClient.create(discordBot.token());
GatewayDiscordClient gateway = client.login().block();
gateway.on(MessageCreateEvent.class).subscribe(event -> {
String content = event.getMessage().getContent();
if ("!ping".equals(content)) {
event.getMessage().getChannel().block().createMessage("Pong!").block();
}
});
gateway.onDisconnect().block();
}
}
If I run this, I get:
2024-02-01 18:33:10,075 INFO [dis.cor.DiscordClientBuilder] (Quarkus Main Thread) Discord4J 3.2.6 (https://discord4j.com)
2024-02-01 18:33:10,165 WARN [io.net.res.dns.DefaultDnsServerAddressStreamProvider] (d4j-limiter-1) Default DNS servers: [/[2001:4860:4860:0:0:0:0:8888]:53, /[2001:4860:4860:0:0:0:0:8844]:53] (Google Public DNS as a fallback)
2024-02-01 18:33:10,268 ERROR [io.qua.run.Application] (Quarkus Main Thread) Failed to start application (with profile [dev]): java.lang.RuntimeException: Failed to start quarkus
at io.quarkus.runner.ApplicationImpl.doStart(Unknown Source)
at io.quarkus.runtime.Application.start(Application.java:101)
at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:111)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:71)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:44)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:124)
at io.quarkus.runner.GeneratedMain.main(Unknown Source)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at io.quarkus.runner.bootstrap.StartupActionImpl$1.run(StartupActionImpl.java:113)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: reactor.core.Exceptions$ReactiveException: java.net.UnknownHostException: Failed to resolve 'discord.com' [A(1), AAAA(28)] after 4 queries
at reactor.core.Exceptions.propagate(Exceptions.java:396)
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:97)
at reactor.core.publisher.Mono.block(Mono.java:1742)
at bots.DiscordBot.onStart(DiscordBot.java:24)
at bots.DiscordBot_Observer_onStart_HOQkuT2M7cTKdLxHQb6PsFBzQ4I.notify(Unknown Source)
at io.quarkus.arc.impl.EventImpl$Notifier.notifyObservers(EventImpl.java:346)
at io.quarkus.arc.impl.EventImpl$Notifier.notify(EventImpl.java:328)
at io.quarkus.arc.impl.EventImpl.fire(EventImpl.java:82)
at io.quarkus.arc.runtime.ArcRecorder.fireLifecycleEvent(ArcRecorder.java:157)
at io.quarkus.arc.runtime.ArcRecorder.handleLifecycleEvents(ArcRecorder.java:108)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy_0(Unknown Source)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy(Unknown Source)
... 11 more
Suppressed: java.lang.Exception: #block terminated with an error
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
... 21 more
Caused by: java.net.UnknownHostException: Failed to resolve 'discord.com' [A(1), AAAA(28)] after 4 queries
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1097)
Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Error has been observed at the following site(s):
*__checkpoint ? Request to GET /gateway/bot [RequestStream]
*__checkpoint ? Request to GET /gateway/bot [DefaultRouter]
Original Stack Trace:
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1097)
...
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: io.netty.resolver.dns.DnsNameResolverException: [33311: /[2001:4860:4860:0:0:0:0:8844]:53] DefaultDnsQuestion(discord.com. IN AAAA) failed to send a query '33311' via UDP (no stack trace available)
Caused by: java.net.SocketException: Network is unreachable: sendto
at java.base/sun.nio.ch.DatagramChannelImpl.send0(Native Method)
at io.netty.channel.socket.nio.NioDatagramChannel.doWriteMessage(NioDatagramChannel.java:297)
...
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:1583)
If I run nslookup discord.com or try to ping it, the commands run as expected. I tried setting my DNS Resolver to Google and Cloudflare, but that did not change anything.
Is there a configuration missing? Is that a IPV4 / IPV6 thing. Or does this just not work with Quarkus and or Quarkus in dev mode. Unfortunately, I couldn't find anything that helped.
Thid is an issue with netty and reactor-netty. You can fix it by forcing the use of the default resolver: