modified: README.md
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
This commit is contained in:
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
2
src/config.toml
Normal file
2
src/config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[test]
|
||||
message = "helloworld"
|
0
src/modules/__init__.py
Normal file
0
src/modules/__init__.py
Normal file
55
src/modules/plugin_modules.py
Normal file
55
src/modules/plugin_modules.py
Normal file
@ -0,0 +1,55 @@
|
||||
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()
|
42
src/modules/user_module.py
Normal file
42
src/modules/user_module.py
Normal file
@ -0,0 +1,42 @@
|
||||
import json
|
||||
import time
|
||||
|
||||
|
||||
|
||||
class User:
|
||||
def __init__(self, user_id):
|
||||
self.user_id = user_id
|
||||
self.nickname = "test"
|
||||
self.messages = []
|
||||
self.signal = True
|
||||
|
||||
def set_input_status(self, status):
|
||||
payload = json.dumps({
|
||||
"user_id": self.user_id,
|
||||
"event_type": status
|
||||
})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
while self.signal:
|
||||
print(f"刷新 {self.nickname} 的输入状态为: {status}")
|
||||
time.sleep(0.5)
|
||||
|
||||
def send_message(self, message):
|
||||
print(f"send message{0}".format(message))
|
||||
|
||||
class Group:
|
||||
def __init__(self, group_id,user=None,users=None):
|
||||
self.group_id = group_id
|
||||
self.current_user = user
|
||||
self.nickname = "test"
|
||||
self.users = users
|
||||
self.get_group_users()
|
||||
self.messages =[]
|
||||
|
||||
|
||||
def get_group_users(self):
|
||||
return(["user1","user2"])
|
||||
|
||||
def send_message(self,message):
|
||||
print(f"send message{0}".format(message))
|
13
src/process.py
Normal file
13
src/process.py
Normal file
@ -0,0 +1,13 @@
|
||||
import threading
|
||||
from src.modules.plugin_modules import BasePlugin, MessageContext
|
||||
import time
|
||||
|
||||
|
||||
class Hello_world(BasePlugin):
|
||||
def before_load(self):#加载消息前
|
||||
self.ctx.user.send_message(self.ctx.config.get("test").get("message"))
|
||||
def after_load(self):#加载消息后
|
||||
self.ctx.user.send_message(self.ctx.user.messages)
|
||||
def after_save(self):#保存用户消息后
|
||||
self.ctx.user.send_message("hello_world")
|
||||
return "ok"
|
Reference in New Issue
Block a user