2024-03-22 07:44:41 +08:00
|
|
|
|
import json
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
import aiofiles
|
|
|
|
|
import nonebot.plugin
|
|
|
|
|
|
2024-03-24 09:43:34 +08:00
|
|
|
|
from liteyuki.utils.data import Database, LiteModel
|
|
|
|
|
from liteyuki.utils.data_manager import GroupChat, InstalledPlugin, User, group_db, plugin_db, user_db
|
|
|
|
|
from liteyuki.utils.ly_typing import T_MessageEvent
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
2024-03-18 18:21:56 +08:00
|
|
|
|
LNPM_COMMAND_START = "lnpm"
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-22 07:44:41 +08:00
|
|
|
|
class PluginTag(LiteModel):
|
|
|
|
|
label: str
|
|
|
|
|
color: str = '#000000'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StorePlugin(LiteModel):
|
|
|
|
|
name: str
|
|
|
|
|
desc: str
|
|
|
|
|
module_name: str
|
2024-03-26 17:14:41 +08:00
|
|
|
|
project_link: str = ""
|
|
|
|
|
homepage: str =""
|
|
|
|
|
author: str = ""
|
2024-03-22 07:44:41 +08:00
|
|
|
|
type: str | None = None
|
2024-03-26 17:14:41 +08:00
|
|
|
|
version: str | None = ""
|
|
|
|
|
time: str = ""
|
2024-03-22 07:44:41 +08:00
|
|
|
|
tags: list[PluginTag] = []
|
|
|
|
|
is_official: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_store_plugin(plugin_module_name: str) -> Optional[StorePlugin]:
|
|
|
|
|
"""
|
|
|
|
|
获取插件信息
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
plugin_module_name (str): 插件模块名
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Optional[StorePlugin]: 插件信息
|
|
|
|
|
"""
|
|
|
|
|
async with aiofiles.open("data/liteyuki/plugins.json", "r", encoding="utf-8") as f:
|
|
|
|
|
plugins: list[StorePlugin] = [StorePlugin(**pobj) for pobj in json.loads(await f.read())]
|
|
|
|
|
for plugin in plugins:
|
|
|
|
|
if plugin.module_name == plugin_module_name:
|
|
|
|
|
return plugin
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_plugin_default_enable(plugin_module_name: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
获取插件默认启用状态,由插件定义,不存在则默认为启用
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
plugin_module_name (str): 插件模块名
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: 插件默认状态
|
|
|
|
|
"""
|
2024-03-24 09:43:34 +08:00
|
|
|
|
plug = nonebot.plugin.get_plugin_by_module_name(plugin_module_name)
|
2024-03-26 17:14:41 +08:00
|
|
|
|
return (plug.metadata.extra.get("default_enable", True)
|
2024-03-24 09:43:34 +08:00
|
|
|
|
if plug.metadata else True) if plug else True
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-24 09:43:34 +08:00
|
|
|
|
def get_plugin_session_enable(event: T_MessageEvent, plugin_module_name: str) -> bool:
|
2024-03-22 07:44:41 +08:00
|
|
|
|
"""
|
2024-03-24 09:43:34 +08:00
|
|
|
|
获取插件当前会话启用状态
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
Args:
|
2024-03-24 09:43:34 +08:00
|
|
|
|
event: 会话事件
|
2024-03-22 07:44:41 +08:00
|
|
|
|
plugin_module_name (str): 插件模块名
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: 插件当前状态
|
|
|
|
|
"""
|
|
|
|
|
if event.message_type == "group":
|
2024-03-26 17:14:41 +08:00
|
|
|
|
session: GroupChat = group_db.first(GroupChat, "group_id = ?", event.group_id, default=GroupChat(group_id=str(event.group_id)))
|
2024-03-22 07:44:41 +08:00
|
|
|
|
else:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
session: User = user_db.first(User, "user_id = ?", event.user_id, default=User(user_id=str(event.user_id)))
|
2024-03-22 07:44:41 +08:00
|
|
|
|
# 默认停用插件在启用列表内表示启用
|
|
|
|
|
# 默认停用插件不在启用列表内表示停用
|
|
|
|
|
# 默认启用插件在停用列表内表示停用
|
|
|
|
|
# 默认启用插件不在停用列表内表示启用
|
|
|
|
|
default_enable = get_plugin_default_enable(plugin_module_name)
|
|
|
|
|
if default_enable:
|
|
|
|
|
return plugin_module_name not in session.disabled_plugins
|
|
|
|
|
else:
|
|
|
|
|
return plugin_module_name in session.enabled_plugins
|
|
|
|
|
|
|
|
|
|
|
2024-03-24 09:43:34 +08:00
|
|
|
|
def get_plugin_global_enable(plugin_module_name: str) -> bool:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2024-03-22 07:44:41 +08:00
|
|
|
|
def get_plugin_can_be_toggle(plugin_module_name: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
获取插件是否可以被启用/停用
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
2024-03-22 07:44:41 +08:00
|
|
|
|
Args:
|
|
|
|
|
plugin_module_name (str): 插件模块名
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
2024-03-22 07:44:41 +08:00
|
|
|
|
Returns:
|
|
|
|
|
bool: 插件是否可以被启用/停用
|
|
|
|
|
"""
|
2024-03-24 09:43:34 +08:00
|
|
|
|
plug = nonebot.plugin.get_plugin_by_module_name(plugin_module_name)
|
2024-03-26 17:14:41 +08:00
|
|
|
|
return plug.metadata.extra.get("toggleable", True) if plug and plug.metadata else True
|