【发布时间】:2018-08-21 01:14:06
【问题描述】:
所以我为我在 python 中管理的 subreddit 编写了一个机器人。 Bot 能够执行,并且能够正确回复评论,除了它只回复列表的最后部分。(cmets 是给使用 sub 的其他人的)。代码还没有完成,到目前为止我只为一个区域完成了它,但除了那一部分之外,一切似乎都有效。请你告诉我我应该怎么做。
例子:
评论: u/crazy_angel1:automaester raven north 回复: u/AutoMaesterGOT:u/CptAwsome12345
代码:
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)
【问题讨论】:
-
很难理解你想要达到的目标。我假设您希望在评论中找到
WordCalls之一时回复评论的完整列表NorthMembers。我对 praw 没有任何经验,但我看到的一件事是您正试图用整个列表对象进行响应,考虑到 reddit 使用[]格式化链接,这可能会变得令人不安。你试过comment.reply(' '.join(NorthMembers))吗? -
是的,这就是我正在努力实现的目标,不,我没有尝试过,我要试一试,感谢您的回复!
-
嗯,当我这样做时,机器人似乎根本没有回复。还有其他想法吗?
-
欢迎我是个白痴......当我把它放到我的代码中时,我删除了comment.reply,我只是把它留在加入NorthMembers。这解决了它,非常感谢你的帮助@xay
-
很高兴它成功了,祝你好运进一步编码:)
标签: python python-3.x oauth reddit praw