让reddit bot生成对提交的随机回复
Getting reddit bot to generate a random reply to submission
我正在开发一个使用 PRAW 从文本文件生成 link 的机器人。这是我的代码:
import praw
from random import choice
import re
reddit = praw.Reddit(user_agent='XXX',
client_id='XXX',
client_secret='XXX',
username='XXX',
password='XXX')
with open("links.txt") as f:
lines = [l.rstrip() for l in f]
subreddit = reddit.subreddit('SUBREDDIT')
for submission in subreddit.stream.submissions:
if re.search("!TRIGGER WORD", submission.title, re.IGNORECASE):
submission.reply(choice(lines))
在 运行 我的脚本之后,我收到此错误消息:
Traceback (most recent call last):
File "main.py", line 16, in <module>
for submission in subreddit.stream.submissions:
TypeError: 'method' object is not iterable
有什么想法吗?
编辑:我忘记了这一行后的括号:
for submission in subreddit.stream.submissions:
它应该是这样的:
for submission in subreddit.stream.submissions():
或者这个:
for submission in subreddit.stream.submissions(skip_existing=True):
后者只会在流开始后寻找新的提交。如果您想随机选择回复,但不使用文件,您可以这样做:
导入您的库:
import praw
import random
创建 reddit 实例:
reddit = praw.Reddit(client_id="my client id",
client_secret="my client secret",
user_agent="my user agent",
username="my username",
password="my password")
像这样创建一个列表:
choices = ['choice', 'choice2', 'choice3']
然后检查你的触发器是否在标题中并随机选择回复:
for submission in subreddit.stream.submissions():
if "trigger" in submission.title:
submission.reply(random.choice(choices))
您发布的代码与回溯中的代码不同。您发布的代码应该可以工作;而回溯中的代码表明您忘记调用 submissions
。那一行应该是
for submission in subreddit.stream.submissions():
或
for submission in subreddit.stream.submissions(skip_existing=True):
但不是
for submission in subreddit.stream.submissions:
我正在开发一个使用 PRAW 从文本文件生成 link 的机器人。这是我的代码:
import praw
from random import choice
import re
reddit = praw.Reddit(user_agent='XXX',
client_id='XXX',
client_secret='XXX',
username='XXX',
password='XXX')
with open("links.txt") as f:
lines = [l.rstrip() for l in f]
subreddit = reddit.subreddit('SUBREDDIT')
for submission in subreddit.stream.submissions:
if re.search("!TRIGGER WORD", submission.title, re.IGNORECASE):
submission.reply(choice(lines))
在 运行 我的脚本之后,我收到此错误消息:
Traceback (most recent call last):
File "main.py", line 16, in <module>
for submission in subreddit.stream.submissions:
TypeError: 'method' object is not iterable
有什么想法吗?
编辑:我忘记了这一行后的括号:
for submission in subreddit.stream.submissions:
它应该是这样的:
for submission in subreddit.stream.submissions():
或者这个:
for submission in subreddit.stream.submissions(skip_existing=True):
后者只会在流开始后寻找新的提交。如果您想随机选择回复,但不使用文件,您可以这样做:
导入您的库:
import praw import random
创建 reddit 实例:
reddit = praw.Reddit(client_id="my client id",
client_secret="my client secret",
user_agent="my user agent",
username="my username",
password="my password")
像这样创建一个列表:
choices = ['choice', 'choice2', 'choice3']
然后检查你的触发器是否在标题中并随机选择回复:
for submission in subreddit.stream.submissions(): if "trigger" in submission.title: submission.reply(random.choice(choices))
您发布的代码与回溯中的代码不同。您发布的代码应该可以工作;而回溯中的代码表明您忘记调用 submissions
。那一行应该是
for submission in subreddit.stream.submissions():
或
for submission in subreddit.stream.submissions(skip_existing=True):
但不是
for submission in subreddit.stream.submissions: