Добавление счетчика ответов к боту reddit - PullRequest
0 голосов
/ 29 июня 2018

У меня есть бот, который отвечает на комментарии / ответы, включающие определенные ключевые слова. Чтобы люди не могли спамить бот, я хочу добавить счетчик, чтобы, когда счетчик достиг определенного числа, например 5 раз для каждой цепочки комментариев, я хотел, чтобы бот перестал отвечать в этой ветке. Я хочу сделать это, добавив счетчик и увеличивая его на 1 каждый раз, когда бот отвечает на одну цепочку комментариев. Вот код:

import praw
import config
import os

Black_list = []
Counter = 0

def bot_giris():
    r = praw.Reddit(username = config.username,
                password = config.password,
                client_id = config.client_id,
                client_secret = config.client_secret,
                user_agent = "Reddit bot")


    return r

def bot_calis(r, comments_replied_to):
    subreddit = r.subreddit('gereksiz')
    for comment in subreddit.comments(limit=10):
        if 'Something' in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
            print ("String found: " + comment.id)
            print(20*"-")
            print ("Comment author: ", comment.author)
            print(20*"-")
            comment.reply("Something else")
            Counter += 1
            print ("Responded: " + comment.id)
            print(20*"-")

            comments_replied_to.append(comment.id)

            with open ("comments_replied_to.txt", "a") as f:
                f.write(comment.id + "\n")


    time.sleep(10)

def get_saved_comments():
    if not os.path.isfile("comments_replied_to.txt"):
        comments_replied_to = []
    else:
        with open("comments_replied_to.txt", "r") as f:
            comments_replied_to = f.read()
            comments_replied_to = list(comments_replied_to)
            comments_replied_to = list(filter(None, comments_replied_to))

    return comments_replied_to

r = bot_giris()
comments_replied_to = get_saved_comments()


while True:
    bot_calis(r, comments_replied_to)

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

Вы можете попробовать добавить время ожидания, например, игнорировать все ответы в течение 5 минут, а затем начать отвечать снова. Примерно так:

last = time.time()

def bot_calis(r, comments_replied_to):
  subreddit = r.subreddit('gereksiz')
  for comment in subreddit.comments(limit=10):
    if 'Something' in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
      ...
      if Counter >= 5:
        # 5 minute cool down
        # or, just return here
        if time.time() - last > 5 * 60 * 1000:
          Counter = 0
        else:
          return
      last = time.time()
      Counter += 1
      comment.reply("Something else")
      ...
0 голосов
/ 29 июня 2018

Похоже, вы забыли инициализировать переменную counter, написав counter = 0.

...