Making a Basic Discord Bot with Java

Making a Basic Discord Bot with Java

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

Step 4

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

Image for post

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

Image for post

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

Image for post

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

Image for post

Give your main class a name and click on OK

Image for post

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

Image for post

Step 6

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

Image for post

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

Image for post

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()

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

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

Image for post

Step 9

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

Image for post

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

Image for post

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

Image for post

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

Note

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

Image for post

12

No Responses

Write a response