소스 검색

[Bugfix] Add sleep when Discord rate limit

Foppe Hemminga 5 년 전
부모
커밋
6e494ee841
1개의 변경된 파일18개의 추가작업 그리고 11개의 파일을 삭제
  1. 18 11
      cron.py

+ 18 - 11
cron.py

@@ -4,35 +4,41 @@ import sqlite3
 import sys
 import time
 import settings
-from pprint import pprint
+from pprint import pprint, pformat
 
 
 def send_notification(maintainer, player, property, old_value, new_value):
-    query = """SELECT discord FROM discord WHERE maintainer = ?;"""
-    conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
-    conn.row_factory = sqlite3.Row
-    cur = conn.cursor()
-    cur.execute(query, (maintainer,))
-    discord = cur.fetchone()['discord']
     query = """SELECT value FROM player
         WHERE maintainer = ?
         AND player = ?
         AND property = 'name';"""
+    conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
+    conn.row_factory = sqlite3.Row
+    cur = conn.cursor()
     cur.execute(query, (maintainer, player))
     name = '?'
     val = cur.fetchone()
     if val:
         name = val['value']
     message = f"{name} [{player}] property '{property}' changed from {old_value} to {new_value}"
+    _send(maintainer, message)
+    conn.close()
+
+def _send(maintainer, message):
+    query = """SELECT discord FROM discord WHERE maintainer = ?;"""
+    conn = sqlite3.connect(settings.db_path + '/slork.sqlite')
+    conn.row_factory = sqlite3.Row
+    cur = conn.cursor()
+    cur.execute(query, (maintainer,))
+    discord = cur.fetchone()['discord']
+    conn.close()
     message_json = {"content": message}
     response = requests.post(discord, json=message_json)
     if response.status_code in [200, 204]:
         print("Webhook executed")
     else:
         print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
-        if response.status_code == int(429):
-            sys.exit(1)
-    conn.close()
+        time.sleep(10)
 
 def update_value(maintainer, player, property, new_value):
     print(f"update_value({maintainer}, {player}, {property}, {new_value})")
@@ -78,7 +84,8 @@ def cron():
             # pprint(faction_json)
         if 'error' in player_json:
             print(f"{row['maintainer']}")
-            pprint(faction_json)
+            pprint(player_json)
+            _send(row['maintainer'], '```'+pformat(player_json)+'```')
             break
 
         # Check for changes