DEV Community

Ashley Tonkin
Ashley Tonkin

Posted on

[Part 1] Getting Started

welcome

I'm guessing you are here because you would like to learn on how to get started making your own discord bot with Java.
You have come to the right place today we will be using the JDA (Java Discord API) built by DV8FromTheWorld and JDA-Utilities built by jagrosh

Before we jump into the code, make sure you have an IDE or text editor with the ability to compile Java.
I'll be using IntelliJ IDEA by Jetbrains some others you can use are Eclipse, Netbeans, VS Code* you will need to install plugins to compile Java

Ok now you have the tools to be able to compile Java lets jump into the code, for this project I will be using Maven as my builder.

We need to start by creating a new project
create_new
We would like to create a Maven project and select our project JDK (Java Development Kit) If you don't have a JDK installed i have provided a link.
maven_project
Here we need to pop the Artifact Coordinates down, Name your project, select the file destination to save your project.
Under Artifact Coordinates
GroupId: This is usually a domain backwards Eg. to.dev
ArtifactId: is normally the name of the project.
Version: I hope is self explanatory
maven_project_02
Click next and you will be greeted with your first file, Its a .xml file called pom, should look at something like this

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>tv.ashdev</groupId>
  <artifactId>DevToProject</artifactId>
  <version>0.0.1</version>


</project>
Enter fullscreen mode Exit fullscreen mode

Here is where we add our JDA and JDA-Utilities
JDA Maven

<dependency>
    <groupId>net.dv8tion</groupId>
    <artifactId>JDA</artifactId>
    <version>VERSION</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode
<repository>
    <id>jcenter</id>
    <name>jcenter-bintray</name>
    <url>https://jcenter.bintray.com</url>
</repository>
Enter fullscreen mode Exit fullscreen mode

JDA-Utilities

<dependency>
  <groupId>com.jagrosh</groupId>
  <artifactId>jda-utilities</artifactId>
  <version>JDA-UTILITIES-VERSION</version>
  <scope>compile</scope>
  <type>pom</type>
</dependency>
Enter fullscreen mode Exit fullscreen mode

We would like to wrap the dependencies in a tag together conveniently the tag we need is <dependencies></dependencies>
We also need to put the repository in a new tag as well <repositories></repositories>
Be sure to add the current build versions

After all that you should have something that looks like this.
pom.xml

Now that our pom file is finished and out of the way, we need to create a new Java class file, On the left hand side you want to drop down your project folder -> src -> main
Inside the main folder you should see a java folder and a resources folder.
You need to right click on the java folder -> new -> Java Class
I have called mine the same as my project DevToProject

This is what i have currently a new java class

public class DevToProject {

}
Enter fullscreen mode Exit fullscreen mode

We need to add the main method to the class so it knows what to run when we compile and run the bot

public static void main(String[] args) {

}
Enter fullscreen mode Exit fullscreen mode

That needs to be added inside of the class brackets
From there we need to first create our bot for discord head over to the discord developer portal and create a new application i called mine DevTo just for this project.

We would like to create a bot so lets click on the bot under settings on the left hand side and go Add Bot and accept.

Now that we now have a bot we need something before we leave the page, that is the bot token. You must keep this to yourself other wise anyone else can take control of your bot.
bot_token

Great we are nearly finished, we will have the bot up and running in no time.

At the top within our main method we would like to start to add our JDA-Utilities code.
We need to create a new EventWaiter and CommandClientBuilder.

EventWaiter waiter = new EventWaiter();
CommandClientBuilder client = new CommandClientBuilder()
Enter fullscreen mode Exit fullscreen mode

before we add the ; at the end of our CommandClientBuilder we need to add some more code.
We would like to add a command prefix, Bot Status. Owner Id and Activity

//JDA-Utilities
    EventWaiter waiter = new EventWaiter();
    CommandClientBuilder client = new CommandClientBuilder()
        .setPrefix("-")
        .setStatus(OnlineStatus.ONLINE)
        .setOwnerId("160269653136900096")
        .setActivity(Activity.playing("with other bots"));
Enter fullscreen mode Exit fullscreen mode

To get your ID you need to enable Dev Mode in discord
Play around with the online status and activity as you can do a lot of fun things with them.

Now lets build the bot using JDA, this can be tricky as the examples on the github page and within the wiki haven't been updated 100% yet to implement the new JDABuilder.

 JDA api = JDABuilder
        .createDefault("BOT TOKEN HERE")
        .addEventListeners(waiter, client.build())
        .build();
api.awaitReady();
Enter fullscreen mode Exit fullscreen mode

WAIT BEFORE WE BUILD OUR BOT
we need to add something to our pom file before out dependencies.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>11</source>
        <target>11</target>
      </configuration>
    </plugin>
  </plugins>
</build>
Enter fullscreen mode Exit fullscreen mode

So it looks like this
new_pom_file

Building the bot, top right hand corner you'll see a button that says Add Configuration, click that and a new pop up window should appear.
config
Click the + top left and select application
Sometimes it will add the Main class sometime's it doesn't if it doesn't click on ... and select the main class we created earlier mine was called DevToProject once that has been set, click apply then ok.

Now next to out configuration button we have a green play button.
build
That will build the bot and put it online, let's not worry about the SLF4J messages at the moment. Let's add our bot to our discord server.

Back over to the discord developer portal -> select our bot -> OAuth2
from here we need to select bot and just for simplicity lets just give the bot admin permissions.
bot_add
Copy the link it gives you paste it in a new browser window.
bot_add_server
select your server, authorize the bot, go thought the google reCAPTCHA and you are done.

If you have made it this far i thank you.
Please let me know if i have missed anything and I will update the post.

Top comments (1)

Collapse
 
thelightmoon profile image
TheLightMoon

when we have part 2?