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
No known key found for this signature in database
GPG Key ID: 2076B128385E8C55
2 changed files with
24 additions and
7 deletions
lgsm/modules/alert_discord.sh
lgsm/modules/alert_pushbullet.sh
@ -7,7 +7,7 @@
moduleselfname = " $( basename " $( readlink -f " ${ BASH_SOURCE [0] } " ) " ) "
moduleselfname = " $( basename " $( readlink -f " ${ BASH_SOURCE [0] } " ) " ) "
jsonshort info = $(
jsoninfo = $(
cat << EOF
cat << EOF
{
{
"username" : "LinuxGSM" ,
"username" : "LinuxGSM" ,
@ -63,7 +63,7 @@ jsonshortinfo=$(
EOF
EOF
)
)
jsonshort noinfo = $(
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 = " ${ jsonshort noinfo } "
json = " ${ jsonnoinfo } "
else
else
json = " ${ jsonshort info } "
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 } " )
@ -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" )