创建一些特定于api响应中数字的值

时间:2018-10-24 06:01:33

标签: python json discord discord.py

因此,我需要设置与API响应内的数字相对应的多个值,以解决不一致的嵌入问题:

    {rows: 7, vbucks: "https://fortnite-public-files.theapinetwork.com/fortnite-vbucks-icon.png",…}
items: [{itemid: "d17ae35-1b64bca-f0cebef-e097b12", name: "Divine Dragon", cost: "???", item: {,…}},…]
0: {itemid: "d17ae35-1b64bca-f0cebef-e097b12", name: "Divine Dragon", cost: "???", item: {,…}}
1: {itemid: "e072f22-591edf6-1243f3d-1686821", name: "Guan Yu", cost: "???", item: {,…}}
2: {itemid: "51c5fcc-445f634-eacbd39-540174d", name: "Loyal Shield", cost: "???", item: {,…}}
3: {itemid: "a7c3fc6-75503a3-393e6cd-8ee4c91", name: "Headbanger", cost: "???",…}
4: {itemid: "88b0141-ba800b9-030f4d4-3efac45", name: "Guandao", cost: "???", item: {,…}}
5: {itemid: "8f7d376-2c49df9-ad16235-6538f87", name: "Onesie", cost: "???", item: {,…}}
6: {itemid: "3c83cd1-592e881-d91e78b-34442d8", name: "Bullseye", cost: "???", item: {,…}}
rows: 7

看看它怎么说rows: 7,那里有7件事?我需要为每个下拉列表数字设置2个值,之所以需要根据rows:数字来设置它是因为它们的数量经常变化,但是我不知道该如何做...是我的代码,其中包含两个值来表明我的想法:

    if unreleased in ('unreleased','upcoming'):
  upcoming = fortnite_api_upcoming(unreleased)

  if upcoming:
    upcomingname0 = upcoming[0]['name']
    upcomingtype0 = upcoming[0]['item']['type']
    upcomingname1 = upcoming[1]['name']
    upcomingtype1 = upcoming[1]['item']['type']

    embed = discord.Embed(title="Item API Search Result", color=0xc600bc)

    embed.set_footer(text="by BattleDash#3866", icon_url="https://pbs.twimg.com/profile_images/1038570723382415361/wVhgKMug_400x400.jpg")
    embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(upcomingname0, upcomingtype0), inline=False)
    embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(upcomingname1, upcomingtype1), inline=False)
    await client.send_message(message.channel, embed=embed)
  else:
    await client.send_message(message.channel, 'Failed to get API data for unreleased items, there might be none!')

感谢任何可以提供帮助的人!

1 个答案:

答案 0 :(得分:1)

items列表中似乎所有这些项目都可用。为什么不只是迭代遍历并使用其中的每个项目呢?

if upcoming:
    embed = discord.Embed(title="Item API Search Result", color=0xc600bc)
    embed.set_footer(text="by BattleDash#3866", icon_url="https://pbs.twimg.com/profile_images/1038570723382415361/wVhgKMug_400x400.jpg")

    for item in upcoming['items']:
        item_name = item['name']
        item_type = item['item']['type']
        embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(item_name, item_type), inline=False)

    await client.send_message(message.channel, embed=embed)
else:
    await client.send_message(message.channel, 'Failed to get API data for unreleased items, there might be none!')