Skip to content

Commit

Permalink
Allow more flexibility in discord webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsvanloef committed Mar 6, 2024
1 parent 133a713 commit d65134d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,26 @@ ENV HOME=/home/steam \
DISCORD_CONNECT_TIMEOUT=30 \
DISCORD_MAX_TIMEOUT=30 \
DISCORD_PRE_UPDATE_BOOT_MESSAGE="Server is updating..." \
DISCORD_PRE_UPDATE_BOOT_MESSAGE_URL= \
DISCORD_PRE_UPDATE_BOOT_MESSAGE_ENABLED=true \
DISCORD_POST_UPDATE_BOOT_MESSAGE="Server update complete!" \
DISCORD_POST_UPDATE_BOOT_MESSAGE_URL= \
DISCORD_POST_UPDATE_BOOT_ENABLED=true \
DISCORD_PRE_START_MESSAGE="Server has been started!" \
DISCORD_PRE_START_MESSAGE_URL= \
DISCORD_PRE_START_MESSAGE_ENABLED=true \
DISCORD_PRE_SHUTDOWN_MESSAGE="Server is shutting down..." \
DISCORD_PRE_SHUTDOWN_MESSAGE_URL= \
DISCORD_PRE_SHUTDOWN_MESSAGE_ENABLED=true \
DISCORD_POST_SHUTDOWN_MESSAGE="Server has been stopped!" \
DISCORD_POST_SHUTDOWN_MESSAGE_URL= \
DISCORD_POST_SHUTDOWN_MESSAGE_ENABLED=true \
DISCORD_PLAYER_JOIN_MESSAGE="\${player_name} has joined Palworld!" \
DISCORD_PLAYER_JOIN_MESSAGE_URL= \
DISCORD_PLAYER_JOIN_MESSAGE_ENABLED=true \
DISCORD_PLAYER_LEAVE_MESSAGE="\${player_name} has left Palworld." \
DISCORD_PLAYER_LEAVE_MESSAGE_URL= \
DISCORD_PLAYER_LEAVE_MESSAGE_ENABLED=true \
ENABLE_PLAYER_LOGGING=true \
PLAYER_LOGGING_POLL_PERIOD=5 \
ARM_COMPATIBILITY_MODE=false \
Expand Down
12 changes: 11 additions & 1 deletion scripts/discord.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ DISCORD_GREEN=52224
TITLE=$1
MESSAGE=$2
LEVEL=$3
ENABLED=$4
URL=$5

if [ -n "${DISCORD_CONNECT_TIMEOUT}" ] && [[ "${DISCORD_CONNECT_TIMEOUT}" =~ ^[0-9]+$ ]]; then
CONNECT_TIMEOUT=$DISCORD_CONNECT_TIMEOUT
Expand Down Expand Up @@ -59,4 +61,12 @@ fi

JSON=$(jo embeds[]="$(jo title="$TITLE" description="$MESSAGE" color=$COLOR)")
LogInfo "Sending Discord json: ${JSON}"
curl -sfSL --connect-timeout "$CONNECT_TIMEOUT" --max-time "$MAX_TIMEOUT" -H "Content-Type: application/json" -d "$JSON" "$DISCORD_WEBHOOK_URL"

if [ "$ENABLED" ]; then
if [ "$URL" == "" ]; then
DISCORD_URL="$DISCORD_WEBHOOK_URL"
else
DISCORD_URL="$URL"
fi
curl -sfSL --connect-timeout "$CONNECT_TIMEOUT" --max-time "$MAX_TIMEOUT" -H "Content-Type: application/json" -d "$JSON" "$DISCORD_URL"
fi
4 changes: 3 additions & 1 deletion scripts/helper_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ DiscordMessage() {
local title="$1"
local message="$2"
local level="$3"
local enabled="$4"
local webhook_url="$5"
if [ -z "$level" ]; then
level="info"
fi
if [ -n "${DISCORD_WEBHOOK_URL}" ]; then
/home/steam/server/discord.sh "$title" "$message" "$level" &
/home/steam/server/discord.sh "$title" "$message" "$level" "$enabled" "$webhook_url" &
fi
}

Expand Down
8 changes: 4 additions & 4 deletions scripts/helper_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,19 @@ InstallServer() {
fi

if [ -z "${TARGET_MANIFEST_ID}" ]; then
DiscordMessage "Install" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE}" "in-progress"
DiscordMessage "Install" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE}" "in-progress" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_URL}"
/home/steam/steamcmd/steamcmd.sh +@sSteamCmdForcePlatformType linux +@sSteamCmdForcePlatformBitness 64 +force_install_dir "/palworld" +login anonymous +app_update 2394010 validate +quit
DiscordMessage "Install" "${DISCORD_POST_UPDATE_BOOT_MESSAGE}" "success"
DiscordMessage "Install" "${DISCORD_POST_UPDATE_BOOT_MESSAGE}" "success" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_URL}"
return
fi

