我正试图使用一些随机API来为我的不和谐机器人获取笑话。这是我的代码
import discord
import random
import aiohttp
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
@client.command()
async def joke(ctx):
async with aiohttp.ClientSession() as session:
# This time we'll get the joke request as well!
request = await session.get('https://some-random-api.ml/joke')
jokejson = await request.json()
embed = discord.Embed(title="I know its funny", color=discord.Color.purple())
embed.set_footer(text=jokejson['Joke'])
await ctx.send(embed=embed)
它运行,但当我使用。joke时,我得到错误discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'Joke'
,我希望有人能告诉我在哪里我错了。
我将embed.set_footer(text=jokejson['Joke'])
中的joke设置为embed.set_footer(text=jokejson['joke'])
,以便笑话与json中的笑话相同
@client.command()
async def joke(ctx):
async with aiohttp.ClientSession() as session:
# This time we'll get the joke request as well!
request = await session.get('https://some-random-api.ml/joke')
jokejson = await request.json()
embed = discord.Embed(title="I know its funny", color=discord.Color.purple())
embed.set_footer(text=jokejson['joke'])
await ctx.send(embed=embed)
这是