綺麗に死ぬITエンジニア

ModなしでMinecraftサーバーとDiscordを連携するツールを作った

2018-04-06

最近、ゲーマー向けのチャット/ボイスチャットアプリである、Discordが色んなところで勧められていて、ゲーマー界隈ではSkypeに代わってDiscordがどんどんメジャーになってきています。

そんな中、Minecraftサーバー(バニラ)を運用する機会があったので、例によってDiscordと連携させてみようと思いついたわけですが、ググってみると別途Modやら入れる必要があるツールばかりのようで、バニラサーバーでは導入できないものばかりでした。

ないなら作っちまえ!ということで、バニラのMinecraftサーバーで、Discordとチャットを共有したり、Minecraftへのログイン等のメッセージをDiscordへ転送するツールを作成し、MITライセンスで公開しましたので紹介します。

Minecord

Minecord

MinecraftとDiscordを連携するので、合わせてMinecordと命名しました。

GitHubはこちら。プラグインの追加等、開発に貢献してくださると非常にありがたいです。

インストール

MinecordはJavaScriptで実装されていますので、利用にはNode.js環境が必須です。導入されていない場合はまずそちらをインストールしてください。

以下のコマンドを実行し、Minecord本体をインストールします。

npm install -g minecord

これで、minecordが利用できるようになったかと思います。

minecord --help

  Usage: minecord [options]

  Options:

    -V, --version                         output the version number
    -c, --config <file>                   set configuration file
    -p, --plugins-dir <dir>               set local plugins directory
    --enable <plugins>                    enable plugin by name, "--enable PLUGIN1,PLUGIN2" for multiple plugins
    --disable <plugins>                   disable plugin by name, "--disable PLUGIN1,PLUGIN2" for multiple plugins
    --minecraft-log <path>                set the path to Minecraft log (It is recommended to specify them collectively in the configuration file)
    --minecraft-rcon-host <host>          set the Minecraft Server rcon host (It is recommended to specify them collectively in the configuration file)
    --minecraft-rcon-port <port>          set the Minecraft Server rcon port (It is recommended to specify them collectively in the configuration file)
    --minecraft-rcon-password <password>  set the Minecraft Server rcon password (It is recommended to specify them collectively in the configuration file)
    --discord-bot-token <token>           set Discord bot token (It is recommended to specify them collectively in the configuration file)
    --discord-channel <id>                set Discord channel ID for for the discord bot (It is recommended to specify them collectively in the configuration file)
    -h, --help                            output usage information

使い方

まず、Minecraftサーバーのserver.propertiesを編集し、以下の記述に変更しましょう。

enable-rcon=true
rcon.password=[PASSWORD]
rcon.port=[PORT]

[PASSWORD]の部分には適当な文字列(該当ポートが解放されている場合はセキュリティの観点から十分な長さがあるランダムな文字列が良い)を指定し、[PORT]の部分には1から65535の中から解放するポート番号を記述します。ポート番号に迷ったら25575で良いと思います。

次に、Discordのbotを登録します。

こちらからDeveloper用サイトに移動し、[My Apps] → [New App]をクリックします。

次に[APP NAME]に好きな名前を書きます。「Minecraft」等の名前で良いかと思います。

そしてBotの欄で[Create a Bot User]をクリックして[Yes, do it]を選択します。その後、Botの欄の[Token]を確認します。後で使いますので、メモしてください。

[Generate OAuth2 URL]を押してBotだけがチェックされた状態で、下の方に記載されているURLに飛びます。その後、連携するDiscordサーバーを選択してください。

Botの登録が済んだら、以下の内容を持ったconfig.jsonという名称の設定ファイルを任意の位置に配置してください。

{
  "pluginsDir": null,
  "enable": [
    "chat",
    "login",
    "whitelist",
    "server"
  ],
  "disable": [],
  "minecraftLog": "/var/minecraft/logs/latest.log",
  "minecraftRconHost": "localhost",
  "minecraftRconPort": 25575,
  "minecraftRconPassword": "password",
  "discordBotToken": "bot_token",
  "discordChannel": "channel_id"
}

