alert_discord.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # LinuxGSM alert_discord.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Discord alert.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. jsoninfo=$(
  9. cat << EOF
  10. {
  11. "username": "LinuxGSM",
  12. "avatar_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  13. "file": "content",
  14. "embeds": [
  15. {
  16. "author": {
  17. "name": "LinuxGSM Alert",
  18. "url": "",
  19. "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg"
  20. },
  21. "title": "${alerttitle}",
  22. "url": "",
  23. "description": "",
  24. "color": "${alertcolourdec}",
  25. "type": "content",
  26. "thumbnail": {
  27. "url": "${alerticon}"
  28. },
  29. "fields": [
  30. {
  31. "name": "Server Name",
  32. "value": "${servername}"
  33. },
  34. {
  35. "name": "Information",
  36. "value": "${alertmessage}"
  37. },
  38. {
  39. "name": "Game",
  40. "value": "${gamename}",
  41. "inline": true
  42. },
  43. {
  44. "name": "Server IP",
  45. "value": "\`${alertip}:${port}\`",
  46. "inline": true
  47. },
  48. {
  49. "name": "Hostname",
  50. "value": "${HOSTNAME}",
  51. "inline": true
  52. },
  53. {
  54. "name": "More info",
  55. "value": "${alerturl}",
  56. "inline": true
  57. },
  58. {
  59. "name": "Server Time",
  60. "value": "$(date)",
  61. "inline": true
  62. }
  63. ],
  64. "footer": {
  65. "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  66. "text": "Sent by LinuxGSM ${version}"
  67. }
  68. }
  69. ]
  70. }
  71. EOF
  72. )
  73. jsonnoinfo=$(
  74. cat << EOF
  75. {
  76. "username": "LinuxGSM",
  77. "avatar_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  78. "file": "content",
  79. "embeds": [
  80. {
  81. "author": {
  82. "name": "LinuxGSM Alert",
  83. "url": "",
  84. "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg"
  85. },
  86. "title": "${alerttitle}",
  87. "url": "",
  88. "description": "",
  89. "color": "${alertcolourdec}",
  90. "type": "content",
  91. "thumbnail": {
  92. "url": "${alerticon}"
  93. },
  94. "fields": [
  95. {
  96. "name": "Server Name",
  97. "value": "${servername}"
  98. },
  99. {
  100. "name": "Information",
  101. "value": "${alertmessage}"
  102. },
  103. {
  104. "name": "Game",
  105. "value": "${gamename}",
  106. "inline": true
  107. },
  108. {
  109. "name": "Server IP",
  110. "value": "\`${alertip}:${port}\`",
  111. "inline": true
  112. },
  113. {
  114. "name": "Hostname",
  115. "value": "${HOSTNAME}",
  116. "inline": true
  117. },
  118. {
  119. "name": "Server Time",
  120. "value": "$(date)",
  121. "inline": true
  122. }
  123. ],
  124. "footer": {
  125. "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  126. "text": "Sent by LinuxGSM ${version}"
  127. }
  128. }
  129. ]
  130. }
  131. EOF
  132. )
  133. fn_print_dots "Sending Discord alert"
  134. if [ -z "${alerturl}" ]; then
  135. json="${jsonnoinfo}"
  136. else
  137. json="${jsoninfo}"
  138. fi
  139. discordsend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${discordwebhook}")
  140. if [ -n "${discordsend}" ]; then
  141. fn_print_fail_nl "Sending Discord alert: ${discordsend}"
  142. fn_script_log_fail "Sending Discord alert: ${discordsend}"
  143. else
  144. fn_print_ok_nl "Sending Discord alert"
  145. fn_script_log_pass "Sending Discord alert"
  146. fi