Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EveyHedgehog/9d530254b9ae80561d33b5a14022911c to your computer and use it in GitHub Desktop.
Save EveyHedgehog/9d530254b9ae80561d33b5a14022911c to your computer and use it in GitHub Desktop.

First, download discord.sh from https://github.com/fieu/discord.sh and create .webhook file with the url of your Discord Webhook

#!/bin/bash

# How often the docker logs should be checked
interval=1m
# Check the Drawpile Server's Docker logs for how many times someone has joined within the given interval
joincount=$(docker logs dpserver-drawpile-srv-1 --since "${interval}" 2>&1 | grep Join -c)

# If one or more people have joined 
if (("$joincount" >= "1"))
then
	# Make a text file of the log of only just the Joins
	docker logs dpserver-drawpile-srv-1 --since "${interval}" 2>&1 | grep Join > joins.txt
	# Get rid of ": Joined session" in the logs, just because it's redudant with the title the Discord embed is getting
	sed -i "s/: Joined session//" joins.txt 
	
	#Looping through the joins within the log text file
	while IFS= read -r join
    do 
        # Get rid of the Info/Join since it's redudant with the embed title
	join="${join//"Info/Join 1;::ffff:"/}"  
	# Get the time from the current line
        time=$(echo $join | grep -o '[0-2][0-9]:[0-5][0-9]:[0-5][0-9][Z]') 
	# Get the date from the current line
	date=$(echo $join | cut -c1-11)
	# Combine the date and time together
	datetime="$date$time"
	# Discord embeds can't use HammerTime so I just convert the date and display it in 12 hr format with the time only
	discordtime="$(date -d $datetime +%r)"
	# Find the IP in the current line
	ip="$(echo $join | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b')"
	# Get rid of IP in the current line
	join="${join//"$ip"/}"

	# /opt/scripts... should be wherever you have your discord.sh script at
        /opt/scripts/discord.sh --title "Someone joined a session!" --description "${join#$datetime ;}" --color "0x1C92F0" --footer-text "${discordtime}" --author-url "YOURURLHERE" --author "SERVERNAMEHERE" --author-icon "YOURFAVICONHERE" 
    done < joins.txt

fi

Now that you have the code, you can set it up to automatically execute every minute (or however long you set the interval as in the code), I used crontab for this. If you'd like to use crontab, type crontab -e into your terminal and insert this at the end

* * * * * /opt/scripts/dplog.sh

The example code there will make it run every minute, so check out crontab.guru as a guide for how to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment