Making a Basic Discord Bot with Java

Oliy Barrett
Jun 19, 2018 · 5 min read

Creating a bot with JDA

We’ll build a really basic bot with the JDA discord API wrapper.

In this tutorial, we’ll use the IntelliJ IDEA IDE, created by Jetbrains.

This tutorial assumes you have a JDK 8+ installed and the JAVA_HOME environment variable is set to it.

Step 1

Download and install IDEA (The community edition is enough)

Step 2

Once you open IDEA, you’ll see this screen

Click on , then select and mark just , like this

Step 3

Choose a group and artifact ID, they can be anything you want, but usually the group id is the reverse of a domain you own, so becomes and the artifact id is an identifier for the project, such as

You don’t need to change any gradle settings, but personally I like to enable auto import.

After that, choose a project name and where to save it and then click on .

Step 4

Wait for gradle to finish configuring your project, and you should see a screen like this

Open build.gradle and let’s add JDA as a dependency (check the latest version here)

If you get a dialog like this showing up, click (it won't show up if you enabled auto import)

Step 5

Now, we’ll create our main class. Open in the file viewer and right click on , then go to New -> Java Class

Give your main class a name and click on

Now, we’ll create a main method (hint: type until a popup shows and then hit enter)

Step 6

Now, we’ll create our JDA instance, using the JDABuilder class

To continue, you need a discord bot token, which you can get on the applications page

Add the token to your JDABuilder using the method

Step 7

It’s finally time to log in to discord with your bot! To build the JDA instance and connect to discord, simply call the method

You’re probably wondering why that line is red. If we hover the mouse above it, we’ll see a message explaining what’s wrong

An exception, huh? For now, we’ll just declare the main method as

Now, click the green play button to the left of our class name and select

You’ll see something like this being printed to your console

The first 6 lines are because we don’t have any slf4j implementations on our project, but we can ignore those for now.

The next 3 lines tell us that JDA has successfully logged in to discord, and is ready to receive messages. But our bot doesn’t do anything right now.

Step 8

To have our bot do something, we need to add a listener to our JDA instance. For now, let’s have our main class extend and override the onMessageReceived method

Now, let’s make it reply with if the message is

To finalize, we register a new instance of our main class in the JDABuilder

Step 9

Now, when we re-run out bot, it’ll respong with when we run

Step 10

Now that you know how to build a basic bot, we need to export it into a runnable jar file. To do so, we’ll use the gradle shadow plugin. Here’s how our build.gradle looks now

To export our project, open the gradle tab on the right and double click on Tasks->shadow->shadowJar

The file will be in PROJECT_ROOT/build/libs, and can be ran with the command:

Note

If you don’t want to risk your bot entering into infinite message loops, add this to your listener

Discord Bots

A blog for all information surrounding discord bots and our…