Files
HeXi/bot.py
T
sansenhoshiandClaude 9371a28e35 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>
2026-09-03 00:44:38 +08:00

64 lines
2.0 KiB
Python

import os
import sys
# 无控制台/指定日志文件时,把 stdout/stderr 写入日志文件(供 Web 日志查看器读取)
# 必须在 `import nonebot` 之前执行,否则 loguru 的 stdout sink 已绑定控制台
_log_file = os.environ.get("HEXI_LOG_FILE")
if _log_file or sys.stdout is None or sys.stderr is None:
if not _log_file:
_log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_bot_run.log")
try:
_log_fh = open(_log_file, "a", encoding="utf-8", buffering=1)
sys.stdout = _log_fh
sys.stderr = _log_fh
except OSError as _e: # noqa: BLE001
(sys.stderr or sys.__stderr__).write(f"打开日志文件失败: {_e}\n")
import nonebot
from nonebot.adapters.onebot.v11 import Adapter as ONEBOTV11Adapter
from nonebot.log import default_filter, default_format, logger
from sqlalchemy import StaticPool
from starlette.middleware.cors import CORSMiddleware
# 强制 loguru 即使写入非 TTY(重定向到 _bot_run.log / pythonw)也保留 ANSI 颜色码。
# 否则 nonebot 默认 sink 检测到文件非终端会自动剥离颜色,Web 日志查看器便看不到色彩。
logger.remove()
logger.add(
sys.stdout,
level=0,
diagnose=False,
filter=default_filter,
format=default_format,
colorize=True,
)
# 初始化 NoneBot 以及 数据库
nonebot.init(datastore_engine_options={"poolclass": StaticPool})
# 获取应用和驱动实例
app = nonebot.get_app()
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://192.168.2.186:5173",
"http://127.0.0.1:5173"
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
driver = nonebot.get_driver()
# 注册适配器
driver.register_adapter(ONEBOTV11Adapter)
# 加载插件
nonebot.load_plugins("hexi")
nonebot.load_from_toml("pyproject.toml")
if __name__ == "__main__":
logger.warning("hexi?启动!")
nonebot.run() # 直接运行,不需要传参