Home > database >  discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'link�
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'link�

Time:12-13

I tried to make a discord.py bot command that sends random dog fact and image but i've got this error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'link'

Here's the code:

@bot.command()
async def dog(ctx):
    response = requests.get('https://some-random-api.ml/animal/dog')
    json_data = json.loads(response.text) 

    embed = discord.Embed(color = 0xff9900, title = 'random dog fact') 
    embed.set_image(url = json_data['link']) 
    await ctx.send(embed = embed)

CodePudding user response:

There is no key in the JSON response called "link". Use "fact" instead for the dog fact, and "image" for the random dog image.

Also, instead of this code block:

response = requests.get('https://some-random-api.ml/animal/dog')
json_data = json.loads(response.text)

you can use

json_data = requests.get('https://some-random-api.ml/animal/dog').json()

using Requests' built-in json() method.

  • Related