Skip to content

Instantly share code, notes, and snippets.

@yuuahp
Created January 4, 2025 09:02
Show Gist options
  • Select an option

  • Save yuuahp/ca37b9bd839389074cf48f518d406595 to your computer and use it in GitHub Desktop.

Select an option

Save yuuahp/ca37b9bd839389074cf48f518d406595 to your computer and use it in GitHub Desktop.
Send Minecraft chat to Discord via webhook. For vanilla Minecraft server.
DISCORD_WEBHOOK_URL="WEBHOOK URL HERE"
# send a message to Discord via webhook
call_webhook() {
curl -X POST \
-H "Content-Type: application/json" \
-d "{\"content\": \"$1\"}" \
"$DISCORD_WEBHOOK_URL"
}
process_new_line() {
local line="$1"
local chat_regex="^\[[0-9]{2}:[0-9]{2}:[0-9]{2}\] \[Server thread/INFO\]: <([^>]+)> (.+)$"
local joined_regex="^\[[0-9]{2}:[0-9]{2}:[0-9]{2}\] \[Server thread/INFO\]: (.+) joined the game$"
local left_regex="^\[[0-9]{2}:[0-9]{2}:[0-9]{2}\] \[Server thread/INFO\]: (.+) left the game$"
if [[ "$line" =~ $chat_regex ]]; then
local username="${BASH_REMATCH[1]}"
local message="${BASH_REMATCH[2]}"
echo "Message from $username: $message"
call_webhook "**$username** : $message"
elif [[ "$line" =~ $joined_regex ]]; then
local username="${BASH_REMATCH[1]}"
echo "$usename joined the game"
call_webhook "$username が参加しました"
elif [[ "$line" =~ $left_regex ]]; then
local username="${BASH_REMATCH[1]}"
echo "$usename left the game"
call_webhook "$username が退出しました"
else
echo "Ignoring: $line"
fi
}
tail -f ./logs/latest.log | while read -r new_line; do
process_new_line "$new_line"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment