1

I am trying to write a reddit bot that does the following:

  1. visits a specified subreddit
  2. checks submission titles for a specified keyword
  3. posts a randomized comment to submissions with the keyword in the title
  4. skips over submissions in which it has already commented

Steps 1-3 work but I'm stuck on step 4.

My code:

#imports
import praw
import random

#setup reddit login
userAgent = my_agent
cID = my_cID
cSC= my_cSC
userN = my_userN
userP = my_userP

reddit = praw.Reddit (user_agent=userAgent, client_id=cID, client_secret=cSC, username=userN, password=userP)

#visits specified subreddit
subreddit = reddit.subreddit(TEST_SUBREDDIT)

#list of random comments
text_stubs = ['COMMENT 1','COMMENT 2','COMMENT 3']

#blank list to store submissions IDs where bot has left a comment
skip = [] 

#counter to track progress through submissions in the subreddit
numFound = 0 

for submission in subreddit.hot(limit=25): #look through submissons in the subreddit
    n_title = submission.title.lower() #regularize title case
    if submission.id not in skip: #check submission against the skip list
        if 'KEYWORD' in n_title: #check for keywword
            numFound = numFound + 1 #advance the counter
        rand_comment = random.randint(1, len(text_stubs) - 1) # pick a random comment
            bot_phrase = str(text_stubs[rand_comment]) #comment on the submission
            skip.append(submission.id) #add the submission ID to the skip list so bot won't comment on the same post twice

When I run the code in a test subreddit, it posts a comment correctly. When I rerun the code again, it will post again on a submission where it already left a comment. I don't want it to do that.

Any thoughts?

NWWPA
  • 69
  • 1
  • 7

1 Answers1

0

Haven't made any projects with python 3 in a long time, so I can't EXACTLY help but maybe look at these posts and see if you can really do much with them. If this answer doesn't help, leave a comment as I will try to test this myself and see what I can do...

Check if item is in an array / list

https://www.programiz.com/python-programming/methods/list/append

SirStopIt
  • 102
  • 2
  • 16
  • 1
    Thanks for the response. I have successfully used list/append in other programs, and the links you provided don't contain new any information. If you wouldn't mind testing out that would be really appreciated :-) – NWWPA May 07 '21 at 15:43
  • Ok, can you show me how to get like account tokens and all that please :D – SirStopIt May 07 '21 at 15:44
  • Also, I am having a dumb moment now but how do I get a user agent xD – SirStopIt May 07 '21 at 15:47
  • The tokens are unique to a reddit account. You'd have create an app to get them. It's not hard, but I wont share mine. I have used the tokens etc. in other PRAW apps without problem so I know that's not it. I suspect it's something to do with the structure of my for/if loops? – NWWPA May 07 '21 at 16:17
  • Yes, I was thinking about adding a for blah, in blah in skip if blah is equal to blah then blah, sorry for all the blah's but you get the idea but I realised quickly that, if there is nothing in the table, it won't loop since it's everything FOR/IN there which is nothing so it won't run unless I am having, once again, another dumb moment. – SirStopIt May 07 '21 at 18:32