所以我在python上写discord bot,我想做后台任务,检查subreddit是否有新的提交,如果有某种天赋则发布。但是,当我试图启动我的僵尸程序时,它之前完美无缺,它只是在等待某些东西并且无法启动。我该怎么办?
async def reddit_task():
await bot.wait_until_ready()
start_time = time.time()
reddit = praw.Reddit(different keys)
subreddit = reddit.subreddit('certain subreddit')
for submission in subreddit.stream.submissions():
if submission.created_utc > start_time:
if submission.link_flair_text == 'certain flair':
em = discord.Embed(title=submission.title+'\n'+submission.shortlink)
if len(submission.preview) > 1:
em.set_image(url=submission.preview['images'][0]['source']['url'])
await bot.send_message(discord.Object(id='my channel id'), embed=em)
else:
pass
if __name__ == "__main__":
for extension in startup_extensions:
try:
bot.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
bot.loop.create_task(reddit_task())
bot.run(config.bot_beta_token)
答案 0 :(得分:0)
我遇到了这个问题,因为subreddit.stream.submissions()
阻止了for循环。所以我通过使用无限循环和subreddit.stream.submissions(pause_after=0)
来解决它,所以如果没有新帖子则会返回None
,然后等待60秒再重新检查。
async def reddit_task():
await client.wait_until_ready()
start_time = time.time()
reddit = praw.Reddit("<client_stuff>")
subreddit = reddit.subreddit("<some_subreddit>")
submissions = subreddit.stream.submissions(pause_after=0)
while not client.is_closed:
submission = next(submissions)
if submission is None:
# Wait 60 seconds for a new submission
await asyncio.sleep(60)
elif submission.created_utc > start_time:
<do_stuff_with_submission>