alert_discord.sh 1.8 KB

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