此代码:
@commands.command(name="currency", aliases=['ce'])
async def currencyexchange(self, ctx, currency1, currency2, amount:str):
url = "https://www.amdoren.com/api/currency.php?api_key=WTkbre7JxKME95tsu62R2bBng42rKE&from=" + currency1 + "&to=" + currency2 + "&amount=" + amount
async with ClientSession() as session:
async with session.get(url) as response:
r = await response.json(content_type=None)
embed = discord.Embed(colour=discord.Colour.purple(), timestamp = ctx.message.created_at, title=f"Currency Rates")
embed.set_footer(text=f"Requested by {ctx.author}")
embed.add_field(name=f"Exchanged", value = f"{r('amount')}", inline = False)
await ctx.send(embed=embed)
返回标题显示的错误。不知道我在哪里搞砸了,但是我需要一些帮助。
API返回以下内容:
{
"error" : 0,
"error_message" : "-",
"amount" : 6.31656
}
谢谢
答案 0 :(得分:0)
在这一行:
embed.add_field(name=f"Exchanged", value = f"{r('amount')}", inline = False)
在编写r('amount')
时,用括号将r
视为函数。为了从amount
获取r
字段的值,您需要使用方括号。看起来像这样:
embed.add_field(name=f"Exchanged", value = f"{r['amount']}", inline = False)