問題には複数の解決策があります。既に応答したスレッドを持つデータベースを維持するのが最もきれいです。この場合、情報はほとんどないので、スレッドIDをファイルに保存するだけで済みます。
しかし、このデータベース/ファイルが失われたり、ボットポストが手作業または他のシナリオ全体で削除されたりすると問題になります。だからおそらく最善のアプローチは動的に行うことです。後でボットを遅くすると、上記のメソッドを高速応答のためのルックアップとして追加することを検討するかもしれません。
私が今話しているのは、あなたがコメントをチェックするたびに、submission_id
を取得し、すべてのコメントをスキャンしてボット応答がまだ追加されていないことを確認します(またはceratianしきい値が渡されていません)。
def scan():
for c in reddit_client.get_comments('chosen_subreddit'):
if keyword not in c.body.lower(): #check main requirement
continue
if c.author == None: #comment deleted
continue
if c.author.name == bot_name: #don't bother with comments made by bot
continue
answer(c,c.link_id[3:])
def answer(comment, sub_id)
sub = reddit_client.get_submission(submission_id=sub_id)
sub.replace_more_comments(limit=None,threshold=0)
flat_comments = praw.helpers.flatten_tree(sub.comments)
if len([com for com in flat_comments if com.author != None and com.author.name.lower() == bot_name.lower()]) > 0:
return False
#here you can prepare response and do other stuff
#....
#....
comment.reply(prepared_response)
return True