Factorio server にプレイヤーが入退出した時に Discord に Webhook で通知するスクリプト。
Factorio server の標準出力を監視して、Join と Leave に関するメッセージを regex で取得している。
使い方: 以下のスクリプトを factorio_watcher.sh
という名前で保存して ./path/to/factorio --start-server ... | DISCORD_WEBHOOK_URL=https:/... ./factorio_watcher.sh
#!/bin/bash
# Factorio Watcher Script
# Provide DISCORD_WEBHOOK_URL as environment variable
# Check envvar
if [ -z "$DISCORD_WEBHOOK_URL" ]; then
echo "DISCORD_WEBHOOK_URL is not set. Exiting."
exit 1
fi
while read -r line; do
echo "$line"
if [[ $line =~ \[JOIN\]\ (.*)\ joined\ the\ game$ ]]; then
player="${BASH_REMATCH[1]}"
message="${player} が Factorio サーバーに参加しました"
elif [[ $line =~ \[LEAVE\]\ (.*)\ left\ the\ game$ ]]; then
player="${BASH_REMATCH[1]}"
message="${player} が Factorio サーバーから退出しました"
else
continue
fi
# Send message to Discord
curl -X POST -H "Content-Type: application/json" -d "{\"username\": \"FactorioWatch\", \"content\": \"${message}\"}" "$DISCORD_WEBHOOK_URL"
done
Gist もあります: https://gist.github.com/tinaxd/e468145ccbf268b2ad1d5343e5684727
コメントを残す