feat: 插件启用停用检测

This commit is contained in:
远野千束 2024-03-26 22:33:17 +08:00
parent 58e603e1ad
commit ac234544a3
4 changed files with 32 additions and 17 deletions

View File

@ -19,7 +19,7 @@ init_log()
installed_plugins: list[InstalledPlugin] = plugin_db.all(InstalledPlugin())
if installed_plugins:
for installed_plugin in installed_plugins:
if not installed_plugin.liteyuki and not check_for_package(installed_plugin.module_name):
if not check_for_package(installed_plugin.module_name):
nonebot.logger.error(f"{installed_plugin.module_name} not installed, but in loading database. please run `npm fixup` in chat to reinstall it.")
else:
nonebot.load_plugin(installed_plugin.module_name)

View File

@ -5,7 +5,7 @@ import aiofiles
import nonebot.plugin
from liteyuki.utils.data import Database, LiteModel
from liteyuki.utils.data_manager import Group, InstalledPlugin, User, group_db, plugin_db, user_db
from liteyuki.utils.data_manager import GlobalPlugin, Group, InstalledPlugin, User, group_db, plugin_db, user_db
from liteyuki.utils.ly_typing import T_MessageEvent
LNPM_COMMAND_START = "lnpm"
@ -90,11 +90,12 @@ def get_plugin_session_enable(event: T_MessageEvent, plugin_module_name: str) ->
def get_plugin_global_enable(plugin_module_name: str) -> bool:
loaded_plugin = nonebot.plugin.get_plugin_by_module_name(plugin_module_name)
return plugin_db.first(
InstalledPlugin(),
GlobalPlugin(),
"module_name = ?",
plugin_module_name,
default=InstalledPlugin(module_name=plugin_module_name, enabled=True)).enabled
default=GlobalPlugin(module_name=plugin_module_name, enabled=True)).enabled
def get_plugin_can_be_toggle(plugin_module_name: str) -> bool:

View File

@ -1,18 +1,21 @@
import os
import nonebot.plugin
from nonebot import on_command, require
from nonebot.exception import FinishedException
from nonebot import require
from nonebot.exception import FinishedException, IgnoredException
from nonebot.internal.adapter import Event
from nonebot.internal.matcher import Matcher
from nonebot.adapters import Bot
from nonebot.message import run_preprocessor
from nonebot.permission import SUPERUSER
from nonebot.plugin import Plugin
from liteyuki.utils.data_manager import Group, InstalledPlugin, User, group_db, plugin_db, user_db
from liteyuki.utils.data_manager import GlobalPlugin, Group, InstalledPlugin, User, group_db, plugin_db, user_db
from liteyuki.utils.language import get_user_lang
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message import Markdown as md, send_markdown
from liteyuki.utils.permission import GROUP_ADMIN, GROUP_OWNER
from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.language import get_user_lang
from .common import get_plugin_can_be_toggle, get_plugin_global_enable, get_plugin_session_enable, get_plugin_default_enable
from .common import get_plugin_can_be_toggle, get_plugin_default_enable, get_plugin_global_enable, get_plugin_session_enable
from .installer import get_store_plugin, npm_update
require("nonebot_plugin_alconna")
@ -197,7 +200,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")))
try:
plugin = plugin_db.first(InstalledPlugin(), "module_name = ?", plugin_module_name, default=InstalledPlugin(module_name=plugin_module_name))
plugin = plugin_db.first(GlobalPlugin(), "module_name = ?", plugin_module_name, default=GlobalPlugin(module_name=plugin_module_name))
if toggle:
plugin.enabled = True
else:
@ -220,8 +223,20 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))
)
@run_preprocessor
async def _(event: T_MessageEvent, matcher: Matcher):
plugin = matcher.plugin
async def pre_handle(event: Event, matcher: Matcher):
plugin: Plugin = matcher.plugin
plugin_global_enable = get_plugin_global_enable(plugin.module_name)
if not plugin_global_enable:
raise IgnoredException("Plugin disabled globally")
if event.get_type() == "message":
plugin_session_enable = get_plugin_session_enable(event, plugin.module_name)
if not plugin_session_enable:
raise IgnoredException("Plugin disabled in session")
@Bot.on_calling_api
async def _(bot: Bot, api: str, data: dict[str, any]):
# TODO 插件启用/停用检查hook
nonebot.logger.info(f"Plugin Callapi: {plugin.module_name}")
nonebot.logger.info(f"Plugin Callapi: {api}: {data}")

View File

@ -31,8 +31,6 @@ class Group(LiteModel):
class InstalledPlugin(LiteModel):
liteyuki: bool = Field(True, alias="liteyuki") # 是否为LiteYuki插件
enabled: bool = Field(True, alias="enabled") # 全局启用
TABLE_NAME = "installed_plugin"
module_name: str = Field(str(), alias="module_name")
version: str = Field(str(), alias="version")
@ -40,6 +38,7 @@ class InstalledPlugin(LiteModel):
class GlobalPlugin(LiteModel):
TABLE_NAME = "global_plugin"
liteyuki: bool = Field(True, alias="liteyuki") # 是否为LiteYuki插件
module_name: str = Field(str(), alias="module_name")
enabled: bool = Field(True, alias="enabled")
@ -48,5 +47,5 @@ def auto_migrate():
print("Migrating databases...")
user_db.auto_migrate(User())
group_db.auto_migrate(Group())
plugin_db.auto_migrate(InstalledPlugin())
plugin_db.auto_migrate(InstalledPlugin(), GlobalPlugin())
common_db.auto_migrate(GlobalPlugin())