Add option to send notifications through Discord

This commit is contained in:
Ben Peddell 2018-06-16 23:40:26 +10:00
parent 6b6e4c0a38
commit 813ae334c9
3 changed files with 87 additions and 0 deletions

View File

@ -353,6 +353,10 @@ instances.
Sends the specified RCON command to the server and prints its
response
`notify "message"`::
Sends the specified message using the configured Discord
webhook
`status`::
Prints the status of the ARK server
@ -544,6 +548,38 @@ The following options can be overridden on a per-instance basis:
`{seconds}`;;
Valid in `msgTimeSeconds`, replaced at runtime with seconds remaining until shutdown
`discordWebhookURL`::
Discord Webhook URL - server status messages and update warning messages will be sent through
this if specified
`notifyTemplate`::
Template to use for sending messages through Discord webhook, with
the following replacement parameters:
`{instance}`;;
Instance name
`{server}`;;
Server hostname
`{msg}`;;
Message
`notifyMsgShuttingDown`::
Message to be sent when shutting down
`notifyMsgStarting`::
Message to be sent when starting
`notifyMsgServerUp`::
Message to be sent when server starts listening
`notifyMsgStoppedListening`::
Message to be sent when server has stopped listening for more than 1 minute
`notifyMsgServerTerminated`::
Message to be sent when server has crashed and is being restarted
`logdir`::
Specifies where to store log files

View File

@ -498,6 +498,39 @@ doBroadcast(){
doBroadcastWithEcho(){
echo "$1"
doBroadcast "$1"
notify "$1"
}
#
# Discord Webhook notifier
#
function notifyDiscord(){
if [ -n "$discordWebhookURL" ]; then
local msg="$(echo -n "${msg}" | tr '\n' '\001' | sed 's/\001/\\n/g;s/["\\]/\\\0/g')"
local json="{\"content\":\"${msg}\"}"
echo "Sending ${json} to Discord"
curl -H "Content-Type: application/json" -d "${json}" "$discordWebhookURL"
fi
}
#
# Overridable notifier
#
function notifyOverride(){
:
}
#
# Notification wrapper
#
function notify(){
local msg
msg="${notifyTemplate:-Message from instance \`{instance\}\` on server \`{server\}\`: {msg\}}"
msg="${msg//\{msg\}/${1}}"
msg="${msg//\{instance\}/${instance}}"
msg="${msg//\{server\}/${HOSTNAME}}"
notifyDiscord "$msg"
notifyOverride "$msg"
}
#
@ -1067,6 +1100,7 @@ doRun() {
# Shutdown the server when we are terminated
shutdown_server(){
restartserver=0
notify "${notifyMsgShuttingDown:-Shutting down}"
rm -f "$arkserverroot/$arkautorestartfile"
if [ "$serverpid" -ne 0 ]; then
kill -INT $serverpid >/dev/null 2>&1
@ -1079,6 +1113,7 @@ doRun() {
# Auto-restart loop
while [ $restartserver -ne 0 ]; do
echo -n "`timestamp`: Running"
notify "${notifyMsgStarting:-Starting}"
printf " %q" "$arkserverroot/$arkserverexec" "$arkserveropts" "${arkextraopts[@]}"
echo
# Put the server process into the background so we can monitor it
@ -1109,6 +1144,7 @@ doRun() {
if ! isTheServerUp; then
# Enable auto-restart if the server is up
echo "`timestamp`: server is up"
notify "${notifyMsgServerUp:-Server is up}"
touch "$arkserverroot/$arkautorestartfile"
restartserver=1
fi
@ -1119,6 +1155,7 @@ doRun() {
# Server has not been listening for 60 seconds, so restart it.
echo "`timestamp`: The server has stopped listening"
echo "`timestamp`: Restarting server"
notify "${notifyMsgStoppedListening:-Server has stopped listening - restarting}"
for (( i = 0; i < 5; i++ )); do
if ! kill -0 "$serverpid"; then
break
@ -1157,6 +1194,7 @@ doRun() {
fi
if [ "$restartserver" -ne 0 ]; then
notify "${notifyMsgServerTerminated:-Server exited - restarting}"
echo "`timestamp`: restarting server"
fi
done
@ -3220,6 +3258,7 @@ main(){
disablemod) nrarg=1; ;;
broadcast) nrarg=1; ;;
rconcmd) nrarg=1; ;;
notify) nrarg=1; ;;
useconfig) nrarg=1; ;;
install-cronjob) nrarg=1; ;;
remove-cronjob) nrarg=1; ;;
@ -3419,6 +3458,9 @@ main(){
broadcast)
doBroadcast "${args[@]}"
;;
notify)
notify "${args[@]}"
;;
saveworld)
doSaveWorld
;;

View File

@ -35,6 +35,15 @@ msgWarnShutdownMinutes="This ARK server will shutdown in %d minutes"
msgWarnShutdownSeconds="This ARK server will shutdown in %d seconds"
msgWarnCancelled="Restart cancelled by player request"
# Notifications
# discordWebhookURL="https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}"
# notifyMsgShuttingDown="Shutting down"
# notifyMsgStarting="Starting"
# notifyMsgServerUp="Server is up"
# notifyMsgStoppedListening="Server has stopped listening - restarting"
# notifyMsgServerTerminated="Server exited - restarting"
# notifyTemplate="Message from instance {instance} on server {server}: {msg}"
# Restart cancel chat command
#chatCommandRestartCancel="/cancelupdate"