Fortnite Discord Bot JSON

贝尼代码

我正在开发我的强力不和谐机器人,并且我想切换外观API。但是我想切换到AKA https://fortniteapi.io的API并没有让我在代码中加上json。请有人帮忙。

这是我的代码:

    async with aiohttp.ClientSession() as session:
        async with session.get(f"https://fortniteapi.io/items/list?name={item}", headers={'Authorization':'auth'}) as r:       
            try:
                json = await r.json()     
                for sub_dict in json['items']:
                    items = sub_dict
                    print(items)
                    embed=discord.Embed(title=f"{items['id']}", description=f"{items['description']}")
                    embed.add_field(name="Name", value=f"{items['name']}", inline=True)
                    embed.add_field(name="Type", value=f"{items['type']}", inline=True)
                    embed.add_field(name="Set", value=f"{items['set']}", inline=True)
                    embed.set_footer(text="Jonsey Bot | Created By ignBane#1828")
                    await ctx.send(embed=embed)
            except Exception as e:
                embed=discord.Embed(title="Oops, that wasn't supposed to happen.", color=0xff0000)
                embed.add_field(name="Error:", value=f"``\n{e}\n``", inline=True)
                embed.set_footer(text="Jonsey Bot | Created By ignBane#1828")
                await ctx.send(embed=embed)

这是我的网站的json。

{'result': True, 'lang': 'en', 'itemsCount': 1, 'items': [[{'id': 'CID_610_Athena_Commando_M_ShiitakeShaolin', 'type': 'outfit', 'name': 'Madcap', 'description': 'The master of mushrooms enters the fight.', 'rarity': 'epic', 'internalRarity': 'epic', 'price': 0, 'added': '2020-07-04', 'reactive': False, 'releaseDate': None, 'interest': 7.84, 'images': {'icon': 'https://media.fortniteapi.io/images/4a7f5d675a4949867abca11cbfe29206/transparent.png', 'full_size': 'https://media.fortniteapi.io/images/4a7f5d675a4949867abca11cbfe29206/featured.png', 'featured': None, 'background': 'https://media.fortniteapi.io/images/4a7f5d675a4949867abca11cbfe29206/background.png', 'full_background': 'https://media.fortniteapi.io/images/4a7f5d675a4949867abca11cbfe29206/background_full.en.png'}, 'video': None, 'gameplayTags': ['Cosmetics.UserFacingFlags.HasVariants', 'Cosmetics.Source.ItemShop', 'Cosmetics.Set.MushroomQueen', 'Cosmetics.Filter.Season.12'], 'set': 'Fungus King'}]]}

这是我得到的错误

list indices must be integers or slices, not str

它应该做的是发送所有信息,例如名称,类型和设置信息。但是由于错误处理,机器人将其发送为“列表索引必须是整数或切片,而不是str”。请有人帮忙。

刺眼的

您错误地将索引编入JSON。json['items']包含一个列表,一个列表,一个字典。

这两个列表中只有一个元素,因此我认为遍历它们不是正确的选择。而是执行以下操作:

json = await r.json()
items = json['items'][0][0]
embed=discord.Embed(title=f"{items['id']}", description=f"{items['description']}")
# And so on...

您收到的错误消息表明您像对待字典一样对待列表。鉴于从列表和字典访问元素都使用方括号,因此当您看到该错误消息时,您将需要查看代码并再次检查您使用的类型是否正确。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章