mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🔧 增加Bot状态管理及模块禁用功能
This commit is contained in:
parent
40cce741fa
commit
ae03ad1665
@ -13,11 +13,15 @@ from tortoise.functions import Count
|
|||||||
from websockets.exceptions import ConnectionClosedError, ConnectionClosedOK
|
from websockets.exceptions import ConnectionClosedError, ConnectionClosedOK
|
||||||
|
|
||||||
from zhenxun.models.bot_connect_log import BotConnectLog
|
from zhenxun.models.bot_connect_log import BotConnectLog
|
||||||
|
from zhenxun.models.bot_console import BotConsole
|
||||||
from zhenxun.models.chat_history import ChatHistory
|
from zhenxun.models.chat_history import ChatHistory
|
||||||
from zhenxun.models.group_console import GroupConsole
|
from zhenxun.models.group_console import GroupConsole
|
||||||
from zhenxun.models.plugin_info import PluginInfo
|
from zhenxun.models.plugin_info import PluginInfo
|
||||||
from zhenxun.models.statistics import Statistics
|
from zhenxun.models.statistics import Statistics
|
||||||
|
from zhenxun.models.task_info import TaskInfo
|
||||||
from zhenxun.services.log import logger
|
from zhenxun.services.log import logger
|
||||||
|
from zhenxun.utils.common_utils import CommonUtils
|
||||||
|
from zhenxun.utils.enum import PluginType
|
||||||
from zhenxun.utils.platform import PlatformUtils
|
from zhenxun.utils.platform import PlatformUtils
|
||||||
|
|
||||||
from ....base_model import Result
|
from ....base_model import Result
|
||||||
@ -27,6 +31,9 @@ from .data_source import bot_live
|
|||||||
from .model import (
|
from .model import (
|
||||||
ActiveGroup,
|
ActiveGroup,
|
||||||
BaseInfo,
|
BaseInfo,
|
||||||
|
BotBlockModule,
|
||||||
|
BotManageUpdateParam,
|
||||||
|
BotStatusParam,
|
||||||
HotPlugin,
|
HotPlugin,
|
||||||
NonebotData,
|
NonebotData,
|
||||||
QueryCount,
|
QueryCount,
|
||||||
@ -94,13 +101,7 @@ async def _(bot_id: str | None = None) -> Result[list[BaseInfo]]:
|
|||||||
)
|
)
|
||||||
for bot in bot_list:
|
for bot in bot_list:
|
||||||
bot.bot = None # type: ignore
|
bot.bot = None # type: ignore
|
||||||
# 插件加载数量
|
select_bot.status = await BotConsole.get_bot_status(select_bot.self_id)
|
||||||
select_bot.plugin_count = await PluginInfo.all().count()
|
|
||||||
fail_count = await PluginInfo.filter(load_status=False).count()
|
|
||||||
select_bot.fail_plugin_count = fail_count
|
|
||||||
select_bot.success_plugin_count = (
|
|
||||||
select_bot.plugin_count - select_bot.fail_plugin_count
|
|
||||||
)
|
|
||||||
# 连接时间
|
# 连接时间
|
||||||
select_bot.connect_time = bot_live.get(select_bot.self_id) or 0
|
select_bot.connect_time = bot_live.get(select_bot.self_id) or 0
|
||||||
if select_bot.connect_time:
|
if select_bot.connect_time:
|
||||||
@ -347,6 +348,76 @@ async def _(
|
|||||||
return Result.ok(hot_plugin_list)
|
return Result.ok(hot_plugin_list)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/change_bot_status",
|
||||||
|
dependencies=[authentication()],
|
||||||
|
response_model=Result,
|
||||||
|
response_class=JSONResponse,
|
||||||
|
description="修改bot全局开关",
|
||||||
|
)
|
||||||
|
async def _(param: BotStatusParam):
|
||||||
|
try:
|
||||||
|
await BotConsole.set_bot_status(param.status, param.bot_id)
|
||||||
|
return Result.ok(info="修改bot全局开关成功!")
|
||||||
|
except ValueError:
|
||||||
|
return Result.fail("Bot未初始化...")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/get_bot_block_module",
|
||||||
|
dependencies=[authentication()],
|
||||||
|
response_model=Result[BotBlockModule],
|
||||||
|
response_class=JSONResponse,
|
||||||
|
description="修改bot全局开关",
|
||||||
|
)
|
||||||
|
async def _(bot_id: str) -> Result[BotBlockModule]:
|
||||||
|
try:
|
||||||
|
bot_data = await BotConsole.get_or_none(bot_id=bot_id)
|
||||||
|
if not bot_data:
|
||||||
|
return Result.fail("Bot数据不存在...")
|
||||||
|
block_tasks = []
|
||||||
|
block_plugins = []
|
||||||
|
all_plugins = await PluginInfo.filter(
|
||||||
|
load_status=True, plugin_type=PluginType.NORMAL
|
||||||
|
).values("module", "name")
|
||||||
|
all_task = await TaskInfo.annotate().values("module", "name")
|
||||||
|
if bot_data.block_tasks:
|
||||||
|
tasks = CommonUtils.convert_module_format(bot_data.block_tasks)
|
||||||
|
block_tasks = [t["module"] for t in all_task if t["module"] in tasks]
|
||||||
|
if bot_data.block_plugins:
|
||||||
|
plugins = CommonUtils.convert_module_format(bot_data.block_plugins)
|
||||||
|
block_plugins = [t["module"] for t in all_plugins if t["module"] in plugins]
|
||||||
|
return Result.ok(
|
||||||
|
BotBlockModule(
|
||||||
|
bot_id=bot_id,
|
||||||
|
block_tasks=block_tasks,
|
||||||
|
block_plugins=block_plugins,
|
||||||
|
all_plugins=all_plugins,
|
||||||
|
all_tasks=all_task,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("获取Bot数据失败", "webui", e=e)
|
||||||
|
return Result.fail(f"获取Bot数据失败 {type(e)}:{e}...")
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/update_bot_manage",
|
||||||
|
dependencies=[authentication()],
|
||||||
|
response_model=Result,
|
||||||
|
response_class=JSONResponse,
|
||||||
|
description="修改bot全局开关",
|
||||||
|
)
|
||||||
|
async def _(param: BotManageUpdateParam):
|
||||||
|
bot_data = await BotConsole.get_or_none(bot_id=param.bot_id)
|
||||||
|
if not bot_data:
|
||||||
|
return Result.fail("Bot数据不存在...")
|
||||||
|
bot_data.block_plugins = CommonUtils.convert_module_format(param.block_plugins)
|
||||||
|
bot_data.block_tasks = CommonUtils.convert_module_format(param.block_tasks)
|
||||||
|
await bot_data.save(update_fields=["block_plugins", "block_tasks"])
|
||||||
|
return Result.ok()
|
||||||
|
|
||||||
|
|
||||||
@ws_router.websocket("/system_status")
|
@ws_router.websocket("/system_status")
|
||||||
async def system_logs_realtime(websocket: WebSocket, sleep: int = 5):
|
async def system_logs_realtime(websocket: WebSocket, sleep: int = 5):
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
|
|||||||
@ -1,8 +1,45 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
from nonebot.adapters import Bot
|
from nonebot.adapters import Bot
|
||||||
from nonebot.config import Config
|
from nonebot.config import Config
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class BotManageUpdateParam(BaseModel):
|
||||||
|
"""bot更新参数"""
|
||||||
|
|
||||||
|
bot_id: str
|
||||||
|
"""bot id"""
|
||||||
|
block_plugins: list[str]
|
||||||
|
"""禁用插件"""
|
||||||
|
block_tasks: list[str]
|
||||||
|
"""禁用被动"""
|
||||||
|
|
||||||
|
|
||||||
|
class BotStatusParam(BaseModel):
|
||||||
|
"""bot状态参数"""
|
||||||
|
|
||||||
|
bot_id: str
|
||||||
|
"""bot id"""
|
||||||
|
status: bool
|
||||||
|
"""状态"""
|
||||||
|
|
||||||
|
|
||||||
|
class BotBlockModule(BaseModel):
|
||||||
|
"""bot禁用模块参数"""
|
||||||
|
|
||||||
|
bot_id: str
|
||||||
|
"""bot id"""
|
||||||
|
block_plugins: list[str]
|
||||||
|
"""禁用插件"""
|
||||||
|
block_tasks: list[str]
|
||||||
|
"""禁用被动"""
|
||||||
|
all_plugins: list[dict[str, Any]]
|
||||||
|
"""所有插件"""
|
||||||
|
all_tasks: list[dict[str, Any]]
|
||||||
|
"""所有被动"""
|
||||||
|
|
||||||
|
|
||||||
class SystemStatus(BaseModel):
|
class SystemStatus(BaseModel):
|
||||||
"""
|
"""
|
||||||
系统状态
|
系统状态
|
||||||
@ -36,13 +73,8 @@ class BaseInfo(BaseModel):
|
|||||||
"""连接日期"""
|
"""连接日期"""
|
||||||
connect_count: int = 0
|
connect_count: int = 0
|
||||||
"""连接次数"""
|
"""连接次数"""
|
||||||
|
status: bool = False
|
||||||
plugin_count: int = 0
|
"""全局状态"""
|
||||||
"""加载插件数量"""
|
|
||||||
success_plugin_count: int = 0
|
|
||||||
"""加载成功插件数量"""
|
|
||||||
fail_plugin_count: int = 0
|
|
||||||
"""加载失败插件数量"""
|
|
||||||
|
|
||||||
is_select: bool = False
|
is_select: bool = False
|
||||||
"""当前选择"""
|
"""当前选择"""
|
||||||
|
|||||||
@ -31,7 +31,7 @@ class Result(Generic[RT], BaseModel):
|
|||||||
"""info"""
|
"""info"""
|
||||||
warning: str | None = None
|
warning: str | None = None
|
||||||
"""警告信息"""
|
"""警告信息"""
|
||||||
data: RT = None
|
data: RT | None = None
|
||||||
"""返回数据"""
|
"""返回数据"""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user