LoginSignup
6
4

More than 5 years have passed since last update.

マインクラフトのログから特定の文字列が入っていたらディスコードに流すシェルスクリプト

Last updated at Posted at 2018-10-21

マイクラのログから死因とか実績をとった際にdiscordに流す用のシェルスクリプトを作ってみた。
discordからwebhookのurlを取ってきて、そこに最新のログから送りたい文字列が入っていたら流す感じにしてる。

コード

discordLog.sh
#!/bin/bash
tail -n0 -F ~/xxx/logs/latest.log |
grep --line-buffered -f ~/xxx/pattern.txt |
xargs -I @ curl -H "Content-Type: application/json" -X POST -d '{"username": "minecraftBot", "content":"@"}' url

tailでlatest.logの出力を書き出して、grepで条件指定して、curlで投げる。
ワンライナー?で出来るのでとってもお気軽:relaxed:

メモ

tail -n0 -F ~/xxx/logs/latest.log |

tail -Fで新しいlogを検出する、大文字のFオプションだとファイルが新規に生成されたりしてもいい感じに対応してくれる。
-n0のオプションで、最初のコマンド使用時に文末の文字を出力させないようにする。

grep --line-buffered -f ~/xxx/pattern.txt |

--line-bufferedで渡された文字列をバッファさせないようにして、
-fオプションでpattern.txtの内容から検索条件を指定する。
pattern.txtの中身はこんな感じ

blew
burned
didn't
died
discovered
drowned
experienced
fell
got
hit
removed
starved
suffocated
tried
walked
was
went
withered
advance
WARN

死因の1単語目と実績(advance)、サーバ負荷などの警告(WARN)の単語が入っていたときにパターンマッチするようにしている。ここはやりたことに合わせて変更する。

xargs -I @ curl -H "Content-Type: application/json" -X POST -d '{"username": "minecraftBot", "content":"@"}' url

xargsで文字列を渡されるごとにcurlでdiscordに投げる。
最後のurlの部分はwebhook用のurlを入れる。

死因の1単語目の一覧をスクレイピングするシェルスクリプト(おまけ)

https://minecraft.gamepedia.com/Health#Death_messages パターンマッチ用にここから条件に合うものをスクレイピングする。

curl https://minecraft.gamepedia.com/Health#Death_messages | grep '\[player\]' | awk '{print $2}' | sed 's/<\/li>//' | sort | uniq

冗長な気もするけど結果はokそうなので問題なし:sweat_smile:

参考

6
4
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
6
4