diff --git a/jarvis_tasks/tasks/reddit.py b/jarvis_tasks/tasks/reddit.py index 4ff5f0c..1e97e95 100644 --- a/jarvis_tasks/tasks/reddit.py +++ b/jarvis_tasks/tasks/reddit.py @@ -164,6 +164,25 @@ async def reddit(bot: Snake) -> None: logger.debug("Starting Task-reddit") red = Reddit(**config.reddit) + logger.debug("Validating follows") + async for sub in Subreddit.find(): + count = 0 + + async for follow in SubredditFollow.find(q(display_name=sub.display_name)): + count += 1 + + guild = await bot.fetch_guild(follow.guild) + channel = await bot.fetch_channel(follow.channel) + if not guild or not channel: + logger.debug(f"Follow {follow.id} no longer valid, deleting") + await follow.delete() + count -= 1 + continue + + if count == 0: + logger.debug(f"Subreddit {sub.display_name} has no followers, removing") + await sub.delete() + while True: subs = Subreddit.find(q(display_name__nin=running)) diff --git a/jarvis_tasks/tasks/twitter.py b/jarvis_tasks/tasks/twitter.py index ab54e2e..387d754 100644 --- a/jarvis_tasks/tasks/twitter.py +++ b/jarvis_tasks/tasks/twitter.py @@ -166,6 +166,25 @@ async def twitter(bot: Snake) -> None: auth = tweepy.AppAuthHandler(config.twitter["consumer_key"], config.twitter["consumer_secret"]) api = tweepy.API(auth) logger.debug("Starting Task-twitter") + + logger.debug("Validating follows") + async for account in TwitterAccount.find(): + count = 0 + + async for follow in TwitterFollow.find(q(twitter_id=account.twitter_id)): + count += 1 + + guild = await bot.fetch_guild(follow.guild) + channel = await bot.fetch_channel(follow.channel) + if not guild or not channel: + logger.debug(f"Follow {follow.id} invalid, deleting") + await follow.delete() + count -= 1 + continue + + if count == 0: + logger.debug(f"Account {account.handle} has no followers, removing") + await account.delete() while True: accounts = TwitterAccount.find()