如何基于MEE6现有数据搭建Discord自定义等级系统及API授权问题
问题:获取MEE6等级数据时遇到KeyError及401授权问题
我正在为一个用了多年MEE6等级系统的Discord服务器开发自定义Bot,想基于MEE6已存的等级数据搭建新系统。参考Stack Overflow的代码尝试获取MEE6服务器数据:
import requests r = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{735338082647146496}') players_json = r.json()['players'] user_id = 766571696491986965 player_level = [i for i in players_json if i['id'] == str(user_id)][0]['level'] print(player_level)
运行时触发错误:
'players' File "C:\Users\REDACTED\Downloads\leveling testing.py", line 4, in <module> players_json = r.json()['players'] ~~~~~~~~^^^^^^^^^^^ KeyError: 'players'
编辑补充:直接访问该MEE6 API链接返回401授权错误:
{"error":{"message":"The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required."},"status_code":401}
我是服务器管理员,能访问MEE6面板,请问怎么获取授权访问API?如果API确实不能用了,还有啥办法获取当前服务器的等级数据?
解决方案
一、解决API授权问题
MEE6的私有服务器数据API现在需要授权才能访问,作为管理员可以按以下步骤获取凭证并修改代码:
- 获取Authorization凭证:
- 登录Discord并打开你的服务器MEE6面板
- 打开浏览器开发者工具(F12),切换到「网络」标签
- 刷新MEE6面板页面,找到任意一个
https://mee6.xyz/api/开头的请求 - 在请求头里复制
Authorization字段的值(格式为Bearer 一串随机字符)
- 修改后的Python代码:
import requests server_id = 735338082647146496 user_id = 766571696491986965 # 替换成你获取到的Authorization值 auth_token = "Bearer 你的凭证内容" headers = {"Authorization": auth_token} r = requests.get(f'https://mee6.xyz/api/plugins/levels/leaderboard/{server_id}', headers=headers) # 先检查请求是否成功 if r.status_code == 200: players_json = r.json()['players'] # 处理可能找不到用户的情况 try: player_level = next(i['level'] for i in players_json if i['id'] == str(user_id)) print(player_level) except StopIteration: print("该用户未在MEE6等级数据中") else: print(f"请求失败,状态码:{r.status_code},错误信息:{r.json()}")
二、API不可用的替代方案
- 导出MEE6数据:在MEE6服务器等级面板里找「导出数据」选项,直接导出包含所有用户等级、经验值的CSV文件,之后把CSV数据导入到自己Bot的数据库中
- Webhook同步:检查MEE6的集成设置,看是否支持将等级更新事件推送到自定义Webhook,这样可以实时同步新的等级数据到你的Bot
- 手动迁移+接管:一次性导出历史数据后,用自定义Bot完全接管等级计算逻辑,不再依赖MEE6
内容的提问来源于stack exchange,提问作者DRags
相关产品推荐
相关产品推荐

