view.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import sys
  3. from pprint import pprint
  4. import json
  5. from dotenv import load_dotenv
  6. import roman
  7. import requests
  8. base_url = 'https://discordapp.com/api/webhooks/'
  9. def send_message(status):
  10. name = status['name']
  11. its_id = status['id']
  12. status_text = f"[{name} [{its_id}]](https://www.torn.com/profiles.php?XID={its_id}) → " + \
  13. "Loot level changed to "+roman.toRoman(status['integer'])
  14. status_text += "\n" + status['time_string']
  15. this_json = {'content': status_text}
  16. pprint(this_json)
  17. _send(this_json)
  18. def send_alert(this_alert):
  19. this_json = {'content': this_alert}
  20. _send(this_json)
  21. def _send(send_json):
  22. load_dotenv()
  23. webhooks_json = os.environ["DISCORD_TOKENS"]
  24. webhooks = json.loads(webhooks_json)
  25. for webhook_key in webhooks:
  26. webhook = webhooks[webhook_key]
  27. if 'Alert: 10 mins' in send_json['content'] and webhook_key == 'Unrelenting':
  28. send_json['content'] = '@here ' + send_json['content']
  29. url = base_url+webhook
  30. response = requests.post(url, json=send_json)
  31. if response.status_code in [200, 204]:
  32. print("Webhook executed")
  33. else:
  34. print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
  35. if response.status_code == int(429):
  36. sys.exit(1)