0

I wanted to build a Reddit Bot, but it answers same comments everytime I run my program. I wanted program to store comment ID's to a json file but it doesn't.

# Reddit
CLIENT_ID = 'xxx'
CLIENT_SECRET = 'xxx'
USERNAME = 'xxx'
PASSWORD = 'xxx'
USER_AGENT = 'xxx'

# NASA
NASA_API = 'xxxxxxxxx'
NASA_HTTP = 'https://api.nasa.gov/planetary/apod'

# to access to the Bot account
reddit = praw.Reddit(client_id=CLIENT_ID,
                     client_secret=CLIENT_SECRET,
                     username=USERNAME,
                     password=PASSWORD,
                     user_agent=USER_AGENT)

# works in that subreddit
subreddit = reddit.subreddit('xxx')

# very last comment
last_comment_time = time.time()

answered_comments = {}

try:
    with open("comment_id.json", "r") as data_file:
        answered_comments = json.load(data_file)
except FileNotFoundError:
    with open("comment_id.json", "w") as data_file:
        json.dump(answered_comments, data_file, indent=4)
except json.decoder.JSONDecodeError:
    data = {}

while True:
    # get newest comments
    new_comments = subreddit.comments(params={"after": last_comment_time})

    for comment in new_comments:
        # update the newest comment
        if comment.created_utc > last_comment_time:
            last_comment_time = comment.created_utc

        # for answer /nasa 
        if '/nasa' in comment.body.lower():
            # if not already answered 
            if comment.id not in answered_comments:
                # parameters that necessary for APO request
                parameters = {
                    "api_key": NASA_API,
                    "hd": True
                }

                # getting data from API
                response = requests.get(NASA_HTTP, params=parameters)
                response.raise_for_status()
                data = response.json()

                # creating png url
                image_url = data["hdurl"]

                # answer the comment
                comment.reply('NASA Image of the Day: ' + image_url)

                # to add answered comment to dictionary
                answered_comments[comment.id] = {
                    "ID": comment.id,
                }

                # open JSON in append mode
                with open("comment_id.json", "a") as data_file:
                    if os.stat("comment_id.json").st_size == 0:
                        data = {}
                    else:
                        json.dump(answered_comments, data_file, indent=4)

        # for answer /indir 
        if '/indir' in comment.body.lower():
            post = comment.parent()
            if comment.id not in answered_comments:
                if post.media:
                    video_url = post.media['reddit_video']['fallback_url']
                    comment.reply('Video URL: ' + video_url)
                    # to add answered comment to dictionary
                    answered_comments[comment.id] = {
                        "ID": comment.id,
                    }
                else:
                    comment.reply('Bu indirilebilecek bir video değil.')
                    # to add answered comment to dictionary
                    answered_comments[comment.id] = {
                        "ID": comment.id,
                    }
                # open JSON in append mode
                with open("comment_id.json", "a") as data_file:
                    if os.stat("comment_id.json").st_size == 0:
                        data = {}
                    else:
                        json.dump(answered_comments, data_file, indent=4)


    # update every 10 minutes
    time.sleep(10 * 60)

While its working,it doesn't answer same comment everytime,but when I re-run,it starts answer.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Have you tried [setting breakpoints in the code](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) and checking how ids are stored? – OneCricketeer Apr 15 '23 at 23:53
  • try putting the while loop into the with open statement. – cyberghost Apr 16 '23 at 00:42

0 Answers0