A fast, reactive Java wrapper for the official Discord Bot API.
Built with Reactor, Netty, and a focus on flexibility, Discord4J provides an effective, non-blocking interface for creating Discord bots. The reactive and asynchronous nature of the library allows for scalability through backpressure handling and the efficient use of resources. Its modularized structure gives the user the ability to tailor their experience to different levels of abstraction and pick the right tools for the job.
Current stable releases are from 3.0.x
. Check the branch for instructions.
Instructions to use our latest pre-releases from master
branch (3.1.x):
repositories {
mavenCentral()
}
dependencies {
implementation 'com.discord4j:discord4j-core:3.1.0.M2'
}
<dependencies>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.1.0.M2</version>
</dependency>
</dependencies>
libraryDependencies ++= Seq(
"com.discord4j" % "discord4j-core" % "3.1.0.M2"
)
- Reactor Core 3.3
- Reactor Netty 0.9
- Jackson Databind 2.11
Coming from v3.0.x? Check our Migration Guide (work in progress)
DiscordClient.create(System.getenv("token"))
.withGateway(client -> {
client.getEventDispatcher().on(ReadyEvent.class)
.subscribe(ready -> System.out.println("Logged in as " + ready.getSelf().getUsername()));
client.getEventDispatcher().on(MessageCreateEvent.class)
.map(MessageCreateEvent::getMessage)
.filter(msg -> msg.getContent().equals("!ping"))
.flatMap(Message::getChannel)
.flatMap(channel -> channel.createMessage("Pong!"))
.subscribe();
return client.onDisconnect();
})
.block();
DiscordClient.create(System.getenv("token"))
.withGateway(client -> {
client.on(ReadyEvent.class)
.subscribe(ready -> System.out.println("Logged in as " + ready.getSelf().getUsername()));
client.on(MessageCreateEvent.class)
.subscribe(event -> {
Message message = event.getMessage();
if (message.getContent().equals("!ping")) {
message.getChannel().block().createMessage("Pong!").block();
}
});
return client.onDisconnect();
})
.block();
Check out more examples here
Discord4J is highly oriented towards customizability. To achieve this, the project is divided into several "modules" which can be used separately depending on your use case.
The core
module combines the other modules to form high-level abstractions for the entire Discord Bot API. This is the module most users will want when making bots.
The rest
module provides a low-level HTTP client specifically for Discord which properly handles Discord's ratelimiting system.
The gateway
module provides a low-level WebSocket client for interacting with the Discord Gateway.
The voice
module provides a client to manipulate audio through Voice Connections.
The common
module contains base utilities and models useful for other modules.
Discord4J's mechanism for storing information received on the gateway is completely pluggable. This allows both the ability to customize what is stored and how. The Stores repository contains some pre-made implementations as well as the API for making your own.
Development builds can be obtained from Sonatype Snapshots repository or Jitpack.
Make sure you have the appropriate repositories, using Gradle:
repositories {
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
mavenCentral()
}
dependencies {
implementation 'com.discord4j:discord4j-core:3.1.0-SNAPSHOT'
}
<repositories>
<repository>
<id>oss.sonatype.org-snapshot</id>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.1.0-SNAPSHOT</version>
</dependency>
</dependencies>