Roblox API Discord 嵌入 If 语句问题

问题描述 投票:0回答:1
main_group_id = 14683506  
army_group_id = 15263473
enemy_group_id = 6521883

# Get main_group_rank (THIS WORKS!)

for group_data in user_groups_data.get("data", []):
    if group_data.get("group", {}).get("id") == main_group_id:
        rank_id = group_data.get("role", {}).get("rank")

# Fetch group roles to find the rank name
        group_api_url = f"https://groups.roblox.com/v1/groups/{main_group_id}/roles"
        group_response = requests.get(group_api_url)
        if group_response.status_code == 200:
            group_info = group_response.json()
            main_group_rank = None

            for role_data in group_info.get("roles", []):
                if role_data.get("rank") == rank_id:
                    main_group_rank = role_data.get("name")
                    break

            if main_group_rank is not None:
                print(f"User Rank: {main_group_rank}")
            else:
                print("Rank name not found.")
    else:
        main_group_rank = None

army_group_rank = None   # WORKS ALSO
for group_data in user_groups_data.get("data", []):
    if group_data.get("group", {}).get("id") == army_group_id:
        rank_id = group_data.get("role", {}).get("rank")
        print("Army in user")
        # Fetch group roles to find the rank name
        group_api_url = f"https://groups.roblox.com/v1/groups/{army_group_id}/roles"
        group_response = requests.get(group_api_url)
        if group_response.status_code == 200:
            group_info = group_response.json()
            print("Army 200")
            for role_data in group_info.get("roles", []):
                if role_data.get("rank") == rank_id:
                    army_group_rank = role_data.get("name")
                    print("Army Yes")
                    break

            if army_group_rank is not None:
                print(f"Army Rank: {army_group_rank}")
            else:
                print("Rank name not found.")
    else:
        army_group_rank = None

if main_group_rank:
    print("Adding Main Rank field")
    embed.add_field(name='Main Rank', value=main_group_rank, inline=False) # THIS WORKS!!!!
else:
    print("Main Rank is None")

if army_group_rank:
    print("Adding Army Rank field")
    embed.add_field(name='Army Rank', value=army_group_rank, inline=False) # THIS DOES NOT WORK BECAUSE [1]
else:
    print("Army Rank is None")

if enemy_group_rank:
    print("Adding Enemy Rank field")
    embed.add_field(name="Enemy Rank", value=enemy_group_rank, inline=False)
else:
    print("Enemy Rank is None")

[1] 输出: 我在获取函数中打印出主军衔和陆军军衔。他们都不是无。但是当我尝试访问嵌入中的陆军军衔时,它说“无”。这是为什么?我该如何解决这个问题?

命令输出:

用户等级:帝国发展办公室

用户中的军队

陆军200

陆军 是

军衔:帝国大臣

用户信息更新成功!

数据库中的用户信息已更新。

添加主排名字段

军衔无

敌人等级为无

python discord embed roblox
1个回答
0
投票

我自己也尝试过类似的脚本,它似乎按预期工作。你确定缩进没有错吗?

这可能是因为最后一个 else 语句不知何故被无效,而是一个真实的语句,其中 Army_group_rank 将始终设置为 after,无论之前的操作如何。

您是否尝试过使用断点来调试代码所经历的流程?

© www.soinside.com 2019 - 2024. All rights reserved.