1

I am attempting to have a bot reply to Reddit comments when they contain any one of a number of certain phrases, however it seemingly replies to any comment at all due to the large number of phrases. I am not sure if I should put the phrases in a string, a list, etc. I'm using Python 3.5, and the latest version of PRAW. It should be noted that I am very much a beginner in Python. Thanks. Here is the code:

if " stupid phrase" or " dumb phrase" or " lame phrase" or " bad phrase" or " terrible phrase" or " me another phrase" or " idiotic phrase" or " awful phrase" or " me a better phrase" or " phrase sucks" or " phrase is stupid" or " phrase is dumb" or " phrase is lame" or " phrase is bad" or " phrase is not funny" or " phrase is terrible" or " phrase is idiotic" or " phrase is awful" in comment.body:
                            print(str(AUTHOR) + str(comment.body))
                            comment.reply((random.choice(REPLIES0)) + (random.choice(REPLIES1)) + (random.choice(REPLIES2)) + (random.choice(REPLIES3)))
                            comments_replied_to.append(comment.id)
                            with open("comments_replied_to.txt", "w") as f:
                                for comment.id in comments_replied_to:
                                    f.write(comment.id + "\n")
else:
    comments_not_replied_to.append(comment.id)
    with open("comments_not_replied_to.txt", "w") as f:
        for comment.id in comments_not_replied_to:
            f.write(comment.id + "\n")

EDIT: My problem has been solved. I turned the phrases into a list, which I then changed into a set, and it's working fine using if any(word in comment.body.split() for word in WORDLISTSSET):. Thanks all.

  • Also see [Why does `a == b or c or d` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – PM 2Ring Aug 03 '17 at 03:08
  • well, it sounds like you are wanting a word list, so that is typically an array of strings, then you can check to see `if comment in wordlist` kind of statement, and then reply with whatever you need. – Fallenreaper Aug 03 '17 at 03:08
  • @Fallenreaper Thanks for the suggestion - i'm not too familiar with any of this, so i've run into a bit of an issue. I get "'in ' requires string as left operand, not list" when using "if WORDLIST in comment.body". Anything I can do about this? – Senpai Soren Aug 03 '17 at 03:24
  • to see if any element in a list is in a string, you can refer this https://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string#4843172 – Bijoy Aug 03 '17 at 03:55
  • @SenpaiSoren You cant have a list on the left side. You would narrow it down and say: [phrase for phrase in worldlist if phrase in comment.body] would return a list of phrases from your world which is in comment.body. – Fallenreaper Aug 03 '17 at 13:26
  • @Fallenreaper Thanks again. I'm not sure how to incorporate what you're suggesting. With WORDLISTS being a list, I've tried if `[str(WORDLISTS) for str(WORDLISTS) in comment.body if str(WORDLISTS) in comment.body]` but it just replies to everything. `if comment.body in WORDLISTS` works but only if the only thing in the comment's body is one of the phrases - can I have it be if any of the phrases in the wordlist is present in the comment body, then: ? – Senpai Soren Aug 03 '17 at 18:49
  • @Fallenreaper I actually got it using `if any(word in comment.body.split() for word in WORDLISTSSET):` and it's working perfectly (for now). Thanks for your help! (Also thanks @Bijoy and @PM 2Ring) – Senpai Soren Aug 03 '17 at 19:15

0 Answers0