'How can I prevent my reddit bot from commenting on a post it's already commented on?

I have a reddit bot that routinely browses the rising posts and comments on certain posts. What I'm noticing is that sometimes, my bot is commenting on the same post 3-4 times. What I have done to fix this is saving each post that I comment on, and then checking that the post is not in my saved list before commenting. That is not working - I've tried the same thing with a list, and that wasn't working either. Any ideas?

Here is my current code:

def search(post):
   saved = reddit.user.me().saved(limit=None)
   if post not in saved:
      ##do code here

def loopRisingPosts():
   for post in reddit.subreddit("subreddit").rising(limit=10):
       search(post)

while True:
   loopRisingPosts()
   sleep(500)


Any idea why this bot is commenting on the same post more than once? Often 2-3 times?



Solution 1:[1]

if you are going to save all of the posts that you have commented on then you can check whether you saved it or not by doing:

def search(post):
   if not post.saved: # each post tags if it was saved or not
      ##do code here

Otherwise if you aren't saving a post a different method would be to search the comments of the post for comments where your username is the author.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Ryan