local targetManifest
targetManifest="${TARGET_MANIFEST_ID}"

LogWarn "Installing Target Version: $targetManifest"
DiscordMessage "Install" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE}" "in-progress"
DiscordMessage "Install" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE}" "in-progress" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_PRE_UPDATE_BOOT_MESSAGE_URL}"
/home/steam/steamcmd/steamcmd.sh +@sSteamCmdForcePlatformType linux +@sSteamCmdForcePlatformBitness 64 +force_install_dir "/palworld" +login anonymous +download_depot 2394010 2394012 "$targetManifest" +quit
cp -vr "/home/steam/steamcmd/linux32/steamapps/content/app_2394010/depot_2394012/." "/palworld/"
CreateACFFile "$targetManifest"
DiscordMessage "Install" "${DISCORD_POST_UPDATE_BOOT_MESSAGE}" "success"
DiscordMessage "Install" "${DISCORD_POST_UPDATE_BOOT_MESSAGE}" "success" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_ENABLED}" "${DISCORD_POST_UPDATE_BOOT_MESSAGE_URL}"
}
2 changes: 1 addition & 1 deletion scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mkdir -p /palworld/backups

# shellcheck disable=SC2317
term_handler() {
DiscordMessage "Shutdown" "${DISCORD_PRE_SHUTDOWN_MESSAGE}" "in-progress"
DiscordMessage "Shutdown" "${DISCORD_PRE_SHUTDOWN_MESSAGE}" "in-progress" "${DISCORD_PRE_SHUTDOWN_MESSAGE_ENABLED}" "${DISCORD_PRE_SHUTDOWN_MESSAGE_URL}"

if ! shutdown_server; then
# Does not save
Expand Down
4 changes: 2 additions & 2 deletions scripts/player_logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ while true; do
broadcast_command "${player_name} has left"

# Replace ${player_name} with actual player's name
DiscordMessage "Player Left" "${DISCORD_PLAYER_LEAVE_MESSAGE//\$\{player_name\}/${player_name}}" "failure"
DiscordMessage "Player Left" "${DISCORD_PLAYER_LEAVE_MESSAGE//\$\{player_name\}/${player_name}}" "failure" "${DISCORD_PLAYER_LEAVE_MESSAGE_ENABLED}" "${DISCORD_PLAYER_LEAVE_MESSAGE_URL}"
done

# Notify Discord and log all players who have joined
Expand All @@ -58,7 +58,7 @@ while true; do
broadcast_command "${player_name} has joined"

# Replace ${player_name} with actual player's name
DiscordMessage "Player Joined" "${DISCORD_PLAYER_JOIN_MESSAGE//\$\{player_name\}/${player_name}}" "success"
DiscordMessage "Player Joined" "${DISCORD_PLAYER_JOIN_MESSAGE//\$\{player_name\}/${player_name}}" "success" "${DISCORD_PLAYER_JOIN_MESSAGE_ENABLED}" "${DISCORD_PLAYER_JOIN_MESSAGE_URL}"
done

old_player_list=("${current_player_list[@]}")
Expand Down
4 changes: 2 additions & 2 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ if [ "${ENABLE_PLAYER_LOGGING,,}" = true ] && [[ "${PLAYER_LOGGING_POLL_PERIOD}"
fi

LogAction "Starting Server"
DiscordMessage "Start" "${DISCORD_PRE_START_MESSAGE}" "success"
DiscordMessage "Start" "${DISCORD_PRE_START_MESSAGE}" "success" "${DISCORD_PRE_START_MESSAGE_ENABLED}" "${DISCORD_PRE_START_MESSAGE_URL}"

echo "${STARTCOMMAND[*]}"
"${STARTCOMMAND[@]}"

DiscordMessage "Stop" "${DISCORD_POST_SHUTDOWN_MESSAGE}" "failure"
DiscordMessage "Stop" "${DISCORD_POST_SHUTDOWN_MESSAGE}" "failure" "${DISCORD_POST_SHUTDOWN_MESSAGE_ENABLED}" "${DISCORD_POST_SHUTDOWN_MESSAGE_URL}"
exit 0

0 comments on commit d65134d

Please sign in to comment.