Discord Bots Blog
Get your latest news about Discord Bot List here!

Making a basic Bot

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 Yes I know it's outdated

Click on Create New Project, then select Gradle and mark just Java, like this Screenshot

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 mywebsite.com becomes com.mywebsite and the artifact id is an identifier for the project, such as my-jda-bot Screenshot

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

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

Step 4

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

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

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

Step 5

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

Give your main class a name and click on OK Screenshot

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

Step 6

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

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

Add the token to your JDABuilder using the setToken(String) method Screenshot

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 JDABuilder#buildAsync() Screenshot

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 Screenshot

An exception, huh? For now, we'll just declare the main method as throws LoginException Screenshot

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

You'll see something like this being printed to your console Screenshot

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 ListenerAdapter and override the onMessageReceived method Screenshot

Now, let's make it reply with Pong! if the message is !ping Screenshot

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

Step 9

Now, when we re-run out bot, it'll respong with Pong when we run !ping


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 Screenshot

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

The file will be in PROJECT_ROOT/build/libs, and can be ran with the java command: java -jar ourjar.jar


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