Search code examples
pythonpython-3.xoauthredditpraw

Writing Reddit Automatic Bot code, Not commenting all parts of a list


So im writing a bot for a subreddit I moderate in python. The Bot is able to execute, and is able to reply to the comment correctly, except that it only replies with the final part of the list .(the comments are for the other people who use the sub). The code is not finished, i have only done it for one region so far, but everything seems to work except that one part. Please could you advise me on what i should do.

Example:

comment : u/crazy_angel1: automaester raven north reply: u/AutoMaesterGOT: u/CptAwsome12345

Code:

import time
#importing time module
import praw
#importing the thing that allows you to run all the code, if you want to run this on your own computer, you will need to install it, look for a tutorial on how to install PRAW online
reddit = praw.Reddit(client_id='redacted',
                     username='AutoMaesterGOT',
                     client_secret='redacted',
                     password='redacted',
                     user_agent='It is a script that messages people every time a certain phrase is passed in my subreddit. Created by /u/crazy_angel1')
print("logging in...")
print(reddit.user.me())
#singing on to the bot with OAuth, again look up online if you want to use it

WordCalls=['AUTOMAESTER RAVEN NORTH', 'AUTOMAESTER RAVEN CROWNLANDS', 'AUTOMAESTER RAVEN CROWNLANDS','AUTOMAESTER RAVEN WESTERLANDS', 'AUTOMAESTER RAVEN DORNE','AUTOMAESTER RAVEN VALE','AUTOMAESTER REACH', 'AUTOMAESTER RAVEN IRON ISLANDS']
#the terms that will call the bot
CommentCache=[]
#storing comments already sorted through
NorthMembers=['u/crazy_angel1','u/StraightOuttaNYC','u/jgames2000','u/CptAwsome12345']

def BOTRUN(): #the bots main code for North
    subreddit = reddit.subreddit('StormOfSwordsRP')#connecting to the subreddit
    comments = subreddit.stream.comments()#sorting through comments
    for comment in comments:
        comment_body = comment.body#storing each individual comment
        comment_body = comment_body.upper()#making every comment uppercase
        isMatch = any(string in comment_body for string in WordCalls)#setting conditions for calling bot 
        if isMatch and comment.id not in CommentCache:#checking if anybody has called bot and it hasnt already been replied to yet
            print("match found, comment id" +comment.id)
            comment.reply(NorthMembers)#calling North members
            CommentCache.append(comment.id)#adding the comment id to the cache
            print("reply succesful")

while True: #infinite loop
    BOTRUN()# executing bot
    time.sleep(10)

Solution

  • So apparently reddit does not like lists so it wouldn’t work properly if you try to reply with a list normally. So instead of the line

    Comment.reply(NorthMembers)

    You will want to use the line:

    Comment.reply(‘ ‘.join (NorthMembers))

    Thanks to @xay for the help