[Java / kotlin] Kai-How to make a Discord Bot-Let's implement a command to say hello-

A long time ago, I introduced how to make a Discord Bot under the title of "How to make a Discord Bot (Java)", but the support for the library has expired and now I can not make a bot by referring to that article. And it seems.

It seems that the demand for Discord is gradually increasing these days when we are forced to study at home and online classes, but we will introduce how to make "Bot" to make it even more convenient.

In this article, I'll show you how to write in Java and kotlin!

environment

Procure library

Please write the following contents in `<dependencies> </ dependencies>` of pom.xml.

        <!--Main library (required)-->
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.1.1_101</version>
        </dependency>

        <!--Library for command processing (required)-->
        <dependency>
            <groupId>com.jagrosh</groupId>
            <artifactId>jda-utilities</artifactId>
            <version>3.0.3</version>
            <scope>compile</scope>
            <type>pom</type>
        </dependency>

        <!--If you use kotlin, these two-->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>1.3.72</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>1.3.72</version>
            <scope>test</scope>
        </dependency>

Create a bot account and invite to the server

Since the explanation will be long, get the token by referring to my article ([here](https://qiita.com/itsu_dev/items/825e4ed32bf32e4b64b2# extra edition-obtaining the application registration token)) and put it on the server Please invite me.

Login / authentication part

Anyway, if you don't log in as a bot, nothing will start, so let's log in to Discord using the token you got earlier!

Run the code below and the bot will be online if everything goes well.

Java

Main.java


package dev.itsu.discordbot;

import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.command.CommandClient;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;

class Main {
    
    private static JDA jda;
    private static final String TOKEN = "MY_TOKEN"; //Obtained bot token
    private static final String COMMAND_PREFIX = "!"; //Command prefix

    public static void main(String args[]) {
        //Generate an event listener that handles commands
        CommandClient commandClient = new CommandClientBuilder()
            .setPrefix(COMMAND_PREFIX) //Command prefix
            .setStatus(OnlineStatus.ONLINE) //Online status settings
            .setActivity(Activity.watching("YouTube")) //Status settings (watching, playing, etc.)
            .build();
        
        jda = new JDABuilder(AccountType.BOT)
            .setToken(TOKEN) //Set token
            .addEventListeners(commandClient) //set commandClient
            .build();
    }

}

kotlin

Main.kt


package dev.itsu.discordbot

import com.jagrosh.jdautilities.command.CommandClientBuilder
import net.dv8tion.jda.api.AccountType
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.OnlineStatus
import net.dv8tion.jda.api.entities.Activity

private lateinit var jda: JDA
private const val TOKEN = "MY_TOKEN" //Obtained bot token
private const val COMMAND_PREFIX = "!" //Command prefix

fun main() {
        //Generate an event listener that handles commands
        val commandClient = CommandClientBuilder()
                .setPrefix(COMMAND_PREFIX) //Command prefix
                .setStatus(OnlineStatus.ONLINE) //Online status settings
                .setActivity(Activity.watching("YouTube")) //Status settings (watching, playing, etc.)
                .build()

        jda = JDABuilder(AccountType.BOT)
                .setToken(TOKEN) //Set token
                .addEventListeners(commandClient) //set commandClient
                .build()
}

Status change

Values that can be set for online status

OnlineStatus.ONLINE //online
OnlineStatus.OFFLINE //off-line
OnlineStatus.IDLE //Away
OnlineStatus.DO_NOT_DISTURB //Busy
OnlineStatus.INVISIBLE //Hide online status

Value that can be set for status

Activity.watching("name") //Watching name
Activity.listening("name") //playing name
Activity.streaming("name", "url") //delivering name
Activity.playing("name") //playing name

Try using the command

This time, I will try to implement the command "Greeting to the person who executed the command".

Implement the command

To implement the command, you need to create a class that inherits from om.jagrosh.jdautilities.command.Command.

Java

HelloCommand.java


package dev.itsu.discordbot

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;

class HelloCommand extends Command {

    public HelloCommand() {
        this.name = "hello"; //Set the command name (!Can be executed with hello)
        this.help = "Just say hello"; //Command description (!Explanation displayed when you execute help)
    }

    //Method called when executing a command
    @Override
    public void execute(CommandEvent event) {
        event.reply("Hello," + event.getAuthor().getName() + "San!"); //reply
    }

}

kotlin

HelloCommand.kt


package dev.itsu.discordbot

import com.jagrosh.jdautilities.command.Command
import com.jagrosh.jdautilities.command.CommandEvent

class HelloCommand : Command() {

    init {
        name = "hello" //Set the command name (!Can be executed with hello)
        help = "Just say hello" //Command description (!Explanation displayed when you execute help)
    }

    //Function called when the command is executed
    override fun execute(event: CommandEvent) {
        event.reply("Hello,${event.author.name}San!") //reply
    }

}

Register the command

You can't execute a command just by creating a class. In order to be able to execute commands, it is necessary to register the implemented command class in CommandClient. Go back to Main.java (Main.kt) and make the following changes.

If you run this, "Hello, (user name) 's!" When you run the "! Hello" in the Discord us back is Bot with!

Java

Main.java


package dev.itsu.discordbot;

import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.command.CommandClient;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;

class Main {
    
    private static JDA jda;
    private static final String TOKEN = "MY_TOKEN";
    private static final String COMMAND_PREFIX = "!";

    public static void main(String args[]) {
        CommandClient commandClient = new CommandClientBuilder()
            .setPrefix(COMMAND_PREFIX)
            .setStatus(OnlineStatus.ONLINE)
            .setActivity(Activity.watching("YouTube")
            .addCommands(new HelloCommand()) //Register the command here!
            .build();
        
        jda = new JDABuilder(AccountType.BOT)
            .setToken(TOKEN)
            .addEventListeners(commandClient)
            .build();
    }

}

kotlin

Main.kt


package dev.itsu.discordbot

import com.jagrosh.jdautilities.command.CommandClientBuilder
import net.dv8tion.jda.api.AccountType
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.OnlineStatus
import net.dv8tion.jda.api.entities.Activity

private lateinit var jda: JDA
private const val TOKEN = "MY_TOKEN"
private const val COMMAND_PREFIX = "!"

fun main() {
        val commandClient = CommandClientBuilder()
                .setPrefix(COMMAND_PREFIX)
                .setStatus(OnlineStatus.ONLINE)
                .setActivity(Activity.watching("YouTube"))
                .addCommands(HelloCommand()) //Register the command here!
                .build()

        jda = JDABuilder(AccountType.BOT)
                .setToken(TOKEN)
                .addEventListeners(commandClient)
                .build()
}

Actually, the addCommands () method (function) takes variable length arguments, so you can register any number of other implemented commands as shown below.

addCommands(HelloCommand(), StatusCommand())

If you have any other features you would like us to introduce, or if you have any suggestions, please leave a comment.

Recommended Posts

[Java / kotlin] Kai-How to make a Discord Bot-Let's implement a command to say hello-
How to make a Discord bot (Java)
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to make a Java array
How to make a Java calendar Summary
I did Java to make (a == 1 && a == 2 && a == 3) always true
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to implement it additionally while using kotlin on a site running Java
I tried to make a login function in Java
Introduction to java command
[Beginner] Try to make a simple RPG game with Java ①
I just wanted to make a Reactive Property in Java
How to deploy a kotlin (java) app on AWS fargate
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I want to implement various functions with kotlin and java!
Java --How to make JTable
[Java] Make it a constant
[Java] How to implement multithreading
Make a rhombus using Java
How to implement a job that uses Java API in JobScheduler
How to run a Kotlin Coroutine sample from the command line
[Java] I tried to make a maze by the digging method ♪
How to make a groundbreaking diamond using Java for statement wwww