From a39e0e0f66b357af526f2a743f4b9d15affd7da2 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Tue, 17 Oct 2023 20:49:20 +0100 Subject: [PATCH] refactor: simplify Discord and Pushbullet alert message handling The code changes in this commit refactor the way Discord and Pushbullet alert messages are handled. The previous implementation used separate variables for short information and no information scenarios, but now it uses a single variable for both cases. Additionally, the code now checks if the "alerturl" is empty instead of comparing it to a specific value. These changes improve code readability and maintainability by reducing redundancy and simplifying conditional logic. --- lgsm/modules/alert_discord.sh | 10 +++++----- lgsm/modules/alert_pushbullet.sh | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lgsm/modules/alert_discord.sh b/lgsm/modules/alert_discord.sh index a16e10811..5eccc06c9 100644 --- a/lgsm/modules/alert_discord.sh +++ b/lgsm/modules/alert_discord.sh @@ -7,7 +7,7 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" -jsonshortinfo=$( +jsoninfo=$( cat << EOF { "username": "LinuxGSM", @@ -63,7 +63,7 @@ jsonshortinfo=$( EOF ) -jsonshortnoinfo=$( +jsonnoinfo=$( cat << EOF { "username": "LinuxGSM", @@ -117,10 +117,10 @@ EOF fn_print_dots "Sending Discord alert" -if [ "${alerturl}" == "not enabled" ]; then - json="${jsonshortnoinfo}" +if [ -z "${alerturl}" ]; then + json="${jsonnoinfo}" else - json="${jsonshortinfo}" + json="${jsoninfo}" fi discordsend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${discordwebhook}") diff --git a/lgsm/modules/alert_pushbullet.sh b/lgsm/modules/alert_pushbullet.sh index d1db7bfd1..0a0145386 100644 --- a/lgsm/modules/alert_pushbullet.sh +++ b/lgsm/modules/alert_pushbullet.sh @@ -7,17 +7,34 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" -json=$( +jsoninfo=$( cat << EOF { "channel_tag": "${channeltag}", "type": "note", "title": "${alertemoji} ${alerttitle} ${alertemoji}", - "body": "Server name\n${servername}\n\nMessage\n${alertmessage}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}" + "body": "Server name\n${servername}\n\nInformation\n${alertmessage}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}" } EOF ) +jsonnoinfo=$( + cat << EOF +{ + "channel_tag": "${channeltag}", + "type": "note", + "title": "${alertemoji} ${alerttitle} ${alertemoji}", + "body": "Server name\n${servername}\n\nInformation\n${alertmessage}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}" +} +EOF +) + +if [ -z "${alerturl}" ]; then + json="${jsonnoinfo}" +else + json="${jsoninfo}" +fi + fn_print_dots "Sending Pushbullet alert" pushbulletsend=$(curl --connect-timeout 10 -sSL -H "Access-Token: ${pushbullettoken}" -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "https://api.pushbullet.com/v2/pushes" | grep "error_code")