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!
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>
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.
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()
}
OnlineStatus.ONLINE //online
OnlineStatus.OFFLINE //off-line
OnlineStatus.IDLE //Away
OnlineStatus.DO_NOT_DISTURB //Busy
OnlineStatus.INVISIBLE //Hide online status
Activity.watching("name") //Watching name
Activity.listening("name") //playing name
Activity.streaming("name", "url") //delivering name
Activity.playing("name") //playing name
This time, I will try to implement the command "Greeting to the person who executed 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
}
}
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