From 6696c15152f655f67c8a9c693b268ef073a38495 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 5 Jul 2018 18:34:42 -0400 Subject: [PATCH] request error handling, .alerts works in non-warframe channels --- warbot.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/warbot.py b/warbot.py index c44d21c..7aa3c2d 100755 --- a/warbot.py +++ b/warbot.py @@ -73,10 +73,12 @@ class WarBot(irc.IRCClient): stop.wait(60) - def checkNewAlerts(self): + def checkNewAlerts(self, channel=None): """ Checks the world state for new alerts or invasions. """ + if not channel: + channel = self.channel while not self.stillConnected(): # startup reasons time.sleep(1) @@ -84,7 +86,11 @@ class WarBot(irc.IRCClient): res = requests.get(URI) res.raise_for_status() 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() alert_ids = [alert["_id"]["$oid"] for alert in data["Alerts"]] @@ -93,14 +99,14 @@ class WarBot(irc.IRCClient): self.alert_ids += new_alerts for alert in data["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] for alert_id in expired_alerts: 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. """ @@ -143,7 +149,7 @@ class WarBot(irc.IRCClient): if items: message += f" | \x0310Items: \x0312{', '.join(items)}\x03" - self.msg(self.channel, message) + self.msg(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 or users alike. """ - pass + if message == ".alerts": + self.alert_ids = [] + self.checkNewAlerts(channel) + return def joined(self, channel):