使用Spoonacular API生成随机食品配方的Discord Bot

发布于 2025-01-23 19:02:41 字数 1490 浏览 0 评论 0原文

我正在尝试在NextCord Python中制作一个Discord Bot,该机器人将使用$ RandomRecipe命令生成随机配方。 我尝试制作它,以便它可以发送菜的标题(以及图像和成分),但我无法使其工作。

@bot.command(name='randomrecipe')
async def randomrecipe(ctx):
    r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=apikey')
    title_name = r.json() ["title"]
    await ctx.send(title_name)

这是该代码的错误

Ignoring exception in command randomrecipe:
Traceback (most recent call last):
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 168, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\iliaa\Downloads\json\app.py", line 22, in randomrecipe
    title_name = r.json() ["title"]
KeyError: 'title'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1048, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 933, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 177, in wrapped
    raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'title'

I am trying to make a discord bot in Nextcord Python that will generate a random recipe using a $randomrecipe command.
I tried making it so that it would send a title of a dish (and also the image and ingredients) but I cant get it to work.

@bot.command(name='randomrecipe')
async def randomrecipe(ctx):
    r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=apikey')
    title_name = r.json() ["title"]
    await ctx.send(title_name)

here's the error for that code

Ignoring exception in command randomrecipe:
Traceback (most recent call last):
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 168, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\iliaa\Downloads\json\app.py", line 22, in randomrecipe
    title_name = r.json() ["title"]
KeyError: 'title'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1048, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 933, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\iliaa\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 177, in wrapped
    raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'title'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-01-30 19:02:41

此错误意味着API未返回标题。
问题在于以下行:title_name = r.json()[“ title”]
尝试在没有[“ title”] print(r.json())的情况下打印put r.json()
您会注意到,该标题密钥不包括在响应中。
您正在寻找根部元素中的标题,但是在复制它之后,打印了整个响应,我注意到标题位于食谱中。有乘数标题键,因为您想要菜的标题(这是第一个),您需要按0索引(计算机开始在0),因此您应该尝试使用r.json()[)[ “食谱”] [0] [“标题”]
使用这种方式后,我得到了输出的输出:
整天简单的慢煮者从骨肋骨上掉下来

这里我使用的完整代码:

import requests
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=your_key_here')
print(r.json()["recipes"][0]["title"])

This error means, that the api not returned a title.
The issue lies in following line: title_name = r.json()["title"]
Try to print put r.json() without the ["title"] print(r.json())
You will notice, that title key is not included in the response.
You are looking for title in the root element but after reproducing it, printing out the whole response, i noticed that title is located inside recipes. There are multiplie title keys and because you want the title of the dish (which is the first one) you need to index it by 0 (computers start couting at 0) so you should try to use r.json()["recipes"][0]["title"]
After using it this way, i got follwoing output:
All Day Simple Slow-Cooker FALL OFF the BONE Ribs

Here the full code i've used:

import requests
r = requests.get('https://api.spoonacular.com/recipes/random?apiKey=your_key_here')
print(r.json()["recipes"][0]["title"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文