Java interface for the official Discord API, written in Java 8. The API is also available in a few other languages.
For the latest dev builds, use the short commit hash or dev-SNAPSHOT
as your version.
Given that @VERSION@
= the version of Discord4J (this can either be a release version, the short commit hash or dev-SNAPSHOT
).
In your pom.xml
add (without the ellipses):
...
<repositories>
...
<repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.github.austinv11</groupId>
<artifactId>Discord4J</artifactId>
<version>@VERSION@</version>
<!-- <classifier>shaded</classifier> <!-- Include this line if you want a shaded jar (all the Discord4J dependencies bundled into one jar)-->
</dependency>
</dependencies>
...
In your build.gradle
add (without the ellipses):
...
repositories {
...
jcenter() //This prevents issues with transitive dependencies
maven {
url "https://jitpack.io"
}
}
...
dependencies {
...
compile "com.github.austinv11:Discord4J:@VERSION@"
//compile "com.github.austinv11:Discord4J:@VERSION@:shaded" //Use this line instead of the one above it if you want a shaded jar (all the Discord4J dependencies bundled into one jar)
}
...
In your build.sbt
add (without the ellipses):
...
libraryDependencies ++= Seq(
"com.github.austinv11" % "Discord4J" % "@VERSION@"
)
resolvers += "jcenter" at "http://jcenter.bintray.com"
resolvers += "jitpack.io" at "https://jitpack.io"
If you don't use Maven nor Gradle (which you really should, because it's a lot more flexible and allows you to update easily), you can always grab the shaded jar file (which has all the D4J dependencies inside), and link it in your IntelliJ or Eclipse project.
Module Settings > Dependencies > click the + > JARs or directories > Select your JAR file
Project Properties > Java Build Path > Add the jar file
- A quick overview of the AudioPlayer by @oopsjpeg
- A Discord Bot quick start example maintined by @quantic
- A simple Discord4J module example by @Martacus
- The Official Javadocs (or the Dash/Velocity/Zeal mirror maintained by @jammehcow)
The very first thing you need to do is obtain an IDiscordClient
object. This can be done by using the ClientBuilder
.
Example:
public class Example {
public static IDiscordClient createClient(String token, boolean login) { // Returns a new instance of the Discord client
ClientBuilder clientBuilder = new ClientBuilder(); // Creates the ClientBuilder instance
clientBuilder.withToken(token); // Adds the login info to the builder
try {
if (login) {
return clientBuilder.login(); // Creates the client instance and logs the client in
} else {
return clientBuilder.build(); // Creates the client instance but it doesn't log the client in yet, you would have to call client.login() yourself
}
} catch (DiscordException e) { // This is thrown if there was a problem building the client
e.printStackTrace();
return null;
}
}
}
The Discord4J library is very event driven. Your bot can detect these events through the use of an event listener. There are two ways of creating an event listener:
- Using
IListener
:
public class InterfaceListener implements IListener<ReadyEvent> { // The event type in IListener<> can be any class which extends Event
@Override
public void handle(ReadyEvent event) { // This is called when the ReadyEvent is dispatched
doCoolStuff();
}
}
- Using the
@EventSubscriber
annotation:
public class AnnotationListener {
@EventSubscriber
public void onReadyEvent(ReadyEvent event) { // This method is called when the ReadyEvent is dispatched
foo(); // Will be called!
}
public void onMessageReceivedEvent(MessageReceivedEvent event) { // This method is NOT called because it doesn't have the @EventSubscriber annotation
bar(); // Never called!
}
}
Registering your listener:
public class Main {
public static void main(String[] args) {
IDiscordClient client = Example.createClient(args[0], true); // Gets the client object (from the first example)
EventDispatcher dispatcher = client.getDispatcher(); // Gets the EventDispatcher instance for this client instance
dispatcher.registerListener(new InterfaceListener()); // Registers the IListener example class from above
dispatcher.registerListener(new AnnotationListener()); // Registers the @EventSubscriber example class from above
}
}
Discord4J has an API for creating modular Discord Bots! See Martacus's sample repo for an example as to how it works.
See the examples directory.
- Official Discord4J Addons: A collection of official addons and modules for Discord4J (https://github.com/Discord4J-Addons)
- Lavaplayer by @sedmelluq: A full-featured audio player for Discord4J and JDA (https://github.com/sedmelluq/lavaplayer)
- Discordinator by @alpha;helix: A modularized command API (https://github.com/alphahelix00/Discordinator)
- Instructability by @Kaioru: A simple command API (https://github.com/Kaioru/Instructability)
- BC4D4J by @danthonywalker: An annotation based command API written in Kotlin (https://github.com/danthonywalker/bc4d4j)
- D4J-OAuth by @xaanit and @UnderMybrella: An OAuth extenssion to Discord4J (https://github.com/xaanit/D4J-OAuth)
- DiscordEmoji4J by @nerd: An emoji library designed with Discord in mind (https://github.com/nerd/DiscordEmoji4J)
Due to the nature of the Discord API, any deprecations found in the API should not be expected to last past the current version. Meaning that if a method is deprecated on version 2.1.0, do not assume the method will be available in version 2.2.0.
The Discord API is still in development. Functions may break at any time.
In such an event, please contact me or submit a pull request.
No one is perfect at programming and I am no exception. If you see something that can be improved, please read the contributing guildelines and feel free to submit a pull request!
More information can be found in the official javadocs. Alternatively you can view the docs through Dash, Velocity, or Zeal (maintained by @jammehcow) under the User Contributed tab.
You can contact me on the Official Discord4J Server (recommended) or the Discord API server in the #java_discord4j channel.