Skip to content

Commit

Permalink
Added language support for notifications, and also fixed the footer t…
Browse files Browse the repository at this point in the history
…hat wasn't working. Improved readme
  • Loading branch information
saadbruno committed May 30, 2021
1 parent c726ef0 commit 2c0bd26
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 59 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ RUN apk add --no-cache bash curl
WORKDIR /app

COPY minecraft-discord-webhook.sh .
COPY ./lang ./lang

ENV FOOTER_TEXT="Minecraft Server"

CMD ["bash", "-c", "./minecraft-discord-webhook.sh $WEBHOOK_URL ./latest.log $FOOTER_TEXT"]
CMD ["bash", "-c", "WEBHOOK_URL=$WEBHOOK_URL SERVERLOG=./latest.log LANGUAGE=$LANGUAGE FOOTER=$FOOTER ./minecraft-discord-webhook.sh $WEBHOOK_URL"]
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# minecraft-discord-webhook
A small, server agnostic, way to push your server updates to Minecraft

![image](https://user-images.githubusercontent.com/23201434/120118752-7e06c880-c16a-11eb-84fb-cce9fb123b38.png)

This script will push your easily push:
- Server joins and leaves
- Deaths
- Advancements, challenges and goals

to a Discord Webhook easily, with minimal configuration, and without needing server-side mods or plugins such as Spigot, Paper, etc (although it works with those servers as well!), meaning it also works with a vanilla server.

This script works by reading your server log file, parsing and formatting it using Discord rich embeds, and pushing it to the webhook endpoint.

## Usage

### With Docker:

There's an image avaible on [Docker Hub](https://hub.docker.com/r/saadbruno/minecraft-discord-webhook)!

#### Docker run:
`docker run --name minecraft-discord-webhook -v /path/to/server/logs/latest.log:/app/latest.log:ro --env WEBHOOK_URL=https://discord.com/api/webhooks/111222333/aaabbbccc --env FOOTER=Optional\ Footer\ Text --env LANGUAGE=en-US saadbruno/minecraft-discord-webhook:latest`
> Note: FOOTER and LANGUAGE are optional
#### Docker Compose:
```
version: '3.3'
services:
minecraft-discord-webhook:
container_name: minecraft-discord-webhook
volumes:
- '/path/to/server/logs/latest.log:/app/latest.log:ro'
environment:
- 'WEBHOOK_URL=https://discord.com/api/webhooks/111222333/aaabbbccc'
- 'FOOTER=Optional Footer Text'
- 'LANGUAGE=en-US'
image: 'saadbruno/minecraft-discord-webhook:latest'
restart: unless-stopped
```
> Note: FOOTER and LANGUAGE are optional
### Without Docker:
- Clone the repo
- run `WEBHOOK_URL=<discord webhook> SERVERLOG=</path/to/server/log/latest.log> FOOTER=<optional footer> LANGUAGE=<optional language> ./minecraft-discord.webook.sh`

## Variables:

- WEBHOOK_URL: it's the discord webhook you want the notifications posted to. Read more at [Discord Support](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks)
- LANGUAGE: The language of the notifications. This only supports joins and leaves. Advancements and death messages are posted "as is", meaning they'll be posted using the language of your server. Check the [lang directory](https://github.com/saadbruno/minecraft-discord-webhook/tree/main/lang) for currently supported languages. Contributions are welcome!
- FOOTER: An optional footer text that will be included with the notifications, you can put your server name, server address or anything else. You can also ommit this for a more compact notification.
![image](https://user-images.githubusercontent.com/23201434/120119109-44cf5800-c16c-11eb-9ce1-8927629c805f.png)

2 changes: 2 additions & 0 deletions lang/en-US.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JOIN="$PLAYER joined the game!"
LEAVE="$PLAYER left the game... :("
2 changes: 2 additions & 0 deletions lang/pt-BR.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JOIN="$PLAYER entrou no servidor!"
LEAVE="$PLAYER saiu do servidor... :("
122 changes: 66 additions & 56 deletions minecraft-discord-webhook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,103 @@
# Minecraft Discord Webhook. A simple, server agnostic, way to push your Minecraft server updates to discord. Works with any server and doesn't need mods and plugins.
# MIT License
# Documentation available at: https://github.com/saadbruno/appendhook
# Usage: ./minecraft-discord.webook.sh <discord webhook url> <path to server.log> <footer text>
# Usage:
# WEBHOOK_URL=<discord webhook> SERVERLOG=</path/to/server/log/latest.log> FOOTER=<optional footer> LANGUAGE=<optional language> ./minecraft-discord.webook.sh
# Also available via a Docker image, read the github repo for more information

url=$1
serverlog=$2
footer=$3
cache=$(date +'%Y%m%d')
#let's check for required variables
if [ -z "$WEBHOOK_URL" ]; then
echo ":: WARNING: Missing arguments. USAGE:"
echo " WEBHOOK_URL=<discord webhook> SERVERLOG=</path/to/server/log/latest.log> FOOTER=<optional footer> LANGUAGE=<optional language> ./minecraft-discord.webook.sh"
echo ":: If you're using Docker, make sure you've set the WEBHOOK_URL environment variable"
exit 1
fi

if [ -z "$url" ]; then
echo ":: WARNING: No webhook URL was set. Please set it using the WEBHOOK_URL environment variable"
if [ ! -f "$SERVERLOG" ]; then
echo ":: WARNING: Couldn't find server log. Make sure $SERVERLOG exists. USAGE:"
echo " WEBHOOK_URL=<discord webhook> SERVERLOG=</path/to/server/log/latest.log> FOOTER=<optional footer> LANGUAGE=<optional language> ./minecraft-discord.webook.sh"
echo ":: If you're using Docker, make sure you mounted your server log to /app/latest.log with '-v /path/to/server/logs/latest.log:/app/latest.log:ro'"
exit 1
fi

echo "Starting webhooks script with the following info:"
echo ":: URL: $url"
echo ":: Footer: $footer"
echo ":: Server log: $serverlog"
DIR=$(dirname $0)

# function to send webhooks to Discord
function webhook() {
curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "Minecraft",
"avatar_url" : "https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/android-icon-192x192.png",
"embeds": [{
"title": "'"$1"'",
"color": "'"$2"'",
"thumbnail": {
"url": "'"$3"'"
},
"footer": {
"text": "'"$footer"'"
}
}
]}' $url
}
# cache forces minotar to guve us a new avatar every day, in case players change their skins
CACHE=$(date +'%Y%m%d')

# Let's default our language to english
if [ -z "$LANGUAGE" ]; then
LANGUAGE="en-US"
fi

LANGFILE=$DIR/lang/$LANGUAGE.sh
echo "================================================="
echo "Starting webhooks script with the following info:"
echo ":: Language: $LANGUAGE"
echo ":: URL: $WEBHOOK_URL"
echo ":: Footer: $FOOTER"
echo ":: Server log: $SERVERLOG"
echo "================================================="

# compact version of the webhook
function webhook_compact() {
curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "Minecraft",
"avatar_url" : "https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/android-icon-192x192.png",
"embeds": [{
"color": "'"$2"'",
"author": {
"name": "'"$1"'",
"icon_url": "'"$3"'"
-d '{
"username": "Minecraft",
"avatar_url" : "https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/android-icon-192x192.png",
"embeds": [{
"color": "'"$2"'",
"author": {
"name": "'"$1"'",
"icon_url": "'"$3"'"
},
"footer": {
"text": "'"$FOOTER"'"
}
}
}
]}' $url
]}' $WEBHOOK_URL
}

