初始化Bot管理插件

This commit is contained in:
HibiKier 2024-11-18 13:34:11 +08:00
parent b0a5371574
commit ee272ec07b
2 changed files with 63 additions and 21 deletions

View File

@ -1,10 +1,17 @@
from pathlib import Path
import nonebot
from nonebot.adapters import Bot
from nonebot.plugin import PluginMetadata
from zhenxun.utils.enum import PluginType
from zhenxun.models.task_info import TaskInfo
from zhenxun.utils.platform import PlatformUtils
from zhenxun.configs.utils import PluginExtraData
from zhenxun.models.bot_console import BotConsole
from zhenxun.models.plugin_info import PluginInfo
driver = nonebot.get_driver()
_sub_plugins = set()
_sub_plugins |= nonebot.load_plugins(str(Path(__file__).parent.resolve()))
@ -17,6 +24,41 @@ __plugin_meta__ = PluginMetadata(
extra=PluginExtraData(
author="",
version="0.1",
plugin_type=PluginType.SUPERUSER,
plugin_type=PluginType.PARENT,
).dict(),
)
@driver.on_bot_connect
async def _(bot: Bot):
"""初始化Bot管理
参数:
bot: Bot
"""
plugin_list = await PluginInfo.get_plugins(
plugin_type__in=[PluginType.NORMAL, PluginType.DEPENDANT, PluginType.ADMIN]
)
available_tasks: list[str] = await TaskInfo.filter(status=True).values_list(
"module", flat=True
) # type: ignore
available_plugins = [p.module for p in plugin_list]
# for _, bot in nonebot.get_bots().items():
platform = PlatformUtils.get_platform(bot)
bot_data, is_create = await BotConsole.get_or_create(
bot_id=bot.self_id, platform=platform
)
if not is_create:
block_plugins = await bot_data.get_plugins(bot.self_id, False)
block_plugins = BotConsole._convert_module_format(block_plugins)
for module in available_plugins.copy():
if module in block_plugins:
available_plugins.remove(module)
block_tasks = await bot_data.get_tasks(bot.self_id, False)
block_tasks = BotConsole._convert_module_format(block_tasks)
for module in available_tasks.copy():
if module in block_plugins:
available_tasks.remove(module)
bot_data.available_plugins = BotConsole._convert_module_format(available_plugins)
bot_data.available_tasks = BotConsole._convert_module_format(available_tasks)
await bot_data.save(update_fields=["available_plugins", "available_tasks"])

View File

@ -50,10 +50,10 @@ class BotConsole(Model):
"""
获取bot状态
Args:
参数:
bot_id (str, optional): bot_id. Defaults to None.
Returns:
返回:
list[tuple[str, bool]] | bool: bot状态
"""
if not bot_id:
@ -82,11 +82,11 @@ class BotConsole(Model):
"""
获取bot被动技能
Args:
参数:
bot_id (str | None, optional): bot_id. Defaults to None.
status (bool | None, optional): 被动状态. Defaults to True.
Returns:
返回:
list[tuple[str, str]] | str: 被动技能
"""
if not bot_id:
@ -119,11 +119,11 @@ class BotConsole(Model):
"""
获取bot插件
Args:
参数:
bot_id (str | None, optional): bot_id. Defaults to None.
status (bool, optional): 插件状态. Defaults to True.
Returns:
返回:
list[tuple[str, str]] | str: 插件
"""
if not bot_id:
@ -140,7 +140,7 @@ class BotConsole(Model):
"""
设置bot状态
Args:
参数:
status (bool): 状态
bot_id (str, optional): bot_id. Defaults to None.
@ -167,10 +167,10 @@ class BotConsole(Model):
"""
`<aaa,<bbb,<ccc,` `["aaa", "bbb", "ccc"]` 之间进行相互转换
Args:
参数:
data (str | list[str]): 输入数据可能是格式化字符串或字符串列表
Returns:
返回:
str | list[str]: 根据输入类型返回转换后的数据
"""
if isinstance(data, str):
@ -189,7 +189,7 @@ class BotConsole(Model):
"""
from_field to_field 之间移动指定的 data
Args:
参数:
bot_id (str): 目标 bot ID
from_field (str): 源字段名称
to_field (str): 目标字段名称
@ -222,7 +222,7 @@ class BotConsole(Model):
"""
禁用插件
Args:
参数:
bot_id (str | None): bot_id
plugin_name (str): 插件名称
"""
@ -248,7 +248,7 @@ class BotConsole(Model):
"""
启用插件
Args:
参数:
bot_id (str | None): bot_id
plugin_name (str): 插件名称
"""
@ -274,7 +274,7 @@ class BotConsole(Model):
"""
禁用被动技能
Args:
参数:
bot_id (str | None): bot_id
task_name (str): 被动技能名称
"""
@ -300,7 +300,7 @@ class BotConsole(Model):
"""
启用被动技能
Args:
参数:
bot_id (str | None): bot_id
task_name (str): 被动技能名称
"""
@ -330,7 +330,7 @@ class BotConsole(Model):
"""
禁用全部插件或被动技能
Args:
参数:
bot_id (str): bot_id
feat (Literal["plugins", "tasks"]): 插件或被动技能
"""
@ -352,7 +352,7 @@ class BotConsole(Model):
"""
启用全部插件或被动技能
Args:
参数:
bot_id (str): bot_id
feat (Literal["plugins", "tasks"]): 插件或被动技能
"""
@ -370,11 +370,11 @@ class BotConsole(Model):
"""
检查插件是否被禁用
Args:
参数:
bot_id (str): bot_id
plugin_name (str): 插件名称
Returns:
返回:
bool: 是否被禁用
"""
bot_data, _ = await cls.get_or_create(bot_id=bot_id)
@ -385,11 +385,11 @@ class BotConsole(Model):
"""
检查被动技能是否被禁用
Args:
参数:
bot_id (str): bot_id
task_name (str): 被动技能名称
Returns:
返回:
bool: 是否被禁用
"""
bot_data, _ = await cls.get_or_create(bot_id=bot_id)