我正在使用PID API搜索图像,然后我的discord.py机器人说出了它。但是,我不知道您必须调用什么函数才能获取图像,以便Discord机器人可以将其实际发送到嵌入程序中。 这是我当前的代码。
@bot.command(name='otter', help='Generates a random otter!')
async def otter(ctx):
r = requests.get('https://pixabay.com/api/?key=keyisherebutforprivacyreasonsiamremovingitlol&q=otter&image_type=photo')
r = r.json()
for item in r['hits']:
print(item['type'])
embed = discord.Embed(title='Otter test', color=discord.Color.from_rgb(226, 22, 31))
embed.set_image(url=item[random.choice(range(0, 5))]) # The problem is here, as I took the image bit away and the embed sent fine.
embed.set_footer(text='Powered by pixabay.')
await ctx.send(embed=embed)
差不多,它只是打印出来:一遍又一遍的照片,这很好,因为这意味着搜索有效。作为测试,我删除了set image部分,并且embed发送正常,所以我知道embed.set_image是一个问题。如果有人知道正确获取图像并发送图像的正确方法,我将不胜感激。谢谢!在控制台中也没有收到错误消息。
答案 0 :(得分:0)
确保从响应(1
8
30
20
AM
99
)中获取正确的内容。如果您遇到任何404错误,则可以检查网址是否包含图片扩展名(r
,.jpg
等)。
为便于将来参考,为了轻松显示json,您可以使用this网站。
与水獭交好运!