diff --git a/jarvis_tasks/tasks/twitter.py b/jarvis_tasks/tasks/twitter.py index 5f1f947..d7149a8 100644 --- a/jarvis_tasks/tasks/twitter.py +++ b/jarvis_tasks/tasks/twitter.py @@ -9,6 +9,7 @@ from jarvis_core.db.models import TwitterAccount, TwitterFollow from naff import Client from naff.client.errors import NotFound from naff.models.discord.embed import Embed +from tweepy import errors from tweepy.asynchronous import AsyncStream from tweepy.models import Status @@ -88,11 +89,21 @@ class JARVISTwitterStream(AsyncStream): async def on_keep_alive(self) -> None: """Override keep-alive to track new accounts.""" - await super().on_keep_alive() - ids = [x.twitter_id async for x in TwitterAccount.find()] - if ids != self.current_filter: - logger.debug("Follows have changed, disconnected") + try: + await super().on_keep_alive() + except errors.Unauthorized as e: + logger.error("Got 401, killing", stack_info=True) + logger.debug(f"{e.api_errors=}") + logger.debug(f"{e.api_messages=}") self.disconnect() + except Exception: + logger.error("Encountered error with stream in keepalive", stack_info=True) + self.disconnect() + else: + ids = [x.twitter_id async for x in TwitterAccount.find()] + if ids != self.current_filter: + logger.debug("Follows have changed, disconnected") + self.disconnect() async def on_status(self, status: Status) -> None: """