我的 reddit 机器人一遍又一遍地回复相同的评论和自己
My reddit bot replies to same comment and itself over and over
所以,我是 python 的新手,制作了一个简单的 reddit 机器人来回复评论。今天早上它运行良好,但现在它一遍又一遍地回复相同的评论甚至自己。
我用我糟糕的谷歌搜索技能找不到解决这个问题的方法……所以,我来了。
代码是这样的
import praw
import time
import config
REPLY_MESSAGE = "Di Molto indeed"
def authenticate():
print("Authenticating...")
reddit = praw.Reddit(client_id = config.client_id,
client_secret = config.client_secret,
username = config.username,
password = config.password,
user_agent = 'FuriousVanezianLad by /u/FuriousVanezianLad')
print("Authenticated as {}".format(reddit.user.me()))
return reddit
def main():
reddit = authenticate()
while True:
run_bot(reddit)
def run_bot(reddit):
print("Obtaining 25 comments...")
for comment in reddit.subreddit('test').comments(limit=25):
if "Di Molto" in comment.body:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
print("Sleeping for 10 seconds...")
time.sleep(10)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Interrupted")
我从 Bboe 的更新中获取了这段代码 "How To Make A reddit Bot — Part One by Busterroni"
我不知道出了什么问题,但它会自言自语。
抱歉,我知道这是一个愚蠢的问题,之前可能已经解决了,但我找不到...
再次抱歉
在此先感谢您的帮助!
问题是你一遍又一遍地获取最新的 25 条评论,但没有新评论被发表(或者它们被发表的速度很慢),所以你最终处理了相同的评论反复
我建议改用流,这是 PRAW 的一项功能。流在发布时获取新项目(在本例中为评论)。这样,您就不会多次处理同一条评论。另外,请在回复之前检查您是否发表了特定评论。这是您的代码的修改版本,它使用流并检查您是否发表了评论:
def run_bot(reddit):
me = reddit.user.me()
try:
for comment in reddit.subreddit('test').stream.comments(skip_existing=True):
if "Di Molto" in comment.body and comment.author != me:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
except Exception as e:
print("Got exception: {}".format(e))
print("Sleeping for 10 seconds...")
time.sleep(10)
所以,我是 python 的新手,制作了一个简单的 reddit 机器人来回复评论。今天早上它运行良好,但现在它一遍又一遍地回复相同的评论甚至自己。 我用我糟糕的谷歌搜索技能找不到解决这个问题的方法……所以,我来了。 代码是这样的
import praw
import time
import config
REPLY_MESSAGE = "Di Molto indeed"
def authenticate():
print("Authenticating...")
reddit = praw.Reddit(client_id = config.client_id,
client_secret = config.client_secret,
username = config.username,
password = config.password,
user_agent = 'FuriousVanezianLad by /u/FuriousVanezianLad')
print("Authenticated as {}".format(reddit.user.me()))
return reddit
def main():
reddit = authenticate()
while True:
run_bot(reddit)
def run_bot(reddit):
print("Obtaining 25 comments...")
for comment in reddit.subreddit('test').comments(limit=25):
if "Di Molto" in comment.body:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
print("Sleeping for 10 seconds...")
time.sleep(10)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Interrupted")
我从 Bboe 的更新中获取了这段代码 "How To Make A reddit Bot — Part One by Busterroni" 我不知道出了什么问题,但它会自言自语。 抱歉,我知道这是一个愚蠢的问题,之前可能已经解决了,但我找不到...
再次抱歉 在此先感谢您的帮助!
问题是你一遍又一遍地获取最新的 25 条评论,但没有新评论被发表(或者它们被发表的速度很慢),所以你最终处理了相同的评论反复
我建议改用流,这是 PRAW 的一项功能。流在发布时获取新项目(在本例中为评论)。这样,您就不会多次处理同一条评论。另外,请在回复之前检查您是否发表了特定评论。这是您的代码的修改版本,它使用流并检查您是否发表了评论:
def run_bot(reddit):
me = reddit.user.me()
try:
for comment in reddit.subreddit('test').stream.comments(skip_existing=True):
if "Di Molto" in comment.body and comment.author != me:
print('String with "Di Molto" found in comment {}',format(comment.id))
comment.reply(REPLY_MESSAGE)
print("Replied to comment " + comment.id)
except Exception as e:
print("Got exception: {}".format(e))
print("Sleeping for 10 seconds...")
time.sleep(10)