enableは、有効にするプラグインを指定します。とりあえず上記の4つのプラグインを有効にしておけばいいでしょう。

minecraftLogは、Minecraftサーバーのログへのパスを指定してください。

minecraftRconPortは、先ほど指定したMinecraftサーバーのrconのポート番号を指定します。

minecraftRconPasswordは、先ほど指定したMinecraftサーバーのrconのパスワードを指定します。

discordBotTokenは、先ほど取得したDiscordのBotのTokenを指定します。

discordChannelは、Botを動作させるDiscordのチャンネルIDを指定します。Discordの[設定] → [テーマ]の詳細設定から開発者モードをONにし、IDを取得したいチャンネルを右クリックで[IDをコピー]できます。

config.jsonの記述が終わったら、以下のコマンドでMinecordを起動できます。

minecord --config /path/to/config.json

これでMinecraftとDiscordのチャットの共有がされますので、可能な場合はDiscordのユーザーニックネーム(チャンネルの管理者が自由に変更できます)をMinecraftのユーザー名と同一にすると、より見やすくなるでしょう。

Minecordをデーモン化(永続化)する場合は、以下の記事を参考にしてください。

プラグインの作り方

Minecordのオプション--plugins-dirや設定ファイルのpluginsDirに指定したディレクトリにプラグインスクリプトを配置すると、動作を追加することができます。

プラグインスクリプトは以下のように定義します。(以下はBabelで記述していますので、コンパイルしてから配置してください。)

export default Plugin => new Plugin({
  discord ({message, channel, user, sendToDiscord, sendToMinecraft}) {
    // Processing when receiving a message from Discord.
  },
  minecraft ({log, time, causedAt, level, message, channel, user, sendToDiscord, sendToMinecraft}) {
    // Processing when receiving a message from Minecraft.
  }
})

discord()メソッド

このメソッドは、Discordのチャンネルに新規メッセージを検出したときに実行されます。

引数messagediscord.jsMessageオブジェクトです。

引数channeldiscord.jsTextChannelオブジェクトです。

引数userdiscord.jsUserオブジェクトです。

引数sendToDiscorddiscord.jssendメソッドです。

引数sendToMinecraftnode-modern-rconsendメソッドです。Minecraftのコマンドを入力します。

minecraft()メソッド

このメソッドは、新しいMinecraftのログを検出したときに実行されます。

引数logは、新しく追加されたログ1行です。

引数timecausedAtlevelおよびmessageは、ログをパースして得られた値です。例えば、以下のようになります。

[01:23:45] [Server thread/INFO]: player joined the game
export default Plugin => new Plugin({
  minecraft ({log, time, causedAt, level, message, channel, user, sendToDiscord, sendToMinecraft}) {
    console.log(log === '[01:23:45] [Server thread/INFO]: player joined the game')
    console.log(time === '01:23:45')
    console.log(causedAt === 'Server thread')
    console.log(level === 'INFO')
    console.log(message === 'player joined the game')
    // 全て true
  }
})

引数channeldiscord.jsTextChannelオブジェクトです。

引数userdiscord.jsUserオブジェクトです。

引数sendToDiscorddiscord.jssendメソッドです。

引数sendToMinecraftnode-modern-rconsendメソッドです。Minecraftのコマンドを入力します。

今は最低限のプラグインしか存在しないため、プラグインを開発した方は、Issuesの方にコードを共有していただけると助かります。

便利なものは、積極的にデフォルトプラグインとして取り込んでいきたいと思います。

まとめ

ということで、Minecordの紹介でした。

バグや改善点等見つけた方は、IssuesTwitterの方にお寄せいただけると助かります。プルリクもお待ちしております。

特に、README.mdの英語の添削をしてくださる方がいましたら、非常にありがたいです……。