Browse Source

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.
pull/4345/head
Daniel Gibbs 2 years ago
parent
commit
a39e0e0f66
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 10
      lgsm/modules/alert_discord.sh
  2. 21
      lgsm/modules/alert_pushbullet.sh

10
lgsm/modules/alert_discord.sh

@ -7,7 +7,7 @@
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
jsonshortinfo=$( jsoninfo=$(
cat << EOF cat << EOF
{ {
"username": "LinuxGSM", "username": "LinuxGSM",
@ -63,7 +63,7 @@ jsonshortinfo=$(
EOF EOF
) )
jsonshortnoinfo=$( jsonnoinfo=$(
cat << EOF cat << EOF
{ {
"username": "LinuxGSM", "username": "LinuxGSM",
@ -117,10 +117,10 @@ EOF
fn_print_dots "Sending Discord alert" fn_print_dots "Sending Discord alert"
if [ "${alerturl}" == "not enabled" ]; then if [ -z "${alerturl}" ]; then
json="${jsonshortnoinfo}" json="${jsonnoinfo}"
else else
json="${jsonshortinfo}" json="${jsoninfo}"
fi fi
discordsend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${discordwebhook}") discordsend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${discordwebhook}")

21
lgsm/modules/alert_pushbullet.sh

@ -7,17 +7,34 @@
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
json=$( jsoninfo=$(
cat << EOF cat << EOF
{ {
"channel_tag": "${channeltag}", "channel_tag": "${channeltag}",
"type": "note", "type": "note",
"title": "${alertemoji} ${alerttitle} ${alertemoji}", "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 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" 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") 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")

Loading…
Cancel
Save