refactor: restructure plugins per MTSS standard, consolidate assets to res/
Per docs/plugin-audit-report.md (plugins normalized to Trigger(handlers) → Service(services) → Model(repository/models) + utils): - Split monolithic __init__.py into handlers/services/utils across dailywife, deer_pipe, dice, galgame_card, helldivers_tools, huoziyinshua, learning_chat, makeaquote, mc_server_status, ncm_saying, picfinder_take, picstatus, random_jm_code, regif, steam_info, video_analysis, group_tools - Move static assets under res/: deer_pipe font/img, makeaquote font, helldivers img/templates, huoziyinshua HuoZiYinShua - Add config.py + register_config_items to ncm_saying, random_jm_code, group_tools; learning_chat unified config bridge - Remove deprecated: voice_trans plugin, bf_bot/test.py, dead code in dailywife/deer_pipe, empty dirs, debug scripts under helldivers temp - Disable brash_general_supercredits_tools (stub comment only) - bot.py: optional stdout/stderr redirect to log file for Web log viewer, force ANSI colorize on non-TTY sinks - Move runtime data (jm_code.json) out of plugin dir into hexi/data - Docs: plugin-audit-report.md; README reflects removed plugins Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""随机 JM 码 —— Service 层。"""
|
||||
|
||||
from .store import add_new_jm_code, get_random_jm_code # noqa: F401
|
||||
@@ -0,0 +1,74 @@
|
||||
"""随机 JM 码 —— 数据访问(Service 层)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from .. import config as _cfg
|
||||
|
||||
# 运行时数据统一放 hexi/data
|
||||
_DATA_DIR = Path(__file__).resolve().parents[3] / "data" / "random_jm_code"
|
||||
FILE_PATH = _DATA_DIR / "jm_code.json"
|
||||
_LEGACY_FILE = Path(__file__).resolve().parents[1] / "jm_code.json"
|
||||
|
||||
JM_CODES: List[int] = []
|
||||
_LOADED = False
|
||||
LOCK = asyncio.Lock()
|
||||
|
||||
|
||||
def _migrate_legacy() -> None:
|
||||
if not FILE_PATH.exists() and _LEGACY_FILE.exists():
|
||||
_DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
os.replace(_LEGACY_FILE, FILE_PATH)
|
||||
|
||||
|
||||
def _safe_write_json(data, file_path: Path):
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp_path = file_path.with_suffix(".tmp")
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
os.replace(tmp_path, file_path)
|
||||
|
||||
|
||||
async def load_codes(file_path: Path = FILE_PATH):
|
||||
global JM_CODES, _LOADED
|
||||
if _LOADED:
|
||||
return
|
||||
async with LOCK:
|
||||
if _LOADED:
|
||||
return
|
||||
_migrate_legacy()
|
||||
if not file_path.exists():
|
||||
JM_CODES = []
|
||||
_safe_write_json(JM_CODES, file_path)
|
||||
else:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
try:
|
||||
JM_CODES = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
JM_CODES = []
|
||||
_LOADED = True
|
||||
|
||||
|
||||
async def get_random_jm_code() -> str:
|
||||
await load_codes()
|
||||
if not JM_CODES:
|
||||
return "暂无可用代码"
|
||||
code = random.choice(JM_CODES)
|
||||
template = random.choice(_cfg.TEXT_TEMPLATES)
|
||||
return template.format(code=code)
|
||||
|
||||
|
||||
async def add_new_jm_code(new_code: int) -> bool:
|
||||
await load_codes()
|
||||
async with LOCK:
|
||||
if new_code in JM_CODES:
|
||||
return False
|
||||
JM_CODES.append(new_code)
|
||||
_safe_write_json(JM_CODES, FILE_PATH)
|
||||
return True
|
||||
Reference in New Issue
Block a user