From 8adeb4fb675ca87109d37fa7bc7c377de7d2b134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=85=E8=AF=BA=E7=8B=90?= <212194964+foxcyber907@users.noreply.github.com> Date: Sun, 24 Aug 2025 22:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + plugins/permission_example/_manifest.json | 44 +++++++++ plugins/permission_example/plugin.py | 108 ++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 plugins/permission_example/_manifest.json create mode 100644 plugins/permission_example/plugin.py diff --git a/.gitignore b/.gitignore index c0db9b4c5..4be4f18df 100644 --- a/.gitignore +++ b/.gitignore @@ -323,6 +323,7 @@ src/plugins/test_plugin_pic/actions/pic_action_config.toml run_pet.bat /plugins/* !/plugins/set_emoji_like +!/plugins/permission_example !/plugins/hello_world_plugin !/plugins/take_picture_plugin diff --git a/plugins/permission_example/_manifest.json b/plugins/permission_example/_manifest.json new file mode 100644 index 000000000..444ff80d0 --- /dev/null +++ b/plugins/permission_example/_manifest.json @@ -0,0 +1,44 @@ +{ + "manifest_version": 1, + "name": "权限示例插件 (Permission Example Plugin)", + "version": "1.0.0", + "description": "MaiCore权限系统演示插件,包含权限节点注册、权限检查和多种权限命令示例。", + "author": { + "name": "MaiBot开发团队", + "url": "https://github.com/MaiM-with-u" + }, + "license": "GPL-v3.0-or-later", + "host_application": { + "min_version": "0.8.0" + }, + "homepage_url": "https://github.com/MaiM-with-u/maibot", + "repository_url": "https://github.com/MaiM-with-u/maibot", + "keywords": ["permission", "example", "权限", "admin", "user", "master", "demo", "tutorial"], + "categories": ["Examples", "Tutorial", "Permission"], + "default_locale": "zh-CN", + "locales_path": "_locales", + "plugin_info": { + "is_built_in": false, + "plugin_type": "example", + "components": [ + { + "type": "command", + "name": "admin_example", + "description": "管理员权限示例命令", + "pattern": "/admin_example" + }, + { + "type": "command", + "name": "user_example", + "description": "用户权限示例命令", + "pattern": "/user_example" + }, + { + "type": "command", + "name": "master_example", + "description": "Master专用示例命令", + "pattern": "/master_example" + } + ] + } +} diff --git a/plugins/permission_example/plugin.py b/plugins/permission_example/plugin.py new file mode 100644 index 000000000..a8dc88330 --- /dev/null +++ b/plugins/permission_example/plugin.py @@ -0,0 +1,108 @@ +""" +权限系统示例插件 + +演示如何在插件中使用权限系统,包括权限节点注册、权限检查等功能。 +""" + +from typing import List + +from src.plugin_system.apis.plugin_register_api import register_plugin +from src.plugin_system.base.base_plugin import BasePlugin +from src.plugin_system.base.base_command import BaseCommand +from src.plugin_system.apis.permission_api import permission_api +from src.plugin_system.apis.logging_api import get_logger +from src.plugin_system.base.config_types import ConfigField +from src.plugin_system.utils.permission_decorators import require_permission, require_master, PermissionChecker +from src.common.message import ChatStream, Message + + +logger = get_logger(__name__) + + +class ExampleAdminCommand(BaseCommand): + """需要管理员权限的示例命令""" + + + command_name = "admin_example" + command_description = "管理员权限示例命令" + command_pattern = r"^/admin_example$" + command_help = "管理员权限示例命令" + command_examples = ["/admin_example"] + intercept_message = True + + def can_execute(self, message: Message, chat_stream: ChatStream) -> bool: + """基本检查""" + return True + + @require_permission("plugin.example.admin") + async def execute(self, message: Message, chat_stream: ChatStream, args: List[str]) -> None: + """执行管理员命令""" + await self.send_text("✅ 你有管理员权限!这是一个管理员专用功能。") + return True, "执行成功", True + + +class ExampleUserCommand(BaseCommand): + """普通用户权限的示例命令""" + command_name = "user_example" + command_description = "用户权限示例命令" + command_pattern = r"^/user_example$" + command_help = "用户权限示例命令" + command_examples = ["/user_example"] + intercept_message = True + + def can_execute(self, message: Message, chat_stream: ChatStream) -> bool: + """基本检查""" + return True + + @require_permission("plugin.example.user") + async def execute(self, message: Message, chat_stream: ChatStream, args: List[str]) -> None: + """执行用户命令""" + await self.send_text("✅ 你有用户权限!这是一个普通用户功能。") + + +class ExampleMasterCommand(BaseCommand): + """Master专用的示例命令""" + + command_name = "master_example" + command_description = "Master专用示例命令" + command_pattern = r"^/master_example$" + command_help = "Master专用示例命令" + command_examples = ["/master_example"] + intercept_message = True + + def can_execute(self, message: Message, chat_stream: ChatStream) -> bool: + """基本检查""" + return True + + @require_master() + async def execute(self, message: Message, chat_stream: ChatStream, args: List[str]) -> None: + """执行Master命令""" + await self.send_text("👑 你是Master用户!这是Master专用功能。") + +@register_plugin +class HelloWorldPlugin(BasePlugin): + """权限系统示例插件""" + + # 插件基本信息 + plugin_name: str = "permission_example" # 内部标识符 + enable_plugin: bool = True + dependencies: List[str] = [] # 插件依赖列表 + python_dependencies: List[str] = [] # Python包依赖列表 + + config_file_name: str = "config.toml" # 配置文件名 + + + # 配置Schema定义 + config_schema: dict = { + "plugin": { + "name": ConfigField(type=str, default="permission_example", description="插件名称"), + "version": ConfigField(type=str, default="1.0.0", description="插件版本"), + "enabled": ConfigField(type=bool, default=False, description="是否启用插件"), + } + } + + def get_plugin_components(self): + return [(ExampleAdminCommand.get_command_info,ExampleAdminCommand), + (ExampleUserCommand.get_command_info,ExampleUserCommand), + (ExampleMasterCommand.get_command_info,ExampleMasterCommand) + ] \ No newline at end of file