Files
HeXi/hexi/plugins/nonebot_plugin_mc_server_status/config.py
T
sansenhoshiandClaude Code 814c1994b6 feat(mc-server-status): 服务器列表 Web 管理页 / 数据层抽离 / mcstatus 13 适配
- services/servers.py:校验 + 读写 + 增删改收敛成一份数据层(顶层不 import nonebot,
  可脱离运行时单测),群命令与 Web API 都只经它改数据,校验不再各写一套
- web_hub.py + /hub/mc_server_status 页面:按群列出服务器,抽屉新增/编辑(改名)/
  测试连接,行内删除(删空一台连带删群);每条路由自带 require_admin
- handlers/server.py 改为薄封装,只在需要命令特有提示时自己判断
- services/mc.py 的图标属性名兼容 mcstatus ≥13 的 icon(旧版叫 favicon,
  读错会把整条查询打成"查询失败")
- tests/test_mc_servers.py:数据层归一/改名/删群/坏文件兜底 + 图标解码两版属性名

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-22 14:23:46 +08:00

115 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from os import makedirs
from pathlib import Path
from typing import List, Optional
from nonebot import get_bot, get_bots, get_driver, get_plugin_config
from nonebot.adapters import Bot
from pydantic import BaseModel
from .services import servers as store
# 数据目录:项目 hexi/data/ 下(与其它插件数据统一)
DATA_DIR = Path(__file__).resolve().parents[2] / "data"
class Config(BaseModel):
# 管理员的QQ号(别问我为什么)
mc_status_admin_qqnum: List[int] = [] # 必填
# 机器人的QQ号(如果写了就按优先级响应,否则就第一个连上的响应) ['1234','5678','6666']
mc_status_bot_qqnum_list: List[str] = [] # 可选
# 数据文件名
mc_status_data_filename: str = "mc_status_data.json"
class Var:
# 处理消息的bot
handle_bot: Optional[Bot] = None
# {"123456": {"提肛": ["mc.hypixel.net:25565","java"]}}
group_list = {}
driver = get_driver()
pc = get_plugin_config(Config)
var = Var()
@driver.on_startup
async def on_startup():
makedirs(DATA_DIR, exist_ok=True)
if not (DATA_DIR / pc.mc_status_data_filename).exists():
save_file()
else:
load_file()
# 群命令与 Web 管理台都改 var.group_list,保存时整表落盘(见 services/servers.py)
def load_file():
"""数据读写都在 services/servers.py(群命令与 Web 管理台共用同一份校验)。"""
var.group_list.update(store.load_servers(pc.mc_status_data_filename))
def save_file():
store.save_servers(pc.mc_status_data_filename, var.group_list)
# qq机器人连接时执行
@driver.on_bot_connect
async def on_bot_connect(bot: Bot):
# 是否有写bot qq,如果写了只处理bot qq在列表里的
if pc.mc_status_bot_qqnum_list and bot.self_id in pc.mc_status_bot_qqnum_list:
# 如果已经有bot连了
if var.handle_bot:
# 当前bot qq 下标
handle_bot_id_index = pc.mc_status_bot_qqnum_list.index(
var.handle_bot.self_id
)
# 新连接的bot qq 下标
new_bot_id_index = pc.mc_status_bot_qqnum_list.index(bot.self_id)
# 判断优先级,下标越低优先级越高
if new_bot_id_index < handle_bot_id_index:
var.handle_bot = bot
# 没bot连就直接给
else:
var.handle_bot = bot
# 不写就给第一个连的
elif not pc.mc_status_bot_qqnum_list and not var.handle_bot:
var.handle_bot = bot
# qq机器人断开时执行
@driver.on_bot_disconnect
async def on_bot_disconnect(bot: Bot):
# 判断掉线的是否为handle bot
if bot == var.handle_bot:
# 如果有写bot qq列表
if pc.mc_status_bot_qqnum_list:
# 获取当前连着的bot列表(需要bot是在bot qq列表里)
available_bot_id_list = [
bot_id for bot_id in get_bots() if bot_id in pc.mc_status_bot_qqnum_list
]
if available_bot_id_list:
# 打擂台排序?
new_bot_index = pc.mc_status_bot_qqnum_list.index(
available_bot_id_list[0]
)
for bot_id in available_bot_id_list:
now_bot_index = pc.mc_status_bot_qqnum_list.index(bot_id)
if now_bot_index < new_bot_index:
new_bot_index = now_bot_index
# 取下标在qq列表里最小的bot qq为新的handle bot
var.handle_bot = get_bot(pc.mc_status_bot_qqnum_list[new_bot_index])
else:
var.handle_bot = None
# 不写就随便给一个连着的(如果有)
elif var.handle_bot:
try:
new_bot = get_bot()
var.handle_bot = new_bot
except ValueError:
var.handle_bot = None