mirror of
https://gitlab.com/chicken-riders/RcGcDw.git
synced 2025-02-23 00:24:09 +00:00
Still not working Discord error handling
This commit is contained in:
parent
8bed09514b
commit
1aa2df4e2f
60
rcgcdw.py
60
rcgcdw.py
|
@ -41,10 +41,7 @@ else:
|
||||||
_ = lambda s: s
|
_ = lambda s: s
|
||||||
|
|
||||||
def send(message, name, avatar):
|
def send(message, name, avatar):
|
||||||
try:
|
recent_changes.discord_message_query.append({"content": message, "avatar_url": avatar, "username": name})
|
||||||
req = requests.post(settings["webhookURL"], data={"content": message, "avatar_url": avatar, "username": name}, timeout=10)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def safe_read(request, *keys):
|
def safe_read(request, *keys):
|
||||||
if request is None:
|
if request is None:
|
||||||
|
@ -318,28 +315,25 @@ def webhook_formatter(action, STATIC, **params):
|
||||||
data["embeds"].append(dict(embed))
|
data["embeds"].append(dict(embed))
|
||||||
data['avatar_url'] = settings["avatars"]["embed"]
|
data['avatar_url'] = settings["avatars"]["embed"]
|
||||||
formatted_embed = json.dumps(data, indent=4)
|
formatted_embed = json.dumps(data, indent=4)
|
||||||
headers = {'Content-Type': 'application/json'}
|
recent_changes.discord_message_query.append(formatted_embed)
|
||||||
#logging.debug(data)
|
|
||||||
result = requests.post(settings["webhookURL"], data=formatted_embed, headers=headers)
|
|
||||||
if result.status_code != requests.codes.ok:
|
|
||||||
handle_discord_http(result.status_code, formatted_embed, headers)
|
|
||||||
|
|
||||||
def handle_discord_http(code, formatted_embed, headers):
|
def handle_discord_http(code, formatted_embed):
|
||||||
if code == 204: #message went through
|
if 300 > code > 199: #message went through
|
||||||
return
|
return 0
|
||||||
elif code == 400: #HTTP BAD REQUEST
|
elif code == 400: #HTTP BAD REQUEST
|
||||||
logging.error("Following message has been rejected by Discord, please submit a bug on our bugtracker adding it:")
|
logging.error("Following message has been rejected by Discord, please submit a bug on our bugtracker adding it:")
|
||||||
logging.error(formatted_embed)
|
logging.error(formatted_embed)
|
||||||
|
return 0
|
||||||
elif code == 401: #HTTP UNAUTHORIZED
|
elif code == 401: #HTTP UNAUTHORIZED
|
||||||
logging.error("Webhook URL is invalid or no longer in use, please replace it with proper one.")
|
logging.critical("Webhook URL is invalid or no longer in use, please replace it with proper one. Use Ctrl-C to leave the script.")
|
||||||
|
sys.exit(1)
|
||||||
elif code == 429:
|
elif code == 429:
|
||||||
logging.error("We are sending too many requests to the Discord, slowing down...")
|
logging.error("We are sending too many requests to the Discord, slowing down...")
|
||||||
time.sleep(20.0)
|
time.sleep(15.0)
|
||||||
result = requests.post(settings["webhookURL"], data=formatted_embed, headers=headers) #TODO Replace this solution with less obscure one
|
return 1
|
||||||
elif code > 500 and code < 600:
|
elif 600 > code > 499:
|
||||||
logging.error("Discord have trouble processing the event, and because the HTTP code returned is 500> it means we blame them.")
|
logging.error("Discord have trouble processing the event, and because the HTTP code returned is 500> it means we blame them.")
|
||||||
time.sleep(20.0)
|
return 2
|
||||||
result = requests.post(settings["webhookURL"], data=formatted_embed, headers=headers)
|
|
||||||
|
|
||||||
def first_pass(change): #I've decided to split the embed formatter and change handler, maybe it's more messy this way, I don't know
|
def first_pass(change): #I've decided to split the embed formatter and change handler, maybe it's more messy this way, I don't know
|
||||||
parsedcomment = (BeautifulSoup(change["parsedcomment"], "lxml")).get_text()
|
parsedcomment = (BeautifulSoup(change["parsedcomment"], "lxml")).get_text()
|
||||||
|
@ -356,11 +350,10 @@ def first_pass(change): #I've decided to split the embed formatter and change ha
|
||||||
combination = "{logtype}/{logaction}".format(logtype=logtype, logaction=logaction)
|
combination = "{logtype}/{logaction}".format(logtype=logtype, logaction=logaction)
|
||||||
logging.debug("combination is {}".format(combination))
|
logging.debug("combination is {}".format(combination))
|
||||||
try:
|
try:
|
||||||
settings["appearance"][combination]
|
STATIC_VARS = {**STATIC_VARS ,**{"color": settings["appearance"][combination]["color"], "icon": settings["appearance"][combination]["icon"]}}
|
||||||
except KeyError:
|
except KeyError:
|
||||||
STATIC_VARS = {**STATIC_VARS ,**{"color": "", "icon": ""}}
|
STATIC_VARS = {**STATIC_VARS ,**{"color": "", "icon": ""}}
|
||||||
logging.error("No value in the settings has been given for {}".format(combination))
|
logging.error("No value in the settings has been given for {}".format(combination))
|
||||||
STATIC_VARS = {**STATIC_VARS ,**{"color": settings["appearance"][combination]["color"], "icon": settings["appearance"][combination]["icon"]}}
|
|
||||||
if logtype=="protect" and logaction=="protect":
|
if logtype=="protect" and logaction=="protect":
|
||||||
webhook_formatter(2, STATIC_VARS, user=change["user"], title=change["title"], desc=parsedcomment, settings=change["logparams"]["description"])
|
webhook_formatter(2, STATIC_VARS, user=change["user"], title=change["title"], desc=parsedcomment, settings=change["logparams"]["description"])
|
||||||
elif logtype=="protect" and logaction=="modify":
|
elif logtype=="protect" and logaction=="modify":
|
||||||
|
@ -550,9 +543,8 @@ def day_overview(): #time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.ti
|
||||||
data = {}
|
data = {}
|
||||||
data["embeds"] = [dict(embed)]
|
data["embeds"] = [dict(embed)]
|
||||||
formatted_embed = json.dumps(data, indent=4)
|
formatted_embed = json.dumps(data, indent=4)
|
||||||
headers = {'Content-Type': 'application/json'}
|
|
||||||
logging.debug(formatted_embed)
|
logging.debug(formatted_embed)
|
||||||
result = requests.post(settings["webhookURL"], data=formatted_embed, headers=headers)
|
recent_changes.discord_message_query.append(formatted_embed)
|
||||||
else:
|
else:
|
||||||
logging.debug("function requesting changes for day overview returned with error code")
|
logging.debug("function requesting changes for day overview returned with error code")
|
||||||
|
|
||||||
|
@ -565,6 +557,7 @@ class recent_changes_class(object):
|
||||||
last_downtime = 0
|
last_downtime = 0
|
||||||
clock = 0
|
clock = 0
|
||||||
tags = {}
|
tags = {}
|
||||||
|
discord_message_query = []
|
||||||
if settings["limitrefetch"] != -1:
|
if settings["limitrefetch"] != -1:
|
||||||
with open("lastchange.txt", "r") as record:
|
with open("lastchange.txt", "r") as record:
|
||||||
file_content = record.read().strip()
|
file_content = record.read().strip()
|
||||||
|
@ -576,6 +569,27 @@ class recent_changes_class(object):
|
||||||
file_id = 999999999
|
file_id = 999999999
|
||||||
else:
|
else:
|
||||||
file_id = 999999999 #such value won't cause trouble, and it will make sure no refetch happens
|
file_id = 999999999 #such value won't cause trouble, and it will make sure no refetch happens
|
||||||
|
def send_from_query(self):
|
||||||
|
for num, item in enumerate(self.discord_message_query):
|
||||||
|
try:
|
||||||
|
result = requests.post(settings["webhookURL"], data=item, headers={**{'Content-Type': 'application/json'}, **settings["header"]}, timeout=10)
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
logging.warning("Timeouted while sending data to the webhook.")
|
||||||
|
break
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
logging.warning("Connection error while sending the data to a webhook")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
code = handle_discord_http(result.status_code, item)
|
||||||
|
if code == 0:
|
||||||
|
pass
|
||||||
|
elif code == 1:
|
||||||
|
self.send_from_query()
|
||||||
|
break
|
||||||
|
elif code == 2:
|
||||||
|
break
|
||||||
|
time.sleep(3.0) #sadly, the time here needs to be quite high, otherwise we are being rate-limited by the Discord, especially during re-fetch
|
||||||
|
self.discord_message_query = self.discord_message_query[num:]
|
||||||
def add_cache(self, change):
|
def add_cache(self, change):
|
||||||
self.ids.append(change["rcid"])
|
self.ids.append(change["rcid"])
|
||||||
#self.recent_id = change["rcid"]
|
#self.recent_id = change["rcid"]
|
||||||
|
@ -619,7 +633,6 @@ class recent_changes_class(object):
|
||||||
logging.debug("Rejected {val}".format(val=change["rcid"]))
|
logging.debug("Rejected {val}".format(val=change["rcid"]))
|
||||||
continue
|
continue
|
||||||
first_pass(change)
|
first_pass(change)
|
||||||
time.sleep(3.0) #sadly, the time here needs to be quite high, otherwise we are being rate-limited by the Discord, especially during re-fetch
|
|
||||||
return change["rcid"]
|
return change["rcid"]
|
||||||
def safe_request(self, url):
|
def safe_request(self, url):
|
||||||
try:
|
try:
|
||||||
|
@ -689,3 +702,4 @@ schedule.every().day.at("00:00").do(recent_changes.clear_cache)
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
|
recent_changes.send_from_query()
|
||||||
|
|
Loading…
Reference in a new issue