Making a Basic Discord Bot with Java

Oliy Barrett
Jun 19, 2018 · 5 min read
Image for post
Image for post

Creating a bot with JDA

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

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
Image for post
Image for post
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
Image for post
Image for post
Image for post
Image for post
Image for post

Step 4

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

Image for post
Image for post
Image for post
Image for post
Image for post
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
Image for post
Image for post
Image for post
Image for post
Image for post

Step 6

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

Image for post
Image for post
Image for post
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
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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
Image for post
Image for post
Image for post
Image for post
Image for post

Step 9

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

Image for post
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
Image for post
Image for post
Image for post

Note

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

Image for post
Image for post

Discord Bots

A blog for all information surrounding discord bots and our…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store