You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py机器人requests请求异常:无法匹配Sploop.io排行榜用户

Discord.py机器人无法匹配Sploop.io排行榜第二名用户的排查与修复

问题描述

我基于discord.py开发了一款Sploop.io赛事机器人,通过requests请求https://account.sploop.io/rankingScore获取排行榜数据(该榜单从第600名到第1名排序,右侧为用户ID,中间为用户名)。目前机器人其他功能正常,但始终提示某用户不在排行榜中,而该用户实际是游戏第二名。以下是机器人whois命令的代码:

@bot.command()
async def whois(ctx, member: discord.Member = None):
    if member is None:
        member = ctx.author

    user_id = member.id
    found = False

    def get_user_data(hashtag):
        url = "https://account.sploop.io/rankingScore"

        response = requests.get(url)
        data = response.json()

        for user in data:
            if user[2] == hashtag:
                score = user[0]
                leaderboard_username = user[1]
                position = data.index(user) + 1
                return score, leaderboard_username, position

        return None, None, None

    with open("linkaccount.txt", "r") as file:
        for line in file:
            if f"Discord={user_id}" in line:
                found = True
                info = line.split(", ")
                rank_account = info[3].split("=")[1]
                hashtag = info[4].split("=")[1]
                game_username = info[5].split("=")[1]
                region = info[2].split("=")[1]
                score, leaderboard_username, position = get_user_data(hashtag)

                if score is not None:
                    await ctx.reply(f"Discord Username: {member.name}\nRegion: {region}\nRank Account: {rank_account}\nHashtag: {hashtag}\nGame Username: {game_username}\nLeaderboard Rank: {position}\nLeaderboard Score: {score}\nUsername: {leaderboard_username}")
                else:
                    await ctx.reply(f"Discord Username: {member.name}\nRegion: {region}\nRank Account: {rank_account}\nHashtag: {hashtag}\nGame Username: {game_username}\nUser not found in the leaderboard.")
                break

    if not found:
        await ctx.reply("Account not found.")

排查与修复方案

1. 修正Hashtag字符串匹配问题

从文本文件读取的hashtag可能带有换行符或首尾空白,导致与API返回的字段不匹配。在获取hashtag后添加strip()处理:

hashtag = info[4].split("=")[1].strip()

2. 验证API返回的数据结构

先打印API返回的前几条数据,确认用户ID(hashtag)对应的数组索引是否正确。在get_user_data函数中添加调试代码:

data = response.json()
print("前5条数据结构:", data[:5])  # 查看数组元素的字段顺序

如果发现用户ID不在user[2]的位置,调整索引值即可。

3. 添加请求头避免被API拦截

部分API会拦截无User-Agent的请求,返回不完整数据。给requests请求添加标准请求头:

def get_user_data(hashtag):
    url = "https://account.sploop.io/rankingScore"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    # ... 后续代码不变

4. 修正排名计算逻辑

榜单是从第600名到第1名排序,原代码用data.index(user)+1会得到反向排名(比如第1名会被计算为600名)。正确的排名计算方式应为:

position = 600 - data.index(user)

如果使用字典映射优化查找,可改为:

user_map = {user[2]: (user[0], user[1], idx) for idx, user in enumerate(data)}
if hashtag in user_map:
    score, leaderboard_username, idx = user_map[hashtag]
    position = 600 - idx  # 正确计算排名
    return score, leaderboard_username, position

5. 优化查找效率(可选)

将数组转换为以hashtag为键的字典,避免循环遍历整个列表,提升查找速度:

def get_user_data(hashtag):
    url = "https://account.sploop.io/rankingScore"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    # 构建hashtag到用户数据的映射字典
    user_dict = {}
    for idx, user in enumerate(data):
        user_dict[user[2]] = (user[0], user[1], 600 - idx)
    return user_dict.get(hashtag, (None, None, None))

内容的提问来源于stack exchange,提问作者İlyax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:42:03