【发布时间】:2020-06-30 12:00:32
【问题描述】:
此 reddit 机器人旨在在调用关键字 '!randomhelloworld' 时使用随机答案回复 subreddit 中的 cmets。它会回复,但总是显示相同的评论,除非我停止并重新运行该项目。如何调整代码以使其始终显示随机注释?
import praw
import random
random_answer = ['hello world 1', 'hello world 2', 'hello world 3']
QUESTIONS = ["!randomhelloworld"]
random_item = random.choice(random_answer)
def main():
reddit = praw.Reddit(
user_agent="johndoe",
client_id="johndoe",
client_secret="johndoe",
username="johndoe",
password="johndoe",
)
subreddit = reddit.subreddit("sandboxtest")
for comment in subreddit.stream.comments():
process_comment(comment)
def process_comment(comment):
for question_phrase in QUESTIONS:
if question_phrase in comment.body.lower():
comment.reply (random_item)
break
if __name__ == "__main__":
main()
【问题讨论】:
标签: python python-3.x random reddit praw