From 5342f9f5bd7d71b5134172cf4c51f7d7cfd908e8 Mon Sep 17 00:00:00 2001 From: K2cr2O1 <2221577113@qq.com> Date: Fri, 23 Jan 2026 16:57:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(permission=5Fmanager):=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=90=88=E5=B9=B6=E5=86=B2=E7=AA=81=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E5=92=8C=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=E8=A3=85=E9=A5=B0?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 清理代码中的合并冲突标记(HEAD和分支标记)并删除未使用的require_admin装饰器函数,以保持代码整洁 --- .gitignore | 2 +- core/managers/permission_manager.py | 17 ------- plugins/auto_approve.py | 2 +- scripts/compile_modules.py | 74 +++++++++++++++++++++++++++++ tests/test_plugin_reload_meta.py | 1 - 5 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 scripts/compile_modules.py diff --git a/.gitignore b/.gitignore index 752468c..aaa3440 100644 --- a/.gitignore +++ b/.gitignore @@ -147,4 +147,4 @@ build/ scratch_files/ /config.toml -/core/data/* \ No newline at end of file +/core/data/* diff --git a/core/managers/permission_manager.py b/core/managers/permission_manager.py index 0ae8740..2d5710c 100644 --- a/core/managers/permission_manager.py +++ b/core/managers/permission_manager.py @@ -412,11 +412,7 @@ class PermissionManager(Singleton): """ try: # 创建空的权限数据 -<<<<<<< HEAD - empty_data: Dict[str, Dict] = {"users": {}} -======= empty_data = {"users": {}} ->>>>>>> 7229017e16735aef7dfc8379e31646b7a382d135 # 原子性写入文件 temp_file = self.data_file + ".tmp" @@ -437,16 +433,3 @@ def require_admin(func): """ from functools import wraps from models.events.message import MessageEvent - from core.managers import permission_manager - - @wraps(func) - async def wrapper(event: MessageEvent, *args, **kwargs): - user_id = event.user_id - if await permission_manager.is_admin(user_id): - return await func(event, *args, **kwargs) - else: - # 假设 event 对象有 reply 方法 - if hasattr(event, "reply"): - await event.reply("抱歉,您没有权限执行此命令。") - return None - return wrapper diff --git a/plugins/auto_approve.py b/plugins/auto_approve.py index 874ca02..fa845a5 100644 --- a/plugins/auto_approve.py +++ b/plugins/auto_approve.py @@ -54,4 +54,4 @@ async def handle_group_request(bot: Bot, event: GroupRequestEvent): ) print(f"[自动同意] 已同意加入群聊 {event.group_id} (邀请人: {event.user_id})") except Exception as e: - print(f"[自动同意] 同意群聊邀请失败: {e}") \ No newline at end of file + print(f"[自动同意] 同意群聊邀请失败: {e}") diff --git a/scripts/compile_modules.py b/scripts/compile_modules.py new file mode 100644 index 0000000..bdd2f01 --- /dev/null +++ b/scripts/compile_modules.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +""" +编译模块脚本 + +这个脚本会单独编译每个Python模块,确保每个模块都在正确位置生成独立的.pyd文件。 +""" +import os +import sys +import glob +from mypyc.build import mypycify +try: + from setuptools import setup +except ImportError: + from distutils.core import setup + +def compile_module(module_path): + """ + 编译单个模块 + + Args: + module_path: 要编译的Python模块路径 + """ + print(f"\nCompiling {module_path}...") + try: + ext_modules = mypycify([module_path]) + setup(name=f'compiled_{os.path.basename(module_path).replace(".py", "")}', + ext_modules=ext_modules) + return True + except Exception as e: + print(f"Error compiling {module_path}: {e}") + return False + +def main(): + """ + 主函数 + """ + # 检查 Python 版本 + if not (sys.version_info.major == 3 and sys.version_info.minor == 14): + print("警告: 推荐使用 Python 3.14 以获得最佳性能") + print(f"当前版本: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}") + print("继续编译可能导致兼容性问题") + print() + + # 要编译的模块列表 + modules = [ + 'core/utils/json_utils.py', # JSON 处理 + 'core/utils/executor.py', # 代码执行引擎 + 'core/managers/command_manager.py', # 指令匹配和分发 + 'core/managers/permission_manager.py', # 权限管理(包含管理员管理功能) + 'core/ws.py', # WebSocket 核心 + 'core/managers/plugin_manager.py', # 插件管理器 + 'core/bot.py', # Bot 核心抽象 + 'core/config_loader.py', # 配置加载 + ] + + # 自动添加 events 模型 + event_models = glob.glob('models/events/*.py') + event_models = [m for m in event_models if not m.endswith('__init__.py')] + modules.extend(event_models) + + print(f"Found {len(modules)} modules to compile.") + + success_count = 0 + for module in modules: + if compile_module(module): + success_count += 1 + + print("\n--- Compilation Summary ---") + print(f"Total modules: {len(modules)}") + print(f"Successfully compiled: {success_count}") + print(f"Failed: {len(modules) - success_count}") + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/tests/test_plugin_reload_meta.py b/tests/test_plugin_reload_meta.py index 8eb0f81..0f889c0 100644 --- a/tests/test_plugin_reload_meta.py +++ b/tests/test_plugin_reload_meta.py @@ -1,4 +1,3 @@ - from core.managers.command_manager import CommandManager class TestPluginReloadMeta: