forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcbe_log.sh
executable file
·68 lines (62 loc) · 2.11 KB
/
mcbe_log.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
syntax='Usage: mcbe_log.sh SERVICE'
send() {
if [ -f "$webhook_file" ]; then
# Escape \ while reading line from file
while read -r url; do
if echo "$url" | grep -Eq 'https://discord(app)?\.com'; then
curl -X POST -H 'Content-Type: application/json' -d "{\"content\":\"$*\"}" -sS "$url" &
# Rocket Chat can be hosted by any domain
elif echo "$url" | grep -q 'https://rocket\.'; then
curl -X POST -H 'Content-Type: application/json' -d "{\"text\":\"$*\"}" -sS "$url" &
fi
done < "$webhook_file"
fi
wait
}
case $1 in
--help|-h)
echo "$syntax"
echo 'Post Minecraft Bedrock Edition server logs running in service to webhooks (Discord and Rocket Chat).'
echo
echo Logs include server start/stop and player connect/disconnect/kicks.
exit
;;
esac
if [ "$#" -lt 1 ]; then
>&2 echo Not enough arguments
>&2 echo "$syntax"
exit 1
elif [ "$#" -gt 1 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
service=$1
if ! systemctl is-active --quiet "$service"; then
>&2 echo "Service $service not active"
exit 1
fi
# Trim off $service before last @
instance=${service##*@}
webhook_file=~mc/.mcbe_log/${instance}_webhook.txt
chmod 600 "$webhook_file"
send "Server $instance starting"
trap 'send "Server $instance stopping"; pkill -s $$' EXIT
# Follow log for unit $service 0 lines from bottom, no metadata
journalctl -fu "$service" -n 0 -o cat | while read -r line; do
if echo "$line" | grep -q 'Player connected'; then
# Gamertags can have spaces as long as they're not leading/trailing/contiguous
player=$(echo "$line" | sed 's/.*Player connected: \(.*\),.*/\1/')
send "$player connected to $instance"
elif echo "$line" | grep -q 'Player disconnected'; then
player=$(echo "$line" | sed 's/.*Player disconnected: \(.*\),.*/\1/')
send "$player disconnected from $instance"
elif echo "$line" | grep -q Kicked; then
player=$(echo "$line" | sed 's/.*Kicked \(.*\) from the game.*/\1/')
reason=$(echo "$line" | sed "s/.*from the game: '\(.*\)'.*/\1/")
# Trim off leading space from $reason
reason=${reason#' '}
send "$player was kicked from $instance because $reason"
fi
done