Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dec6540
Create en-US.md
Kipp-ie Aug 23, 2024
d737025
Create en_US.md
Kipp-ie Aug 23, 2024
2c5b91b
Update en_US.md
Kipp-ie Aug 23, 2024
0c44ed9
Update en_US.md
Kipp-ie Aug 23, 2024
771d13a
Update en_US.md
Kipp-ie Aug 23, 2024
f6d68fd
Create en_US.md
Kipp-ie Aug 23, 2024
1644cb4
Update en_US.md
Kipp-ie Aug 23, 2024
c4562c8
Update en_US.md
Kipp-ie Aug 23, 2024
9ed0d4e
Update en_US.md
Kipp-ie Aug 23, 2024
1700ab3
Create en-US.md
Kipp-ie Aug 23, 2024
dbb3935
Update en_US.md
Kipp-ie Aug 23, 2024
8c54d83
Update en_US.md
Kipp-ie Aug 23, 2024
9acb129
Update en-US.md
Kipp-ie Aug 23, 2024
483e03d
Update en-US.md
Kipp-ie Aug 23, 2024
38ba707
Create part-4.md
Kipp-ie Aug 23, 2024
a2d5b47
Rename jams/batches/java-discord-bot/part-4.md to jams/batches/java-d…
Kipp-ie Aug 23, 2024
9f47a4d
Add files via upload
Kipp-ie Aug 23, 2024
df4e0a3
Rename jams/batches/java-discord-bot/Untitled.pdf to jams/batches/jav…
Kipp-ie Aug 23, 2024
eb0b4b7
Delete jams/batches/java-discord-bot/pdf/part-1.pdf
Kipp-ie Aug 23, 2024
e7609f3
Update en_US.md
Kipp-ie Aug 23, 2024
a4900fb
Update en-US.md
Kipp-ie Aug 23, 2024
94ed35c
Update en_US.md
Kipp-ie Aug 23, 2024
b33be65
Update en-US.md
Kipp-ie Aug 23, 2024
4c99538
Update part-4.md
Kipp-ie Aug 23, 2024
9bafa23
Rename en-US.md to en_US.md
Kipp-ie Sep 7, 2024
ee802de
Update en_US.md
Kipp-ie Sep 7, 2024
bf7ad33
Update en_US.md
Kipp-ie Sep 7, 2024
d62268a
Update and rename en-US.md to en_US.md
Kipp-ie Sep 7, 2024
b854cda
Update and rename part-4.md to en_US.md
Kipp-ie Sep 7, 2024
2368ae4
Rename en_US.md to en-US.md
Kipp-ie Sep 7, 2024
d6ba91a
Rename en_US.md to en-US.md
Kipp-ie Sep 7, 2024
73415ed
Rename en_US.md to en-US.md
Kipp-ie Sep 7, 2024
6b7d4c3
Rename en_US.md to en-US.md
Kipp-ie Sep 7, 2024
871a440
Rename en_US.md to en-US.md
Kipp-ie Sep 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions jams/batches/java-discord-bot/part-1/en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
part: "part-1"
batch: 'java-discord-bot'
totalParts: '4'
title: 'Basics'
description: 'First we are going to register the bot at Discord and making the bot run.'
contributor: 'Kipp-ie'
contributorSlackID: 'U07HBU94Q1E'
thumbnail: 'https://github.com/user-attachments/assets/ef4418ba-c9c2-4648-92d5-8df8a198a713'
timeEstimate: '1 Hour'
difficulty: 'Beginner'
keywords: 'java, discord-bot'
presentation: 'https://www.figma.com/slides/XYXBH3tG4jlvZJMyFQCXsT/Untitled?node-id=5-297&t=OpU8bpu74eR06IRE-1'
presentationPlay: 'https://www.figma.com/deck/XYXBH3tG4jlvZJMyFQCXsT/Untitled?node-id=5-117&t=wFGI7lZsBox5fgGX-1&scaling=min-zoom&content-scaling=fixed&page-id=0%3A1'
presentationPDF: 'https://github.com/Kipp-ie/jamassets/blob/main/part-1.pdf'
---

# Hi 👋

You might have heard of Discord Bots before, if you don't i will give a short description for you. A Discord bot is a bot designed with multiple features for a Discord Server, think of a message when you join or sending random funny memes. Let's make one today!

