Files
vcbot-napcat/model/McFind.py
2025-07-05 17:26:12 +08:00

56 lines
2.0 KiB
Python

import requests
import json
import logging
import toml
class McFind:
def __init__(self,name):
self.name = name
def main(self):
with open('./config.toml', 'r', encoding='utf-8') as f:
config = toml.load(f)
# 获取所需字段
velocity_ip = config.get("velocity_ip")
velocity_port = config.get("velocity_port")
velocity_token = config.get("velocity_token")
# API URL
url = f"http://{velocity_ip}:{velocity_port}/vc/find_player" # 替换为实际的 API 地址
# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": velocity_token # 替换为你的 API 密钥
}
# 请求体
payload = {
"name": self.name,
}
# 发送 POST 请求
try:
response = requests.post(url, headers=headers, data=json.dumps(payload))
# 检查响应状态码
if response.status_code == 200:
logging.info("请求成功!返回结果:")
response_data = response.json()
logging.info(json.dumps(response_data, indent=4, ensure_ascii=False)) # 格式化输出 JSON
# 提取 answer 值
online = response_data.get("online", "online 字段不存在")
server = response_data.get("server", "server 字段不存在")
if online:
return f"{self.name}在线,所在服务器:{server}"
else:
return f"{self.name}不在线"
else:
logging.error(f"请求失败!状态码: {response.status_code}")
logging.error(f"错误信息: {response.text}") # 打印错误信息
return "服务器繁忙,请稍后再逝"
except Exception as e:
logging.error(f"请求过程中出现异常: {e}")
return "服务器繁忙,请稍后再逝"