Discord.py error Discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:“dict”对象不可调用

2024-05-17 03:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码:

    @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
}

谢谢


Tags: nameapiurlasyncresponsesessionaswith
1条回答
网友
1楼 · 发布于 2024-05-17 03:19:18

在这一行:

embed.add_field(name=f"Exchanged", value = f"{r('amount')}", inline = False)

当你写r('amount')时,用括号把r当作一个函数。为了从r中获取amount字段的值,需要使用方括号。它看起来是这样的:

embed.add_field(name=f"Exchanged", value = f"{r['amount']}", inline = False)

相关问题 更多 >