Search code examples
pythonbotsredditattributeerrorpraw

Praw AttributeError: can't set attribute


I coded a reddit bot that will search through a subreddit's comments, and if a comment says feels it will reply to it. It then writes the comment id into a text file so that it never comments on the same comment twice.

Here is the code:

import praw
import time
import sys
import random

r = praw.Reddit('Posts Feels gif in response to someone saying feels'
                'by: Mjone77')
r.login('Feels_Bot', 'nottherealpassword')
file = open('Commentids.txt', 'r')
already_done = file.read()
file.close()
times = 0
feels = 0


while True:
   # try: 
        subreddit = r.get_subreddit('frozen')
        subreddit_comments = subreddit.get_comments()
        times+=1
        for comment in subreddit_comments:
            commentSays = comment.body
            commentSays = commentSays.lower()
            #print(commentSays)
            #print('\n')
            #if 'stop feels_bot' in commentSays:
                #sys.exit("Commanded to stop.")
            if comment.id not in already_done and 'feels' in commentSays:
                gif = random.randrange(0,3)
                if gif == 0:
                    comment.reply('[Relevant](http://i.imgur.com/pXBrf.gif)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                if gif == 1:
                    comment.reply('[Relevant](http://gfycat.com/BraveSerpentineAzurevase)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                if gif == 2:
                    comment.reply('[Relevant](http://www.gfycat.com/PlushMeanCowrie)\n\n___\n\n^I ^am ^a ^bot ^not ^a ^real ^redditor \n\n ^Please ^contact ^/u/Mjone77 ^with ^any ^problems')
                already_done = already_done+' '+comment.id
                file = open('Commentids.txt', 'w')
                file.write(already_done)
                file.close()
                #print('Commented~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
                time.sleep(5)
                feels+=1
        print('\n\n\n\n\n\n\n')        
        print('Feels:')
        print(feels)
        print('Times Ran:')
        print(times)
        time.sleep(60)
    #except AttriuteError:
        #pass

I ran it and left it running and came back to an HTTP Error 504: Gateway Time-out. Does anyone know how to keep that from happening or make it wait 60 seconds then try again?

Also, when I ran it again, without changing any code from the working program, it decided to give me this:

Traceback (most recent call last):
  File "C:\Users\Me\Desktop\My Programs\Feels Bot\Feels Bot\FeelsBot.py", line 20, in <module>
    for comment in subreddit_comments:
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 471, in get_content
    page_data = self.request_json(url, params=params)
  File "C:\Python34\lib\site-packages\praw\decorators.py", line 161, in wrapped
    return_value = function(reddit_session, *args, **kwargs)
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 516, in request_json
    data = json.loads(response, object_hook=hook)
  File "C:\Python34\lib\json\__init__.py", line 331, in loads
    return cls(**kw).decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "C:\Python34\lib\site-packages\praw\__init__.py", line 403, in _json_reddit_objecter
    return object_class.from_api_response(self, json_data['data'])
  File "C:\Python34\lib\site-packages\praw\objects.py", line 58, in from_api_response
    return cls(reddit_session, json_dict=json_dict)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 514, in __init__
    underscore_names=['replies'])
  File "C:\Python34\lib\site-packages\praw\objects.py", line 72, in __init__
    self.has_fetched = self._populate(json_dict, fetch)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 141, in _populate
    setattr(self, name, value)
  File "C:\Python34\lib\site-packages\praw\objects.py", line 102, in __setattr__
    object.__setattr__(self, name, value)
AttributeError: can't set attribute
sys:1: ResourceWarning: unclosed <socket object at 0x0342D930>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty

It no longer runs at all, anyone know how to fix this error?


Solution

  • I believe that this error is a result of this recent reddit update.

    From the above link:

    This change may also have some unexpected side-effects on third-party extensions/apps/etc. that display or otherwise use the specific up/down numbers. We've tried to take various precautions to make the transition smoother, but please let us know if you notice anything going horribly wrong due to it.

    I started experiencing it with my own bot today as well with no changes to the code.

    I posted an issue on the PRAW Github repo here.

    EDIT: The AttributeError has been fixed. Make sure you update PRAW to the newest version and it should work again.