创建特定于api响应中某个数字的多个值

2024-05-14 05:58:03 发布

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

因此,我需要为不一致嵌入设置一个与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!')

感谢任何能帮忙的人


Tags: namehttpsapimessagetype数字embeditem
1条回答
网友
1楼 · 发布于 2024-05-14 05:58:03

看起来所有这些项目都可以在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!')

相关问题 更多 >