0

I cant figure out why I keep getting this error. I assume it has to do with something in python 3.x that doesnt allow appending of strings? A little help would be appreciated. Im basically using a bot. I want it to reply with a comment when a string is found in another comment. It then adds the comment id to a txt file so it doesnt reply multiple times to the same comment.

import praw
import config
import time
import os

def bot_login():
    print("Attempting to log in...")
    r = praw.Reddit(username = config.username,
            password = config.password,
            client_id = config.client_id,
            client_secret = config.client_secret,
            user_agent = "Rick roll comment responder v0.1")
    print("login was succesful!")
    return r

def run_bot(r, comments_replied_to):
    print("getting the latest 500 comments...")



for comment in r.subreddit('test').comments(limit = 500):
        if "https://www.youtube.com/watch?v=dQw4w9WgXcQ" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
            print ("Rick Roll Found!") and comment.id
            comment.reply("Dont be fooled this link is a rick roll! Bleep Bleep Bloop Bloop I am a bot!")
            print("replied to comment") and comment.id

            comments_replied_to.append(comment.id)

            with open ("comments_replied_to.txt", "a"):
                f.write(comment.id + "\n")


    print ("going to go sleep for 30 minutes")
    #sleep for 30 minutes
    time.sleep(1800)

def get_saved_comments():
    if not os.path.isfile("comments_replied_to.txt"):
        comments_replied_to = []
    else:
        with open("comments_replied_to.txt", "r") as f:
            comments_replied_to = f.read()
            comments_replied_to = comments_replied_to.split("\n")
            comments_replied_to = filter(None, comments_replied_to)

    return comments_replied_to

r = bot_login()
comments_replied_to = get_saved_comments()
print (comments_replied_to)
while True:
    run_bot(r, comments_replied_to)
bills
  • 59
  • 9
  • problem is in python 2, `filter` returned a `list` and your code relied on that. comments_replied_to = filter(None, comments_replied_to) if you just iterate on `comments_replied_to`, it's not an issue, but now in python 3 you have to force to `list` – Jean-François Fabre Feb 23 '17 at 09:52
  • ah ok I see now I added list before the filter and that worked thank you. Im also getting an error on the return statement on line 46. The error is improper syntax. What am I missing here it looks fine to me – bills Feb 23 '17 at 09:58

0 Answers0