69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import requests
|
||
import json
|
||
import logging
|
||
import toml
|
||
|
||
class McHh:
|
||
def __init__(self,qid,message):
|
||
self.qid = qid
|
||
self.message = message
|
||
|
||
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/hh" # 替换为实际的 API 地址
|
||
|
||
# 请求头
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Authorization": velocity_token # 替换为你的 API 密钥
|
||
}
|
||
|
||
# 请求体
|
||
payload = {
|
||
"qID": self.qid,
|
||
"message": self.message
|
||
}
|
||
|
||
# 发送 POST 请求
|
||
try:
|
||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||
|
||
time = response.text
|
||
|
||
try:
|
||
time = int(time)
|
||
seconds = time / 1000
|
||
formatted_time = round(seconds, 1)
|
||
except Exception as e :
|
||
logging.error(f"请求过程中出现异常: {e}")
|
||
return "服务器繁忙,请稍后再逝"
|
||
|
||
# 检查响应状态码
|
||
if response.status_code == 200:
|
||
return "喊话成功"
|
||
|
||
elif response.status_code == 403:
|
||
return "您未绑定MCUNC游戏账户,请先绑定后重试"
|
||
|
||
elif response.status_code == 429:
|
||
return f"喊话过快,剩余时间:{formatted_time}s"
|
||
|
||
elif response.status_code == 409:
|
||
return "喊话失败:您没有足够的话筒!"
|
||
|
||
else:
|
||
logging.error(f"请求失败!状态码: {response.status_code}")
|
||
logging.error(f"错误信息: {response.text}") # 打印错误信息
|
||
return "服务器繁忙,请稍后再逝"
|
||
|
||
except Exception as e:
|
||
logging.error(f"请求过程中出现异常: {e}")
|
||
return "服务器繁忙,请稍后再逝"
|