First go to [Discord Developer Applications](https://discord.com/developers/applications). You'll probably need to sign in.

Click on **New Application**

![Discord Developer Home Page](https://cloud-6p1pvbaeh-hack-club-bot.vercel.app/0image.png)

You can choose any name wou want here. Proceed by clicking the **check box** to accepts Discord's TOS (You should read them!).

![Application Creation](https://cloud-cqn2d1ojp-hack-club-bot.vercel.app/0image.png)

Click on the page **Bot**. On this page you can customize the bot anyway you want!

![Bot page](https://cloud-ief8tg9tb-hack-club-bot.vercel.app/0image.png)

A bot token is how you can communicate with a Discord Bot, think of it like an account password. You need to keep this code safe, otherwise if someone else gets the code they can hijack your bot. Save this token for now in a notepad/word file.

![Resetting bot token](https://cloud-69uxismjr-hack-club-bot.vercel.app/0image.png)

You need to enable all these Privileged Gateway Intents, otherwise your bot won't function!

![Permissions](https://cloud-j9k09iazm-hack-club-bot.vercel.app/0image.png)

You have now changed all the settings needed, let's invite this bot to our server!

Click on the tab **OAuth2**

![OAuth](https://cloud-4upqdgtaj-hack-club-bot.vercel.app/0image.png)

Check **bot**

![Bot](https://cloud-1y24aht1v-hack-club-bot.vercel.app/0image.png)

Check **Administrator**

![Check administrator](https://cloud-fitsonnil-hack-club-bot.vercel.app/0image.png)

On the bottom click on **Copy**

![Copy LINK](https://cloud-5ze8tu4h1-hack-club-bot.vercel.app/0image.png)

Paste this link in a new tab and proceed by following the instructions.

# You now invited your own Discord Bot to your server, good job!

Let's now proceed by actually writing the code.

We will be using **JDA** (Java Discord API).

The recommended IDE for Java is **Intellij** but you can use any IDE you want (However i will be using Intellij so it may be harder to follow if you use another IDE).

Now, let's create a new project.

You can give the bot/project any name you want. When you chose a name make sure the project is set on Maven instead of Gradle. Click on **Create**.

At the left your files should look something like this.

![IntellIJ](https://cloud-f4tqhukr8-hack-club-bot.vercel.app/0image.png)

Open pom.xml.

And add this to the file:

``` xml
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.2</version> <!-- replace $version with the latest version -->
</dependency>
</dependencies>
```

Press Ctrl + Shift + Q

It should like like this.

![Example pom](https://cloud-8k7a2wspb-hack-club-bot.vercel.app/0image.png)

In main.java delete everything except package (your name).

Paste this in your main.java, make sure to replace BOT_TOKEN with the bot token we got earlier.

``` java
public class Main {



private ShardManager shardManager;

public Main() throws LoginException, InterruptedException {
DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(BOT_TOKEN);
builder.setStatus(OnlineStatus.DO_NOT_DISTURB);
builder.setActivity(Activity.watching("Hi hackclub!"));
builder.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES, GatewayIntent.MESSAGE_CONTENT);
shardManager = builder.build();
shardManager.addEventListener(
);


}

public static void main(String[] args) {
try {
Main bot = new Main();
} catch (LoginException e) {
System.out.println("Bot token invalid!");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
```
Click on the things that are red, then Alt + Enter and Import Class.

Also make sure that the bot token is between "" example: "dwah8esfsehfhsnvcb"

Now you can try running the main.java file and seeing if the bot boots.

If it boots, then the hardest part is behind you! If it doesn't try to retrace the steps to see if you maybe went somewhere wrong. Feel fry to dm me on Discord @kippenboutske or tag me/dm me on the Hack Club Slack: @Kipp-ie.

Ready for the next step? If so you can follow part 2!

















190 changes: 190 additions & 0 deletions jams/batches/java-discord-bot/part-2/en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
---
part: "part-2"
batch: 'java-discord-bot'
totalParts: '4'
title: 'Commands'
description: 'Creating some simple commands.'
contributor: 'Kipp-ie'
contributorSlackID: 'U07HBU94Q1E'
thumbnail: 'https://github.com/user-attachments/assets/01c63e14-014b-4dce-9b90-b3468deca8fd'
timeEstimate: '1 Hour'
difficulty: 'Beginner'
keywords: 'java, discord-bot'
presentation: 'https://www.figma.com/slides/OPzkpUPcVzR78sXNefuGLk/Untitled?node-id=1-238&t=E11NlCiFb5enyqGi-1'
presentationPlay: 'https://www.figma.com/deck/OPzkpUPcVzR78sXNefuGLk/Untitled?node-id=1-32&t=E11NlCiFb5enyqGi-0&scaling=min-zoom&content-scaling=fixed&page-id=0%3A1'
presentationPDF: 'https://github.com/Kipp-ie/jamassets/blob/main/part-2.pdf'
---

# 👋 Welcome back!

So our bot is kinda lifeless right now, let's give it some spark ;). So the way you communicate with Discord Bots are slash commands. Let's make some!

So first, we need to make a system that will register slash commands with the Discord Servers.

So, create a **package**, it's like a folder for Java.

Right click on the folder where main.java is located.

Click on New, then package.

Name it whatever you want but for simplicity you could call it Managers.

It should now look like this.

![Package Creation](https://cloud-36n8smf7u-hack-club-bot.vercel.app/0image.png)

Click create again, then **class**, name it SlashCommandManager (Or something else! Be creative ;).

So the point of the SlashCommandManager is communicating to the Discord Servers like "Hey Discord Servers, look at this cool command, okay now make sure this bot can use it".

You know how it works now, let's make it B).

Open SlashCommandManager, it should like like this:

``` java
public class SlashCommandManager {

}
```

add extends ListenerAdapter behind SlashCommandManager, like this!

``` java
public class SlashCommandManager extends ListenerAdapter {
}
```
Make sure your textcursor is between the brackets.

Click Ctrl + O

Search for OnGuildReady and click enter.

What CTRL + O does is it shows you what events your bot can pick up. For example onSlashCommand If someone does a slashcommand it sends a signal to the class that uses onSlashCommand, you can use that for example sending a message when someone does /message.

Remove super.onGuildReady(event);

It should now look like this:

``` java
public class SlashCommandManager extends ListenerAdapter {
@Override
public void onGuildReady(GuildReadyEvent event) {
}
}
```

So you can now register your commands in public void onGuildReady(GuildReadyEvent event) {}

Add this to onGuildReady:
List<CommandData> commands = new ArrayList<>();

and

event.getGuild().updateCommands().addCommands(commands).queue();

If anything becomes red click on it then Alt + Enter, please remember this, you will need it more in the future.

Now it should look like this:

``` java
public class SlashCommandManager extends ListenerAdapter {
@Override
public void onGuildReady(GuildReadyEvent event) {
List<CommandData> commands = new ArrayList<>();
# Place your slashcommands here!
event.getGuild().updateCommands().addCommands(commands).queue();
}
}
```

So now we can make some commands!

Place the command in between onGuildReady and event.getGuild().updateCommands().addCommands(commands).queue();

For making a command you do:

commands.add(Commands.slash("name of your command", "description of your command"));

The name should be all lowercase, description can be with anything.

Lets make the name "ping", the description can be anything you want (Let your imagination choose something ;))

Any class we make in Java we have to add to Main.java.

Like this:

![Main.java](https://cloud-rey8pf8mq-hack-club-bot.vercel.app/0image.png)

If you add more classes to the main make sure to sepperate them with ,

Like this

``` java
new pongcommand(),
new xcommand
```

# Let's try it out!

Run the bot, go to your Discord Server, and type /ping. Does it show up? If it doesn't please retrace your steps.

It should look like this.

![/ping test](https://cloud-o35vh3hmp-hack-club-bot.vercel.app/0image.png)

You have maybe noticed but if you send /ping it doesn't really do anything, that's because we need to create a Ping class. So i talked about events, we need to create a class that picks up SlashCommands with the name "ping" or whatever you chose.

You will get this error if you don't make a onSlashCommand.

![/ping error](https://cloud-udjw1d4l7-hack-club-bot.vercel.app/0image.png)

Let's do that!

So first create a Package called commands (Or whatever you want.).

Then create a class in the package with the name whatever you want, nameing the classes with what command it is will help.

Like this:

![Ping Class](https://cloud-q5vhpib42-hack-club-bot.vercel.app/0image.png)

In the class add extends ListenerAdapter\

Press CTRL + O and type onSlashCommand and click enter.

Remove super.onSlashCommandInteraction(event);

Okay this will get pretty complicated so read very good.

In onSlashCommand add:
``` java
if (event.getName().equals("ping")) {

}
```
So what this does is check is the slash command has the name **ping**
If it does we can reply!

With:

``` java
event.reply("Message what you want to reply with!").queue;
```

It should look like this now!

![ping command class](https://cloud-84sly80bm-hack-club-bot.vercel.app/0image.png)

Okay this class is pretty good look now, let's add it to our main class! (You should probably know how! If not scroll a bit up!)

Try /ping now! If you followed the instructions it should reply now instead of that error. If not retrace steps. Feel fry to dm me if you are stuck on Discord @kippenboutske or tag me/dm me on the Hack Club Slack: @Kipp-ie.

# Good job, you can create slashCommands now! Pretty epic, right?








Loading