Closed troubleshootme closed 1 year ago
ON_VALHEIM_LOG_FILTER_REGEXP_Day='{ read l; l=${l// (\d+) /}; msg="Starting Day $l"; curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" "$DISCORD_WEBHOOK"; }'
lets look at each step.
read l;
- this reads the whole line from the log
Time 341662.25998408, day:189 nextm:342270.000010729 skipspeed:50.6450022207573
l=${l// (\d+) /};
this takes that log line and does a string replace using bash parameter expansion (If there are two slashes separating parameter and pattern, all matches of pattern are replaced with string.) since we didnt find the literal string ' (\d+) ' nothing is replaced
Time 341662.25998408, day:189 nextm:342270.000010729 skipspeed:50.6450022207573
msg="Starting Day $l";
now we pass our variable I into the new string and get
Starting Day Time 341662.25998408, day:189 nextm:342270.000010729 skipspeed:50.6450022207573
`curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" $DISCORD_WEBHOOK"; Finally, we post that string to the discord hook as content, with the webhook posting as username 'Valheim'
So what I think you may be looking for is
ON_VALHEIM_LOG_FILTER_REGEXP_Day='{ read l; l=${l// (\d+) /}; msg="Starting Day $l"; curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" "$DISCORD_WEBHOOK"; }'
if you want to stick with bash parameter expansion, you'll want to use the cut functions
so first read the line
read l;
"Time 341662.25998408, day:189 nextm:342270.000010729 skipspeed:50.6450022207573"
then cut the first half off at the word day (including the colon)
l=${l#*day:};
"189 nextm:342270.000010729 skipspeed:50.6450022207573"
then cut the second half off at nextm
l=${l%%nextm*};
"189 "
remove extra spaces
l=${l//[[:space:]]/};
"189"
msg="Starting Day $l";
now we pass our variable I into the new string and get
"Starting Day 189"
`curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" $DISCORD_WEBHOOK"; Finally, we post that string to the discord hook as content, with the webhook posting as username 'Valheim'
so to put it all together,
ON_VALHEIM_LOG_FILTER_REGEXP_Day='{ read l; l=${l#*day:}; l=${l%%nextm*}; l=${l//[[:space:]]/}; msg="Starting Day $l"; curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" "$DISCORD_WEBHOOK"; }'
its by no means clean, but it gets you where you want, without using awk, send, cut or regex capture groups
@ncraig419 That's exactly what I was looking for, thanks so much for sharing your knowledge!
I have my server start, server shutdown, player joined, player death, and player disconnect hooks working but I am really struggling with the new day webhook. On new day, the server log shows :
Mar 13 14:04:45 supervisord: valheim-server 03/13/2023 14:04:45: Time 341662.25998408, day:189 nextm:342270.000010729 skipspeed:50.6450022207573
I have the following in my stack.env file and at the moment, the hook works but my regex capture is no good (I only know a little regex) and discord message is the whole line:
Starting Day 03/13/2023 00:20:17: Time 336308.279902585, day:186 nextm:336870.000010729 skipspeed:46.8100090119988
I am just trying to capture the day number (\d+) and print "Starting day $1"
The env lines:
VALHEIM_LOG_FILTER_REGEXP_Day=day:(\d+)
ON_VALHEIM_LOG_FILTER_REGEXP_Day='{ read l; l=${l// (\d+) /}; msg="Starting Day $l"; curl -sfSL -X POST -H "Content-Type: application/json" -d "{\"username\":\"Valheim\",\"content\":\"$msg\"}" "$DISCORD_WEBHOOK"; }'
If possible, can someone break down what each part does?