Change how/when invalid reddit follows are deleted

This commit is contained in:
Zeva Rose 2022-04-21 10:14:55 -06:00
parent da2445f04f
commit 3d091a7cb4

View file

@ -106,21 +106,23 @@ async def _stream(sub: Sub, bot: Snake) -> None:
continue continue
logger.debug(f"Got new post in {sub.display_name}") logger.debug(f"Got new post in {sub.display_name}")
follows = SubredditFollow.find(q(display_name=sub.display_name)) follows = SubredditFollow.find(q(display_name=sub.display_name))
follows_to_delete = []
num_follows = 0 num_follows = 0
async for follow in follows: async for follow in follows:
num_follows += 1 num_follows += 1
guild = await bot.fetch_guild(follow.guild) guild = await bot.fetch_guild(follow.guild)
if not guild: if not guild:
logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting") logger.warning(f"Follow {follow.id}'s guild no longer exists, deleting")
follows_to_delete.append(follow) await follow.delete()
num_follows -= 1
continue continue
channel = await bot.fetch_channel(follow.channel) channel = await bot.fetch_channel(follow.channel)
if not channel: if not channel:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting") logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
follows_to_delete.append(follow) await follow.delete()
num_follows -= 1
continue continue
embeds = await post_embeds(sub, post) embeds = await post_embeds(sub, post)
@ -133,17 +135,14 @@ async def _stream(sub: Sub, bot: Snake) -> None:
) )
except DNotFound: except DNotFound:
logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting") logger.warning(f"Follow {follow.id}'s channel no longer exists, deleting")
follows_to_delete.append(follow) await follow.delete()
num_follows -= 1
continue continue
except Exception: except Exception:
logger.error( logger.error(
f"Failed to send message to {channel.id} in {channel.guild.name}", exc_info=True f"Failed to send message to {channel.id} in {channel.guild.name}", exc_info=True
) )
# Delete invalid follows
for follow in follows_to_delete:
await follow.delete()
if num_follows == 0: if num_follows == 0:
s = await Subreddit.find_one(q(display_name=sub.display_name)) s = await Subreddit.find_one(q(display_name=sub.display_name))
if s: if s: