request error handling, .alerts works in non-warframe channels

This commit is contained in:
iou1name 2018-07-05 18:34:42 -04:00
parent 4c5fc3d98b
commit 6696c15152

View File

@ -73,10 +73,12 @@ class WarBot(irc.IRCClient):
stop.wait(60) stop.wait(60)
def checkNewAlerts(self): def checkNewAlerts(self, channel=None):
""" """
Checks the world state for new alerts or invasions. Checks the world state for new alerts or invasions.
""" """
if not channel:
channel = self.channel
while not self.stillConnected(): # startup reasons while not self.stillConnected(): # startup reasons
time.sleep(1) time.sleep(1)
@ -84,7 +86,11 @@ class WarBot(irc.IRCClient):
res = requests.get(URI) res = requests.get(URI)
res.raise_for_status() res.raise_for_status()
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
return return self.msg(channel, f"Connection Error")
except requests.exceptions.HTTPError:
return self.msg(channel, f"HTTP Error: {res.status_code}")
except Exception as e:
return self.msg(channel, f"Error: {e}")
data = res.json() data = res.json()
alert_ids = [alert["_id"]["$oid"] for alert in data["Alerts"]] alert_ids = [alert["_id"]["$oid"] for alert in data["Alerts"]]
@ -93,14 +99,14 @@ class WarBot(irc.IRCClient):
self.alert_ids += new_alerts self.alert_ids += new_alerts
for alert in data["Alerts"]: for alert in data["Alerts"]:
if alert["_id"]["$oid"] in new_alerts: if alert["_id"]["$oid"] in new_alerts:
self.process_alert(alert) self.process_alert(alert, channel)
expired_alerts = [a for a in self.alert_ids if a not in alert_ids] expired_alerts = [a for a in self.alert_ids if a not in alert_ids]
for alert_id in expired_alerts: for alert_id in expired_alerts:
self.alert_ids.remove(alert_id) self.alert_ids.remove(alert_id)
def process_alert(self, alert): def process_alert(self, alert, channel):
""" """
Processes the provided alert and sends a message to channel. Processes the provided alert and sends a message to channel.
""" """
@ -143,7 +149,7 @@ class WarBot(irc.IRCClient):
if items: if items:
message += f" | \x0310Items: \x0312{', '.join(items)}\x03" message += f" | \x0310Items: \x0312{', '.join(items)}\x03"
self.msg(self.channel, message) self.msg(channel, message)
def privmsg(self, user, channel, message): def privmsg(self, user, channel, message):
@ -151,7 +157,10 @@ class WarBot(irc.IRCClient):
Called when the bot receives a PRIVMSG, which can come from channels Called when the bot receives a PRIVMSG, which can come from channels
or users alike. or users alike.
""" """
pass if message == ".alerts":
self.alert_ids = []
self.checkNewAlerts(channel)
return
def joined(self, channel): def joined(self, channel):