我想让我的机器人从 subreddit (r/memes) 发送一个随机图像。这是我所拥有的:
@client.event
async def on_message(message):
if message.content.startswith('.meme'):
memes_submissions = reddit.subreddit('memes').hot()
post_to_pick = random.randint(1, 50)
for i in range(0, post_to_pick):
submission = next(x for x in memes_submissions if not x.stickied)
await bot.say(submission.url)
它似乎不起作用,有什么提示吗?
答案 0 :(得分:1)
bot.say
在 discord.py 1.0 中被移除并替换为 .send
。
所以行 await bot.say(submission.url)
应该替换为
await message.channel.send(submission.url)
https://discordpy.readthedocs.io/en/latest/migrating.html?highlight=say#removed-helpers
在您的 for 循环中,您每次都重新创建非粘贴帖子列表,然后调用 next 但因为您重新创建,它将始终返回重新创建列表的第一个元素。您应该将 x for x in memes_submissions if not x.stickied
移出循环到一个变量中。然后您可以使用 next
但更简单的方法是将该变量作为一个带有随机数的列表进行索引。