Fazy
Fazy

Reputation: 166

'CommentHelper' is not iterable

I'm trying to learn how to make a Reddit bot in Python but am getting the following error message...

C:\Users\Warbz\Desktop>python redditbot.py
Logging in...
Grabbing subreddit...
Grabbing comments...
Traceback (most recent call last):
  File "redditbot.py", line 30, in <module>
    run_bot()
  File "redditbot.py", line 19, in run_bot
    for comment in subredditComments:
TypeError: 'CommentHelper' object is not iterable

My code is this...

import praw

r = praw.Reddit(client_id="***",
            client_secret="***",
            username="***",
            password="***",
            user_agent = "/u/Fazy89")#Description of the script

print("Logging in...")

words_to_match = ['definately', 'defiantly', 'definantly', 'definatly', 'definitly']
cache = []#ID's of comments already replied to

def run_bot():
    print("Grabbing subreddit...")
    subreddit = r.subreddit("test")#Get SubReddit e.g /r/test
    print("Grabbing comments...")
    subredditComments = subreddit.comments
    for comment in subredditComments:
        comment_text = comment.body.lower()#Assign variable to lower case body of text 
        isMatch = any(string in comment_text for string in words_to_match)
        if comment.id not in cache and isMatch:#^If comment has not been added to cache and is a match
            print("Match found! Comment ID: " +comment.id)
            comment.reply('I think you meant to say "Definitely".')
            print("Reply successful!")
            cache.append(comment.id)#Add comment ID to cache
    print("Comments loop finished, going to sleep...")

while True:
    run_bot()
    time.sleep(10)

I've tried looking at the PRAW api but I'm not sure what is actually going wrong, any help please?

Upvotes: 1

Views: 438

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191748

comments provides an instance of CommentHelper, which needs to be instantiated using parenthesis to call it.

for comment in r.subreddit('test').comments(limit=25):
    print(comment.author)

https://praw.readthedocs.io/en/latest/code_overview/models/subreddit.html#praw.models.Subreddit.comments

Upvotes: 2

Related Questions