# actual loop with parsing of the log
tail -n 0 -F $serverlog | while read line; do
tail -n 0 -F $SERVERLOG | while read LINE; do

case $line in
case $LINE in

# match for chat message. If it's chat, we catch it first so we don't trigger false positives later
*\<*\>*) echo "Chat message" ;;

# joins and parts
*joined\ the\ game)
player=$(echo "$line" | grep -o ": .*" | awk '{print $2}')
echo "$player joined. Sending webhook..."
webhook_compact "$player entrou no servidor!" 6473516 "https://minotar.net/helm/$player?v=$cache"
PLAYER=$(echo "$LINE" | grep -o ": .*" | awk '{print $2}')
source $LANGFILE
echo "$PLAYER joined. Sending webhook..."
webhook_compact "$JOIN" 6473516 "https://minotar.net/helm/$PLAYER?v=$CACHE"
;;

*left\ the\ game)
player=$(echo "$line" | grep -o ": .*" | awk '{print $2}')
echo "$player left. Sending webhook..."
webhook_compact "$player saiu do servidor... :(" 9737364 "https://minotar.net/helm/$player?v=$cache"
PLAYER=$(echo "$LINE" | grep -o ": .*" | awk '{print $2}')
source $LANGFILE
echo "$PLAYER left. Sending webhook..."
webhook_compact "$LEAVE" 9737364 "https://minotar.net/helm/$PLAYER?v=$CACHE"
;;

# death messages, based on https://minecraft.gamepedia.com/Death_messages
*was*by* | *was\ burnt* | *whilst\ trying\ to\ escape* | *whilst\ fighting* | *danger\ zone* | *bang* | *death | *lava* | *flames | *fell* | *fell\ while* | *drowned* | *suffocated* | *blew\ up | *kinetic\ energy | *hit\ the\ ground | *didn\'t\ want\ to\ live* | *withered\ away*)
player=$(echo "$line" | grep -o ": .*" | awk '{print $2}')
message=$(echo "$line" | grep -o ": .*" | cut -c 3-)
echo "$player died. Sending webhook..."
webhook_compact "$message" 10366780 "https://minotar.net/helm/$player?v=$cache"
*was*by* | *was\ burnt* | *whilst\ trying\ to\ escape* | *whilst\ fighting* | *danger\ zone* | *bang* | *death | *lava* | *flames | *fell* | *fell\ while* | *drowned* | *suffocated* | *blew\ up | *kinetic\ energy | *hit\ the\ ground | *didn\'t\ want\ to\ live* | *withered\ away*)
PLAYER=$(echo "$LINE" | grep -o ": .*" | awk '{print $2}')
MESSAGE=$(echo "$LINE" | grep -o ": .*" | cut -c 3-)
source $LANGFILE
echo "$PLAYER died. Sending webhook..."
webhook_compact "$MESSAGE" 10366780 "https://minotar.net/helm/$PLAYER?v=$CACHE"
;;

# advancements
*has\ made\ the\ advancement*|*completed\ the\ challenge*|*reached\ the\ goal*)
player=$(echo "$line" | grep -o ": .*" | awk '{print $2}')
message=$(echo "$line" | grep -o ": .*" | cut -c 3-)
echo "$player made an advancement! Sending webhook..."
webhook_compact "$message" 2842864 "https://minotar.net/helm/$player?v=$cache"
*has\ made\ the\ advancement* | *completed\ the\ challenge* | *reached\ the\ goal*)
PLAYER=$(echo "$LINE" | grep -o ": .*" | awk '{print $2}')
MESSAGE=$(echo "$LINE" | grep -o ": .*" | cut -c 3-)
source $LANGFILE
echo "$PLAYER made an advancement! Sending webhook..."
webhook_compact "$MESSAGE" 2842864 "https://minotar.net/helm/$PLAYER?v=$CACHE"
;;

esac
Expand Down

0 comments on commit 2c0bd26

Please sign in to comment.