我一直在尝试使用自己创建的Reddit机器人,该机器人对任何子Reddit的评论均以“ anywordcountbot用户名,以词对词”的格式进行响应。人说这个词。但是,它尚未回复我的评论或输出任何错误。怎么了?
import praw
from botsecrets import PUT_CLIENT_ID_HERE, PUT_CLIENT_SECRET_HERE, PUT_PASSWORD_HERE, PUT_USER_AGENT_HERE, PUT_USER_NAME_HERE
reddit = praw.Reddit(client_id= PUT_CLIENT_ID_HERE,
client_secret= PUT_CLIENT_SECRET_HERE,
password= PUT_PASSWORD_HERE,
user_agent= PUT_USER_AGENT_HERE,
username= PUT_USER_NAME_HERE)
# Checks each comment in the generated stream of new comments
for comment in reddit.subreddit('all').stream.comments(skip_existing = True):
try:
# Checks for bot-name, username, word-to-check
text = re.compile(r'anywordcountbot(\s)+(\w)+(\s)+(\w)+',re.I).search(comment.body).group()
except:
continue
wordcount = 0
# Divides comment into the bot-name, username, word-to-check
commentattributes = text.split()
# Checks each submission title and increases count for every instance
for submission in reddit.redditor(commentattributes[1]).submissions.new(limit=None):
if commentattributes[2] in submission.title:
for word in submission.title.split():
if commentattributes[2] in word:
wordcount += 1
# Checks each submission text to increase count
if commentattributes[2] in submission.selftext:
for word in submission.selftext.split():
if commentattributes[2] in word:
wordcount += 1
# Checks each comment and increases count for every instance
for usercomment in reddit.redditor(commentattributes[1]).comments.new(limit=None):
if commentattributes[2] in usercomment.body:
for word in usercomment.body.split():
if commentattributes[2] in word:
wordcount += 1
# If the wordcount is one, times will not be plural ↓
times = 'times'
if wordcount == 1:
times = times[:4]
comment.reply(f'u/{commentattributes[1].title()} has said {commentattributes[2]} {wordcount} {times}')
print('Posted')