如何使用 PRAW 使 reddit 机器人调用用户名

How to make a reddit bot invoke a username using PRAW

我一直在玩 PRAW 来构建 reddit 机器人。虽然构建一个自动响应触发关键字的通用消息的机器人很容易,但我想构建更具交互性的东西。

我正在尝试构建一个 reddit 机器人,它调用它正在回复的 redditor 的用户名。例如,redditor /u/ironman666 发帖 "good morning",我希望机器人自动回复 "good morning to you too! /u/ironman666"。我怎样才能使这项工作?谢谢!

示例代码:在哪里以及如何调用触发用户的名称?

import praw
import time
from praw.helpers import comment_stream

r = praw.Reddit("response agent")
r.login()


target_text = "Good Morning!"
response_text = "Good Morning to you too! #redditor name go here "



processed = []
while True:
    for c in comment_stream(r, 'all'):   
        if target_text == c.body.lower() and c.id not in processed: 
            print('Wiseau bot activated! :@')
            c.reply(response_text)
            processed.append(c.id)   #then push the response 
            time.sleep(2)

如果你 read the docs 你会看到评论有一个 author 属性(除非它被删除),所以你应该能够做到:

response_text = 'Good morning to you too, {}!'
...
c.reply(response_text.format(c.author))