Java Discord セットアップ〜Hello Discord〜

イントロダクション

前回、見つけたサイトでGladleでの環境セットアップを行なっていたのでそのようにやったのですが、結局Gitからの情報だったのでGitへ移動したところMavenを使用していたので、やり直します。※しかしサイトは古い画像が載っています。

最後に作成したものがうまくいったものです。

Introduction

Last time, I found web page write about setting up for git in Intellij using gradle.resently wrote in git. so i found how to set up using maven. result of all, i setted up using maven.(pom.xml)

success file is Last of this page.

Maveでセットアップ

pom.xmlを作成してビルドをかけると必要なライブラリなどがインストール出来ます。

プロジェクトの作成は下のページを参照してください。
こちらを参照してください※こっちの方が正しい情報があります。「正しい」というのはちゃんとインポートできるということです。

Setting by Maven

we can download libraries easy using maven.just build using pom file.

how to create project is “こちらを参照”.

※インポートできない時は下の様にMavenをUpdate(更新)してください。そして、最後にプロジェクトのビルドをおこなってください。。。


以下のように、POMファイルを修正します。
※上にあるGItへのリンクを参照

change pom file as bellow.sight is 参照

そして、修正すると以下のように「Import Changes」のように表示されるのでそれをクリックします。

下のようにコピペしたままだと赤くりなますので最新のバージョンを設定します。

check the version in pom file.

自分が調べたところでは、javacord(やっぱりGit)には以下のように記載がありました。

there are many kind of libs in that site so i use “javacord”.

ここには「2.0.17」とバージョンの記載があったのでそれを記載し「Import Changes」をクリックします。

しかし、赤いままになってしまいます。

リポジトリが足りませんでした。。。

I did no write repository.

上記のように修正し再度「Import Changes」をクリック。。。がダメでした。

but I can not download ...

仕方ないので以下のサイトのPOMファイルのPOMで再度挑戦

実際に起動したPOMファイルは下になります。結果はエラーになりました。

I can not download libs ...

<?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>zenryokuservice.discord</groupId>
    <artifactId>bot.prac</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
        <repository>
            <id>javacord-repo</id>
            <url>http://repo.bastian-oppermann.de</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>de.btobastian.javacord</groupId>
            <artifactId>javacord</artifactId>
            <version>2.0.17</version>
            <!-- This will use the shaded javacord which contains all required dependencies -->
            <classifier>shaded</classifier>
        </dependency>
        <!-- A SLF4J comaptible logging framework. I would recommend to use logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
    <!-- 2018/07/16追記 コマンドフレームワーク追加-->
        <dependency>
           <groupId>com.discord4j</groupId> <!-- However our packages are all under sx.blah in version 2.x! -->
           <artifactId>Discord4J</artifactId>
           <version>2.10.1</version>
           </dependency>
        <dependency>
           <groupId>de.btobastian.sdcf4j</groupId>
           <artifactId>sdcf4j-core</artifactId>
           <version>1.0.9</version>
        </dependency>
    <!-- The module for your preferred lib -->
        <dependency>
           <groupId>de.btobastian.sdcf4j</groupId>
           <!-- Possible artifact ids: sdcf4j-javacord, sdcf4j-jda3, sdcf4j-discord4j -->
           <artifactId>sdcf4j-javacord</artifactId>
           <version>2.0.17</version>
        </dependency>
   </dependencies>
</project>

そして、サンプルコードをコピーして実行したいのですが、ビルドエラーが出ます。

足りないライブラリは以下です。

This is my lost lib.(included this class)

om.google.common.util.concurrent.FutureCallback;

URL
https://mvnrepository.com/artifact/com.google.guava/guava

最終的に、作成したコードです。(Success file)

public class HelloDiscord {
    public static void main(String[] args) {
// See "How to get the token" below
        DiscordAPI api = Javacord.getApi("トークンを記述", true);
        api.setEmail("メールアドレス"); // 2018/07/15追記 
        api.setPassword("パスワード"); // 2018/07/15追記
        // connect 
       api.connect(new FutureCallback<DiscordAPI>() {
          public void onSuccess(DiscordAPI api) {
             // register listener 
             api.registerListener(new MessageCreateListener() {
                public void onMessageCreate(
                       DiscordAPI api, Message message) { 
                          // check the content of the message 
                          if (message.getContent().equalsIgnoreCase("ping")) { 
                              // reply to the message 
                              message.reply("pong"); } } });
                      }
                public void onFailure(Throwable t) {
                    t.printStackTrace();
                }
           }
        ); 
   }
}

次回は、トークン(認証情報)を取得して実際にログインします。

関連ページ一覧

Java Discord

  1. IntelliJ IDEA Discord Botを作る〜Gradle環境のセットアップ〜
  2. Java Discord セットアップ〜Hello Discord〜
  3.  Java Discord ピンポン〜Discordプログラム〜
  4. Java Discord Listener実装〜コマンドを好きなだけ追加しよう〜

JavaDoc

    1. Java Doc 読解〜System.out〜
    2. JavaDoc 読解 〜 Filesクラス 〜
    3. Java Doc読解 BufferedReader
    4. Java Doc 読解〜BufferedWriter〜
    5. Java Doc 読解 List 〜JavaDocList その1〜
    6.  Java Doc 読解 Map

[rakuten ids="book:18900891"]

投稿者:

takunoji

音響、イベント会場設営業界からIT業界へ転身。現在はJava屋としてサラリーマンをやっている。自称ガテン系プログラマー(笑) Javaプログラミングを布教したい、ラスパイとJavaの相性が良いことに気が付く。 Spring framework, Struts, Seaser, Hibernate, Playframework, JavaEE6, JavaEE7などの現場経験あり。 SQL, VBA, PL/SQL, コマンドプロント, Shellなどもやります。

コメントを残す