[Part 1] Getting Started
Tags: java, beginners, codenewbie
Published: 2020-03-31 16:07:17

#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**](https://github.com/DV8FromTheWorld) and JDA-Utilities built by [**jagrosh**](https://github.com/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](https://www.jetbrains.com/) some others you can use are [Eclipse](https://www.eclipse.org/), [Netbeans](https://netbeans.org/), [VS Code](https://code.visualstudio.com/)* 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](https://www.oracle.com/java/technologies/javase-downloads.html) (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
<?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>
```
Here is where we add our JDA and JDA-Utilities
JDA Maven
```xml
<dependency>
    <groupId>net.dv8tion</groupId>
    <artifactId>JDA</artifactId>
    <version>VERSION</version>
</dependency>
```
```xml
<repository>
    <id>jcenter</id>
    <name>jcenter-bintray</name>
    <url>https://jcenter.bintray.com</url>
</repository>
```
JDA-Utilities
```xml
<dependency>
  <groupId>com.jagrosh</groupId>
  <artifactId>jda-utilities</artifactId>
  <version>JDA-UTILITIES-VERSION</version>
  <scope>compile</scope>
  <type>pom</type>
</dependency>
```

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
```java
public class DevToProject {

}
```
We need to add the `main` method to the class so it knows what to run when we compile and run the bot
```java
public static void main(String[] args) {

}
```
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](https://discordapp.com/developers/) 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.
```Java
EventWaiter waiter = new EventWaiter();
CommandClientBuilder client = new CommandClientBuilder()
```
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
```Java
//JDA-Utilities
    EventWaiter waiter = new EventWaiter();
    CommandClientBuilder client = new CommandClientBuilder()
        .setPrefix("-")
        .setStatus(OnlineStatus.ONLINE)
        .setOwnerId("160269653136900096")
        .setActivity(Activity.playing("with other bots"));
```
To get your ID you need to enable [Dev Mode](https://www.discordia.me/en/developer-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.
```Java
 JDA api = JDABuilder
        .createDefault("BOT TOKEN HERE")
        .addEventListeners(waiter, client.build())
        .build();
api.awaitReady();
```
WAIT BEFORE WE BUILD OUR BOT
we need to add something to our pom file before out dependencies.
```xml
<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>
```
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.

[create_new]: https://i.ibb.co/B317p20/create-new-project.png
[maven_project]: https://i.ibb.co/tDqmLwS/maven-project.png
[maven_project_02]: https://i.ibb.co/VwXwhmt/maven-project-02.png
[pom.xml]: https://i.ibb.co/25LfjxS/Screenshot-5.png
[bot_token]: https://i.ibb.co/4SCFB3w/Screenshot-7.png
[new_pom_file]: https://i.ibb.co/Jzd7N50/Screenshot-8.png
[config]: https://i.ibb.co/ykxjgKJ/Screenshot-9.png
[build]: https://i.ibb.co/J2130Bv/Screenshot-11.png
[bot_add]: https://i.ibb.co/JjBXfcM/Screenshot-12.png
[bot_add_server]: https://i.ibb.co/KmkWwXX/Screenshot-13.png