mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🎨 显示添加优先级
This commit is contained in:
parent
7f5f689900
commit
e84fc89fed
@ -16,7 +16,7 @@ from zhenxun.models.sign_user import SignUser
|
||||
from zhenxun.models.user_console import UserConsole
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.decorator.shop import shop_register
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.manager.resource_manager import ResourceManager
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
@ -71,7 +71,7 @@ from public.bag_users t1
|
||||
"""
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
await ResourceManager.init_resources()
|
||||
"""签到与用户的数据迁移"""
|
||||
|
||||
@ -14,7 +14,7 @@ from zhenxun.services.log import logger
|
||||
from zhenxun.utils._build_image import BuildImage
|
||||
from zhenxun.utils._image_template import ImageTemplate
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
BASE_PATH = DATA_PATH / "welcome_message"
|
||||
@ -92,7 +92,7 @@ def migrate(path: Path):
|
||||
json.dump(new_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
def _():
|
||||
"""数据迁移
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.configs.utils import RegisterConfig
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
_yaml = YAML(pure=True)
|
||||
_yaml.allow_unicode = True
|
||||
@ -103,7 +103,7 @@ def _generate_simple_config(exists_module: list[str]):
|
||||
temp_file.unlink()
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup(0)
|
||||
@PriorityLifecycle.on_startup(priority=0)
|
||||
def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
|
||||
@ -20,7 +20,7 @@ from zhenxun.utils.enum import (
|
||||
PluginLimitType,
|
||||
PluginType,
|
||||
)
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
from .manager import manager
|
||||
|
||||
@ -96,7 +96,7 @@ async def _handle_setting(
|
||||
)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
|
||||
@ -10,7 +10,7 @@ from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.common_utils import CommonUtils
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
@ -133,7 +133,7 @@ async def create_schedule(task: Task):
|
||||
logger.error(f"动态创建定时任务 {task.name}({task.module}) 失败", e=e)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
|
||||
@ -10,12 +10,12 @@ from zhenxun.configs.path_config import TEXT_PATH
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def update_city():
|
||||
"""
|
||||
部分插件需要中国省份城市
|
||||
@ -62,7 +62,7 @@ async def _():
|
||||
await update_city()
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
"""开启/禁用插件格式修改"""
|
||||
_, is_create = await GroupConsole.get_or_create(group_id=133133133)
|
||||
|
||||
@ -16,7 +16,7 @@ from zhenxun.models.sign_log import SignLog
|
||||
from zhenxun.models.sign_user import SignUser
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.image_utils import BuildImage
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
from .config import (
|
||||
@ -55,7 +55,7 @@ LG_MESSAGE = [
|
||||
]
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def init_image():
|
||||
SIGN_RESOURCE_PATH.mkdir(parents=True, exist_ok=True)
|
||||
SIGN_TODAY_CARD_PATH.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
@ -10,7 +10,7 @@ from zhenxun.configs.config import Config as gConfig
|
||||
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||
from zhenxun.services.log import logger, logger_
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
from .api.configure import router as configure_router
|
||||
from .api.logs import router as ws_log_routes
|
||||
@ -94,7 +94,7 @@ WsApiRouter.include_router(status_routes)
|
||||
WsApiRouter.include_router(chat_routes)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup(0)
|
||||
@PriorityLifecycle.on_startup(priority=0)
|
||||
async def _():
|
||||
try:
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ default_menus = [
|
||||
]
|
||||
|
||||
|
||||
class MenuManage:
|
||||
class MenuManager:
|
||||
def __init__(self) -> None:
|
||||
self.file = DATA_PATH / "web_ui" / "menu.json"
|
||||
self.menu = []
|
||||
@ -50,15 +50,7 @@ class MenuManage:
|
||||
)
|
||||
)
|
||||
else:
|
||||
temp_menu.append(
|
||||
default_menus[
|
||||
next(
|
||||
i
|
||||
for i, m in enumerate(default_menus)
|
||||
if m.module == module
|
||||
)
|
||||
]
|
||||
)
|
||||
temp_menu.append(self.__get_menu_model(module))
|
||||
self.menu = temp_menu
|
||||
except Exception as e:
|
||||
logger.warning("菜单文件损坏,已重新生成...", "WebUi", e=e)
|
||||
@ -66,6 +58,11 @@ class MenuManage:
|
||||
self.menu = default_menus
|
||||
self.save()
|
||||
|
||||
def __get_menu_model(self, module: str):
|
||||
return default_menus[
|
||||
next(i for i, m in enumerate(default_menus) if m.module == module)
|
||||
]
|
||||
|
||||
def get_menus(self):
|
||||
return MenuData(menus=self.menu)
|
||||
|
||||
@ -76,4 +73,4 @@ class MenuManage:
|
||||
json.dump(temp, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
menu_manage = MenuManage()
|
||||
menu_manage = MenuManager()
|
||||
|
||||
@ -13,7 +13,7 @@ from zhenxun.models.bot_connect_log import BotConnectLog
|
||||
from zhenxun.models.chat_history import ChatHistory
|
||||
from zhenxun.models.statistics import Statistics
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
from ....base_model import BaseResultModel, QueryModel
|
||||
@ -32,7 +32,7 @@ driver: Driver = nonebot.get_driver()
|
||||
CONNECT_TIME = 0
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
global CONNECT_TIME
|
||||
CONNECT_TIME = int(time.time())
|
||||
|
||||
@ -8,7 +8,7 @@ from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
from ....base_model import BaseResultModel, QueryModel, Result
|
||||
from ....utils import authentication
|
||||
@ -22,7 +22,7 @@ router = APIRouter(prefix="/database")
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
for plugin in nonebot.get_loaded_plugins():
|
||||
module = plugin.name
|
||||
|
||||
@ -1,6 +1,12 @@
|
||||
import sys
|
||||
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import nonebot
|
||||
from strenum import StrEnum
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from enum import StrEnum
|
||||
else:
|
||||
from strenum import StrEnum
|
||||
|
||||
from zhenxun.configs.path_config import DATA_PATH, TEMP_PATH
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ from tortoise.models import Model as Model_
|
||||
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.utils.exception import HookPriorityException
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
from .log import logger
|
||||
|
||||
@ -48,7 +48,7 @@ class DbConnectError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup(1)
|
||||
@PriorityLifecycle.on_startup(priority=1)
|
||||
async def init():
|
||||
if not BotConfig.db_url:
|
||||
# raise DbUrlIsNode("数据库配置为空,请在.env.dev中配置DB_URL...")
|
||||
|
||||
@ -6,7 +6,7 @@ from nonebot.utils import is_coroutine_callable
|
||||
from pydantic import BaseModel
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
@ -101,6 +101,6 @@ class PluginInitManager:
|
||||
logger.error(f"执行: {module_path}:remove 失败", e=e)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@PriorityLifecycle.on_startup()
|
||||
async def _():
|
||||
await PluginInitManager.install_all()
|
||||
|
||||
@ -1,12 +1,17 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
import random
|
||||
import sys
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from strenum import StrEnum
|
||||
|
||||
from ._build_image import BuildImage
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from enum import StrEnum
|
||||
else:
|
||||
from strenum import StrEnum
|
||||
|
||||
|
||||
class MatType(StrEnum):
|
||||
LINE = "LINE"
|
||||
|
||||
@ -1,7 +1,12 @@
|
||||
from strenum import StrEnum
|
||||
import sys
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from enum import StrEnum
|
||||
else:
|
||||
from strenum import StrEnum
|
||||
|
||||
|
||||
class HookPriorityType(StrEnum):
|
||||
class PriorityLifecycle(StrEnum):
|
||||
STARTUP = "STARTUP"
|
||||
"""启动"""
|
||||
SHUTDOWN = "SHUTDOWN"
|
||||
|
||||
@ -1,13 +1,18 @@
|
||||
import contextlib
|
||||
import sys
|
||||
from typing import Protocol
|
||||
|
||||
from aiocache import cached
|
||||
from nonebot.compat import model_dump
|
||||
from pydantic import BaseModel, Field
|
||||
from strenum import StrEnum
|
||||
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from enum import StrEnum
|
||||
else:
|
||||
from strenum import StrEnum
|
||||
|
||||
from .const import (
|
||||
CACHED_API_TTL,
|
||||
GIT_API_COMMIT_FORMAT,
|
||||
|
||||
@ -5,17 +5,17 @@ import nonebot
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import HookPriorityType
|
||||
from zhenxun.utils.enum import PriorityLifecycle
|
||||
from zhenxun.utils.exception import HookPriorityException
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
|
||||
class HookPriorityManager:
|
||||
_data: ClassVar[dict[HookPriorityType, dict[int, list[Callable]]]] = {}
|
||||
class PriorityLifecycle:
|
||||
_data: ClassVar[dict[PriorityLifecycle, dict[int, list[Callable]]]] = {}
|
||||
|
||||
@classmethod
|
||||
def add(cls, hook_type: HookPriorityType, func: Callable, priority: int = 5):
|
||||
def add(cls, hook_type: PriorityLifecycle, func: Callable, priority: int = 5):
|
||||
if hook_type not in cls._data:
|
||||
cls._data[hook_type] = {}
|
||||
if priority not in cls._data[hook_type]:
|
||||
@ -23,17 +23,17 @@ class HookPriorityManager:
|
||||
cls._data[hook_type][priority].append(func)
|
||||
|
||||
@classmethod
|
||||
def on_startup(cls, priority: int = 5):
|
||||
def on_startup(cls, *, priority: int = 5):
|
||||
def wrapper(func):
|
||||
cls.add(HookPriorityType.STARTUP, func, priority)
|
||||
cls.add(PriorityLifecycle.STARTUP, func, priority)
|
||||
return func
|
||||
|
||||
return wrapper
|
||||
|
||||
@classmethod
|
||||
def on_shutdown(cls, priority: int = 5):
|
||||
def on_shutdown(cls, *, priority: int = 5):
|
||||
def wrapper(func):
|
||||
cls.add(HookPriorityType.SHUTDOWN, func, priority)
|
||||
cls.add(PriorityLifecycle.SHUTDOWN, func, priority)
|
||||
return func
|
||||
|
||||
return wrapper
|
||||
@ -41,7 +41,7 @@ class HookPriorityManager:
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
priority_data = HookPriorityManager._data.get(HookPriorityType.STARTUP)
|
||||
priority_data = PriorityLifecycle._data.get(PriorityLifecycle.STARTUP)
|
||||
if not priority_data:
|
||||
return
|
||||
priority_list = sorted(priority_data.keys())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user