Skip to main content

Quickstart with Discord4J

Learn how to install Discord4J and where to go next to get a bot up and running.

Download / Installation#

The recommended way to get Discord4J is to use a build automation tool like Gradle or Maven. Use the appropriate snippet below for your preferred dependency manager.

tip

Replace VERSION with the current version from Maven Central. Maven Central

repositories {  mavenCentral()}
dependencies {  implementation 'com.discord4j:discord4j-core:VERSION'}

Basic Bot#

With D4J installed, you can start writing your bot! Here's an example of a dead-simple "ping-pong" bot which simply responds "Pong!" any time someone sends "!ping".

DiscordClient.create("TOKEN")    .withGateway(client ->        client.on(MessageCreateEvent.class, event -> {          Message message = event.getMessage();
          if (message.getContent().equalsIgnoreCase("!ping")) {            return message.getChannel()                .flatMap(channel -> channel.createMessage("Pong!"));          }
          return Mono.empty();        }))    .block();

This basic example is easily extended to do all kinds of commands!

See the Basic Bot Tutorial for an in-depth explanation of this example.