Add Necesse/chat2discord.sh

This commit is contained in:
Clarth 2024-03-15 14:50:59 -04:00
parent d2892e2471
commit 1c03d459ac
1 changed files with 37 additions and 0 deletions

37
Necesse/chat2discord.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
# Set your Discord webhook URL
DISCORD_WEBHOOK_URL="XXXXXXXXXXXXX"
# Set the log file you want to monitor
LOG_FILE="/home/necesse/latest-server-log.txt"
# Set the string you want to monitor for
MONITOR_STRING="):"
MONITOR_STRING2="(Print)"
MONITOR_STRING3=" connected on slot "
# Function to send a message to Discord
send_to_discord() {
local message="$1"
from="Server Chat"
curl -H "Content-Type: application/json" -X POST -d '{"username": "'"$from"'", "content": "'"$message"'", "avatar_url": "https://necessewiki.com/images/7/79/Player_Nav_Icon.png"}' "$DISCORD_WEBHOOK_URL"
}
# Tail the log file and monitor for the specified string
tail -n 0 -F "$LOG_FILE" | while read line; do
if [[ $line == *"$MONITOR_STRING3"* ]]; then
# When the string is found, send the line to Discord
message=$(echo "$line" | sed -E 's/\[[^]]+\] |"//g')
send_to_discord "$message"
fi
if [[ $line == *"$MONITOR_STRING"* ]] && [[ $line != *"$MONITOR_STRING2"* ]]; then
# Extract the portion of the line after the first '(' character
message="${line#*\(}"
send_to_discord "($message" # Send the modified message to Discord
fi
done