Discord py cryptocurrency API命令不工作

2024-09-30 10:26:53 发布

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

我有一个命令,该命令应该获取给定加密货币的价格,但它不起作用。代码如下:

    @commands.command()
    async def crypto(self, ctx, cc = None):
        try:
            if cc is None:
                ccbed=discord.Embed(title='Command Usage:', description=f'/crypto [crypto currency]', colour=random.randint(0x000000, 0xffffff), timestamp=datetime.utcnow())
                await ctx.send(embed=ccbed)

            else:
                url = f"https://api.coingecko.com/api/v3/simple/price?ids={cc}&vs_currencies=usd%2Ceur%2Cgbp"
                stats = requests.get(url)
                json_stats = stats.json()
                usdprice = json_stats["usd"]
                europrice = json_stats["eur"]
                gbpprice = json_stats["gbp"]

                ccbed2 = discord.Embed(title=f"**Current price of {cc}**", description="This data might be inaccurate.", colour=random.randint(0x000000, 0xffffff), timestamp=datetime.utcnow())
                ccbed2.add_field(name="**USD:**", value=usdprice, inline=True)
                ccbed2.add_field(name="**EURO:**", value=europrice, inline=True)
                ccbed2.add_field(name="**GBP:**", value=gbpprice, inline=True)

                await ctx.send(embed=ccbed2)

        except:
            ccbed3 = discord.Embed(title="Invalid crypto currency or API error.", colour=random.randint(0x000000, 0xffffff), timestamp=datetime.utcnow())
            ccbed3.set_author(name="Error!")
            await ctx.send(embed=ccbed3)

当我运行该命令时,它会触发无效的加密货币或API错误


Tags: name命令jsontitlestatsrandomembedcrypto

热门问题