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のチャンネルに新規メッセージを検出したときに実行されます。
引数message
はdiscord.jsのMessage
オブジェクトです。
引数channel
はdiscord.jsのTextChannel
オブジェクトです。
引数user
はdiscord.jsのUser
オブジェクトです。
引数sendToDiscord
はdiscord.jsのsend
メソッドです。
引数sendToMinecraft
はnode-modern-rconのsend
メソッドです。Minecraftのコマンドを入力します。
minecraft()
メソッドこのメソッドは、新しいMinecraftのログを検出したときに実行されます。
引数log
は、新しく追加されたログ1行です。
引数time
、causedAt
、level
および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
}
})
引数channel
はdiscord.jsのTextChannel
オブジェクトです。
引数user
はdiscord.jsのUser
オブジェクトです。
引数sendToDiscord
はdiscord.jsのsend
メソッドです。
引数sendToMinecraft
はnode-modern-rconのsend
メソッドです。Minecraftのコマンドを入力します。
今は最低限のプラグインしか存在しないため、プラグインを開発した方は、Issuesの方にコードを共有していただけると助かります。
便利なものは、積極的にデフォルトプラグインとして取り込んでいきたいと思います。
ということで、Minecordの紹介でした。
バグや改善点等見つけた方は、IssuesかTwitterの方にお寄せいただけると助かります。プルリクもお待ちしております。
特に、README.md
の英語の添削をしてくださる方がいましたら、非常にありがたいです……。