new file: dependence.py new file: package.py new file: packup.bat new file: packup.sh new file: requirements.txt new file: scripts/__init__.py new file: scripts/file_store_api.py new file: src/__init__.py new file: src/config.toml new file: src/modules/__init__.py new file: src/modules/plugin_modules.py new file: src/modules/user_module.py new file: src/process.py new file: test.py
55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
from abc import ABC, abstractmethod
|
||
from typing import Optional
|
||
from dataclasses import dataclass
|
||
from src.modules import user_module as usermod
|
||
import scripts.file_store_api as file_M
|
||
|
||
class MessageContext:
|
||
"""封装消息处理的上下文数据"""
|
||
def __init__(self, uid: str, gid: Optional[str], raw_message: str,id:str):
|
||
self.raw_message = raw_message
|
||
self._processed = False
|
||
self.response: Optional[str] = None
|
||
# 核心服务实例化
|
||
self.chat_manager = file_M.ChatManager()
|
||
self.user = usermod.User(user_id=uid)
|
||
self.rebot_id = id
|
||
|
||
# 动态加载数据
|
||
if gid:
|
||
self.group = usermod.Group(group_id=gid)
|
||
self.group.current_user = self.user
|
||
else:
|
||
self.group = None
|
||
|
||
|
||
@dataclass
|
||
class PluginPermission:
|
||
access_private: bool = False # 允许处理私聊消息
|
||
access_group: bool = True # 允许处理群消息
|
||
read_history: bool = False # 允许读取历史记录
|
||
|
||
from pathlib import Path
|
||
import os
|
||
|
||
class BasePlugin(ABC):
|
||
def __init__(self, ctx: MessageContext):
|
||
self.ctx = ctx
|
||
self._config_manager = file_M.ConfigManager(self._get_plugin_config_path())
|
||
|
||
@property
|
||
def config(self) -> dict:
|
||
"""直接访问插件配置的快捷方式"""
|
||
return self._config_manager.load_config(self.name)
|
||
|
||
def _get_plugin_config_path(self) -> str:
|
||
"""获取插件配置目录路径(config/插件名)"""
|
||
plugin_name = self.__class__.__name__.lower()
|
||
|
||
return str(Path("config") / plugin_name)
|
||
|
||
def save_config(self, config_dict: dict = None) -> bool:
|
||
"""快捷保存配置"""
|
||
if config_dict:
|
||
self._config_manager.update_config({"config": config_dict})
|
||
return self._config_manager.save_config() |