我的主程序 ro.py 会将收到的消息进行拆解,将”/ 战绩 “后的值作为 “Player_id” 传递给 zj.py,也会将 zj.py 收到的返回进行判断来发送对应的消息
我的需求是,当 zj.py 返回的值为 1 时发送 “API 接口已上限,请稍等 1-2 分钟后查询”,当返回值为 2 时发送 “未找到该玩家信息,请核对玩家 ID 后重试”
但现在遇到个问题,为什么我即使是输入错误的 ID,但是返回的值还是 1?一直显示“API 接口已上限,请稍等 1-2 分钟后查询”
ro.py
[Python] 纯文本查看 复制代码 class MyClient(botpy.Client):
async def on_group_at_message_create(self, message: GroupMessage):
content = message.content.strip()
if content.startswith("/战绩 "):
player_id = content.split(" ")[1]
print("Detected /战绩 message with player_id:", player_id)
# await message._api.post_group_message(
# group_openid=message.group_openid,
# msg_type=0,
# msg_id=message.id,
# content="正在帮你查询中,请稍等~")
result = subprocess.run([sys.executable, "zj.py", player_id], capture_output=True, text=True, encoding='utf-8')
if result.returncode == 2:
messageResult = await message._api.post_group_message(
group_openid=message.group_openid,
msg_type=0,
msg_id=message.id,
content="API接口已上限,请稍等1-2分钟后查询")
_log.info(messageResult)
elif result.returncode == 1:
messageResult = await message._api.post_group_message(
group_openid=message.group_openid,
msg_type=0,
msg_id=message.id,
content="未找到该玩家信息,请核对玩家ID后重试")
_log.info(messageResult)
else:
file_url = f"http://bot.6638.cf:443/{player_id}_stats.png"
uploadMedia = await self.api.post_group_file(
group_openid=message.group_openid,
file_type=1,
url=file_url
)
await self.api.post_group_message(
group_openid=message.group_openid,
msg_type=7, # 7表示富媒体类型
msg_id=message.id,
media=uploadMedia,
content=' ',
)
time.sleep(10)
file_path = os.path.join(os.getcwd(), f"{player_id}_stats.png")
if os.path.exists(file_path):
os.remove(file_path)
zj.py
[Asm] 纯文本查看 复制代码 def get_player_data(player_id):
url = f"https://api.pubg.com/shards/steam/players?filter[playerNames]={player_id}"
response = requests.get(url, headers=headers)
if not response.text.strip():
return 1
elif "No Players Found Matching Criteria" in response.text:
return 2
data = response.json()
ban_type = data['data'][0]['attributes']['banType'] if 'data' in data and data['data'] else None
return data, ban_type |