Java is a very popular, stable, and robust programming language that has been around for decades. There’s no shortage of demand for the language, with thousands upon thousands of developers writing Java code every single day. 

If Java is your bread and butter, or if you’re just looking to get into Java and need a good starting point, then this just may be the perfect fit! 

Making your own Discord bot in Java is going to teach you all the nuances of setting up a project, configuring it, adding a library/wrapper, writing code, and finally getting your bot online. 

We’ll be talking about two Java Discord wrappers here: JavaCord and Discord4J. Each has its own pros/cons and slightly different event handler syntax, so it’ll be up to you to compare the two and choose which one you feel fits your coding style more. 

In either case, they’re both very powerful libraries that’ll get the job done. 

So let’s get to it! We’ll talk about configuring your environment, choosing your IDE, and getting a library setup for Discord bot creation in the next section. 

Configuring Java for Discord Bot Development

If you’ve never written code in a language like Java or C#, you’ll immediately notice that setting up your environment is a bit different compared to if you were using node.js/Javascript, for example. I only mention this because when it comes to Discord bots, JavaScript seems to be the most popular language to go with (I see it everywhere!)

Java development is typically done through an IDE (Integrated Development Environment) that lives on your local system.  This is essentially a software suite that you use to write, debug, and compile all of your code.

Before you can start writing any code, however, you’ll first choose an IDE, download it, install it, and then get a project up and running.

Luckily, this isn’t complicated stuff. Let’s dive into how you can configure your IDE and chosen Library/Wrapper.

The Best Java IDEs for Discord Bot Creation

IntelliJ IDEA and Eclipse are the top two Java IDEs we’d recommend to any Java Developer. They’re proven, they’re stable, and they have great feedback from devs across the board.

IntelliJ IDEA Java IDE Splash Screen

IntelliJ IDEA has a free, open-source community version that you can download and start using in minutes. It’s cross-platform and supports development on Windows, Mac, and Linux.

I personally like this IDE since it has a more modern look and feel compared to Eclipse, but this is just my personal preference. They’re both great IDEs.

You can download IntelliJ IDEA right now and get started.

Eclipse IDE for Java

Eclipse is a Java IDE that’s been around for quite a while (originally brought to life by IBM in 2001). It’s a very stable, robust development environment that developers all around the world continue to use and love.

Eclipse is also cross-platform and runs on Windows, Mac, and Linux.

You can download Eclipse right now and get going in Java.

Top Discord Wrappers/Libraries for Java Bot Development

When it comes to Java API Wrappers/Libraries for Discord JavaCord and Discord4J come out on top as the most widely used and supported.

To simplify your life and start writing your bot as soon as possible, I would strongly recommend getting one of these libraries before starting development.

We’ll walk through the setup of each one, and then show you how implementing each wrapper in actual Java code looks like.

Before we get too far: Gradle vs. Maven

Gradle vs. Maven

One of the first things you’ll notice when setting up your development environment for JavaCord and Discord4J is the delineation between Gradle and Maven. What are they, and what’s the difference?

Well, both Gradle and Maven are “Build Automation Tools”. This means that when you hit “Build” or “Compile” on your project, Gradle/Maven go through the process of compiling your code, packaging it, and running any automated tests you may have set up right off the bat.

You essentially set up a series of steps that happen every time you build/compile your code, saving you the trouble of doing it manually on each build.

We’re not going to get deep into the Gradle vs. Maven debate in this guide as it’s out of scope, but we do have a great resource for you to read through if you’re interested in all of the nuances and understanding when you should use one or the other.

Take a look at this Gradle vs. Maven comparison guide from DZone.

What does this mean for Discord bot development?

It means you’ll have to choose either Gradle or Maven to move forward with, and then add either JavaCord or Discord4J as a dependency so that these libraries are included in your bot on compile time.

If this sounds complicated, don’t worry, we’re about to show you exactly how to set this up.

Integrating JavaCord or Discord4J into your Discord bot project

First thing’s first: you’ll have to make a choice between JavaCord or Discord4J!

Here are a few fun facts to hopefully make your decision a bit easier.

Update Frequency:

Discord4J is updated much more frequently than JavaCord. If you look at release tags generated by Discord4J and release tags generated by JavaCord over their history, Discord4J is the winner. If you want a library that’s frequently updated, I’d choose Discord4J.

Contributors Working on the Project:

Discord4J (as of the time this guide was written) has nearly double the contributors of JavaCord. This means that there are more people working on improving it, which increases the likelihood you’ll be dealing with less buggy code and a more robust framework. Here is the contribution chart for Discord4J and the contribution chart for JavaCord.

