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.
Download and install IDEA (The community edition is enough)
Once you open IDEA, you?ll see this screen
Click on Create New Project, then select Gradle and mark just Java, like this
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
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 Finish.
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 Import Changes (it won’t show up if you enabled auto import)
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
Give your main class a name and click on OK
Now, we?ll create a main method (hint: type psvm until a popup shows and then hit enter)
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 setToken(String) method
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()
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 throws LoginException
Now, click the green play button to the left of our class name and select Run
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.
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
Now, let?s make it reply with Pong! if the message is !ping
To finalize, we register a new instance of our main class in the JDABuilder
Now, when we re-run out bot, it?ll respong with Pong when we run !ping
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 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