A multithreaded but easy to use library to create a Discord bot in Java.
This README is for the rewrite of Javacord (aka. Javacord 3) and not complete yet (e.g. the wiki is still for version 2.x). If you have any trouble with Javacord 3, it's highly recommended to join the Javacord Discord server (Invite) and ask for help there.
Javacord covers every action a Discord bot is able to perform (e.g. sending messages, banning users, editing servers, etc.) besides sending voice, which will be added in a later release. New features introduced by Discord are usually added in less than one week, depending on their size.
The recommended way to "download" Javacord is to use a build manager like Maven. If you are not familiar with Maven, you can take a look at the Setup Guide or directly download it from Jenkins.
Repository
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Javacord Dependency
<dependency>
<groupId>de.btobastian.Javacord</groupId>
<artifactId>javacord</artifactId>
<!-- See below what to insert here -->
<version>COMMIT_ID</version>
<type>pom</type>
</dependency>
Replace COMMIT_ID
with the latest commit id. Once the rewrite is finished, there will be proper version numbers.
In this example the version would be 5255914
:
Optional Logger Dependency
<!-- Any SLF4J compatible logging framework. logback-classic is recommended -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
If you never used maven before you should take a look at the setup tutorial:
- Javacord server (recommended)
- DiscordAPI #java_javacord channel
For detailed information take a look at the wiki: Wiki
The wiki for Javacord 3 is a work in progress and not complete!
The latest JavaDocs can be found here: JavaDocs
Logging in is very simple
public class MyFirstBot {
public static void main(String[] args) {
String token = args[0];
new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Print the invite url of your bot
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
}).exceptionally(ExceptionLogger.get());
}
}
You can also login blocking which throws an exception if the login failed:
public class MyFirstBot {
public static void main(String[] args) {
String token = args[0];
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Print the invite url of your bot
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
}
}
An example bot can be found here: Example Bot
Click here: Creating a Bot Account
The version number has the a 3-digit format: major.minor.trivial
major
: Increased extremely rarely to mark a major release (usually a rewrite affecting very huge parts of the library). You can expect this digit to not change for several years.minor
: Any backwards incompatible change to the api. You can expect this digit to change about 1-3 times per year.trivial
: A backwards compatible change to the api. This is usually an important bugfix (or a bunch of smaller ones) or a backwards compatible feature addition. You can expect this digit to change 1-2 times per month.
A method/class which was marked as deprecated can be removed with the next minor release (but it will usually stay for several minor releases). A minor release might remove a class/method without having it deprecated, but we will do our best to deprecate it before removing it. We are unable to guarantee this though, because we might have to remove/replace something due to changes made by Discord which we are unable to control. Usually you can expect a deprecated method/class to stay for at least 6 months before it finally gets removed, but this is not guaranteed.
Javacord has its own Discord Server. You can join it (Invite) for support, status updates or just chatting with other users.