Search code examples
pythonbotsreddit

How do I add more subreddits/keywords to this reddit bot code?


I found this reddit bot code on GitHub, it literally is just a comment bot.

https://github.com/yashar1/reddit-comment-bot

I know little about Python, so i tried to doctor the code to have multiple subreddits and keywords, adding these:

keywords = ["hello", "okay"]
subreddits = ["test", "bottest"]

    for comment in r.subreddit(any(subreddits)).comments(limit=100):
        if any(keywords) in comment.body and comment.id not in comments_replied_to 
and comment.author != r.user.me():

This code doesn't seem to work, mostly due to my limited knowledge of Python. I want the bot to scan multiple subreddits and search for multiple keywords.

Thanks.


Solution

  • You can include multiple subreddits like so.

    subreddits_list = "test+bottest"
    subreddits = r.subreddit(subreddits_list)
    

    To check for multiple keywords, you can use.

    keywords = ["hello", "okay"]
    for comment in subreddits.comments(limit=100):
        for keyword in keywords:
            if keyword in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():