I am just learning Python and decided to write a really simple Python bot to reply on Reddit.
On compiling I am getting the following error:
File "C:\Python35\Scripts\RedditBot\Reddit.py", line 28 except attributeerror: ^ SyntaxError: invalid syntax
I am unable to see what is causing this as the code looks correct to me.
import praw
USERAGENT = "BOT Name"
USERNAME = "Username"
PASSWORD = "Password"
SUBREDDIT = "Subreddit"
MAXPOSTS = 100
SETPHRASES = ["Phrase", "PhraseOne"]
SETRESPONSE = "This is the response."
print('Logging in to Reddit')
r = praw.Reddit(USERAGENT)
r.login (USERNAME, PASSWORD)
def replybot():
print('Fetching Subreddit ' + SUBREDDIT)
subreddit = r.get_subreddit(SUBREDDIT)
print('Fetching comments')
comments = subreddit.get_comments(limit=MAXPOSTS)
for comment in comments:
try:
cauthor = comment.author.name
cbody = comment.body.lower()
if any(key.lower() in cbody for key in SETPHRASES):
print("Replying to " + cauthor)
comment.reply(SETRESPONSE)
except attributeerror:
pass
replybot()
You are having two problems.
So the inside of your for loop should look as follows:
try:
cauthor = comment.author.name
cbody = comment.body.lower()
if any(key.lower() in cbody for key in SETPHRASES):
print("Replying to " + cauthor)
comment.reply(SETRESPONSE)
except AttributeError:
pass