2
0

More than 1 year has passed since last update.

初めてのjda-utilties (JavaのDiscordBot)

Last updated at Posted at 2022-01-12

使い方・初めてではまったことを書いていきます。
jda初心者なのでミスがあるかもしれないです。

appとbotの作成

まずDiscord Developer Portalに行きます。

https://discordapp.com/developers/applications/

  1. NewApplicationを押す
  2. 名前の入力
  3. create
  4. ここで画像とかを設定できます
  5. 左のメニューからbotを選択します
  6. AddBotを押す

これでbotができました。

botの招待urlの取得

  1. 左のメニューからOAuth2 > URL Generatorを選択します
  2. botを選択し、権限も設定します。
  3. Copyでurlをコピーできます。

tokenの取得

  1. 左のメニューのbotを選択します。
  2. アイコンの右にあるTOKENの下のCopyでコピーできます。

ownerIdの取得

  1. botのオーナーでログインします。
  2. 設定を開き詳細設定を開きます。
  3. 開発者モードをオンにします。
  4. マイアカウントを開きます。
  5. ユーザー名の隣の点々を押すと出てくるユーザーIdをコピーを押してコピーできます。

プロジェクトの作成

今回はmavenを使います
プロジェクト内のpom.xmlに依存関係を追記します
jdaとjda-utiltiesのバージョンがわかりにくかったのでurlを張っておきます。

jda: https://mvnrepository.com/artifact/net.dv8tion/JDA?repo=jcenter
jda-utlties: https://mvnrepository.com/artifact/com.jagrosh/jda-utilities?repo=jcenter

githubのurl

JDA: https://github.com/DV8FromTheWorld/JDA
jda-utilties: https://github.com/JDA-Applications/JDA-Utilities

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>groupeId</groupId>
    <artifactId>artifactId</artifactId>
    <version>version</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

<!--ここから追記-->
    <repositories>
        <repository>
            <id>central</id>
            <name>bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.2.0_247</version>
        </dependency>
        <dependency>
            <groupId>com.jagrosh</groupId>
            <artifactId>jda-utilities</artifactId>
            <version>3.0.5</version>
            <scope>compile</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
<!--ここまで-->
</project>

コードを書く

botにログインするコードを書きます。

import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
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;

import javax.security.auth.login.LoginException;

public class Bot {
    private static final String TOKEN = "token";
    private static final String OWNER_ID = "ownerId";
    private static final String COMMAND_PREFIX = "cmd";
    private static JDA jda;

    public static void main(String[] args) {
        CommandClient commandClient = new CommandClientBuilder()
                .setPrefix(COMMAND_PREFIX)
                .setOwnerId(OWNER_ID)
                .setStatus(OnlineStatus.ONLINE)
                .setActivity(Activity.playing("play中"))
                .build();
        try {
            jda = JDABuilder.createDefault(TOKEN)
                    .addEventListeners(commandClient)
                    .build();
        } catch (LoginException e) {
            e.printStackTrace();
        }
    }
}

最後に

ネットに転がっている情報だと古くてうまく動かなかったり苦労しました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0