alert_discord.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # LinuxGSM alert_discord.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: faflfama, diamondburned
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Discord alert.
  7. local modulename="ALERT"
  8. local commandaction="Alert"
  9. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. if ! command -v jq > /dev/null; then
  11. fn_print_fail_nl "Sending Discord alert: jq is missing."
  12. fn_script_log_fatal "Sending Discord alert: jq is missing."
  13. fi
  14. escaped_servername=$(echo -n "${servername}" | jq -sRr "@json")
  15. escaped_alertbody=$(echo -n "${alertbody}" | jq -sRr "@json")
  16. json=$(cat <<EOF
  17. {
  18. "username":"LinuxGSM",
  19. "avatar_url":"https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.png",
  20. "file":"content",
  21. "embeds": [{
  22. "color": "2067276",
  23. "author": {"name": "${alertemoji} ${alertsubject}", "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.png"},
  24. "title": "",
  25. "description": ${escaped_alertbody},
  26. "url": "",
  27. "type": "content",
  28. "thumbnail": {},
  29. "footer": {"text": "Hostname: ${HOSTNAME} / More info: ${alerturl}", "icon_url": ""},
  30. "fields": [
  31. {
  32. "name": "Game",
  33. "value": "${gamename}",
  34. "inline": true
  35. },
  36. {
  37. "name": "Server IP",
  38. "value": "[${alertip}:${port}](https://www.gametracker.com/server_info/${alertip}:${port})",
  39. "inline": true
  40. },
  41. {
  42. "name": "Server Name",
  43. "value": ${escaped_servername},
  44. "inline": true
  45. }
  46. ]
  47. }]
  48. }
  49. EOF
  50. )
  51. fn_print_dots "Sending Discord alert"
  52. discordsend=$(curl -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "$json" | jq -c .)" "${discordwebhook}")
  53. if [ "${discordsend}" ]; then
  54. fn_print_fail_nl "Sending Discord alert: ${discordsend}"
  55. fn_script_log_fatal "Sending Discord alert: ${discordsend}"
  56. else
  57. fn_print_ok_nl "Sending Discord alert"
  58. fn_script_log_pass "Sending Discord alert"
  59. fi