Syntax Differences and Implementation:

Both libraries/wrappers use event listeners, where you create code that “filters” out particular events and then runs your own code snippets as responses (once those events are triggered).

Discord4J is a reactive Java Wrapper for Discord’s API.

JavaCord is a library for Discord’s API boasting easy multi-threading.

Take a look at the two code examples for a simple “Ping Pong” below and choose which one appeals to you the most.

Discord4J – Simple Ping Pong Bot Code Example

public class Bot {

  public static void main(String[] args) {
    DiscordClientBuilder builder = new DiscordClientBuilder("TOKEN HERE");
    DiscordClient client = builder.build();

    client.getEventDispatcher().on(ReadyEvent.class)
        .subscribe(event -> {
          User self = event.getSelf();
          System.out.println(String.format("Logged in as %s#%s", self.getUsername(), self.getDiscriminator()));
        });

    client.getEventDispatcher().on(MessageCreateEvent.class)
        .map(MessageCreateEvent::getMessage)
        .filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
        .filter(message -> message.getContent().orElse("").equalsIgnoreCase("!ping"))
        .flatMap(Message::getChannel)
        .flatMap(channel -> channel.createMessage("Pong!"))
        .subscribe();

    client.login().block();
  }
}

JavaCord – Simple Ping Pong Bot Code Example

public class MyFirstBot {

    public static void main(String[] args) {
        // Insert your bot's token here
        String token = "your token";

        DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();

        // Add a listener which answers with "Pong!" if someone writes "!ping"
        api.addMessageCreateListener(event -> {
            if (event.getMessageContent().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());
    }
}

I personally chose Discord4J since its the most active and more frequently updated of the two, but JavaCord is an awesome choice as well.

If you have a tough time deciding you can always create a small simple bot in both and see how each one works/functions for your purposes.

Either way, let’s get to where we actually set up both of these libraries in our project!

Prerequisites:

By this point, you should have either IntelliJ IDEA or Eclipse installed as an IDE and ready to go on your machine.

You should know whether you’re going to be using Gradle or Maven as your build automation system.

And finally, you should have a good idea of which library/wrapper for the Discord API you want to use first: JavaCord or Discord4J.

Setting up JavaCord

JavaCord3 Library Logo

To get started with JavaCord, you should go to this link and start reading through the download/installation section.

There are guides on setting up with both Gradle and Maven, so no matter which build manager you chose earlier you’ll have everything you need to get going.

Link to download JavaCord (latest release).

On the getting started page you’ll see an “IDE Setup” section with several guides for IntelliJ / Eclipse and Gradle/Maven.

Direct link to the IDE Setup section of the JavaCord Guide.

JavaCord3 IDE Setup Guide

Go through that guide to configure JavaCord in either IntelliJ or Eclipse, and you’ll be ready to start coding!

Helpful JavaCord Links:

https://github.com/Javacord/Javacordhttps://javacord.org/wiki/getting-started/welcome/

Setting Up Discord4J

Discord 4J Library

To get started with Discord4J, go to this link and start reading through and following along with the setup guide.

You’ll see a download/installation guide for both IntelliJ IDEA and Eclipse as well as Maven and Gradle.

Here’s a link to the download/installation section of the Discord4J Guide.

Java Discord Bot Download/Installation

Once you have Discord4J all configured and ready to go, I recommend following along with this simple bot guide on the wiki.

That guide goes over how Discord4J works (subscribing to events, sending messages, etc.) and will give you a great starting point.

Helpful Discord4J Links:

https://discord4j.com/https://github.com/Discord4J/Discord4J/wiki

Your Next Steps in Java Bot Development

By now your Java bot development environment is all set up, configured, and ready to go!

If you’ve been following along, you’ve likely gone through one of the basic bot tutorials provided by the library/wrapper of your choice (either Discord4J or JavaCord), and I’m sure you’re getting excited about all of the potential bots you can create.

What should you do next?

Well, if you haven’t already, we recommend scrolling down to the bottom of this guide to the “Discord Bot Ideas” section and going through an idea generation exercise.

This will help you uncover any interesting and unique bot ideas that might’ve been hiding in your head all along.

Once you’ve done that, it’s up to you to start coding and making the Discord bot of your dreams.

If you want to learn even more about Discord bot development and learn even more tips, tricks, methods, and tactics on the subject of bot development, then join the WriteBots community today!

Leave a Reply