alert_discord.sh 1.9 KB

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