调整代码
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
# 从当前包的main模块导入GroupAddPlugin类
|
||||
from .main import GroupAddPlugin
|
||||
|
||||
# 提供get_plugin函数,返回插件实例
|
||||
def get_plugin():
|
||||
"""获取插件实例"""
|
||||
return GroupAddPlugin()
|
||||
@@ -1,2 +0,0 @@
|
||||
[GroupAdd]
|
||||
enable = false
|
||||
@@ -1,125 +0,0 @@
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from utils.robot_cmd.robot_command import Feature, PermissionStatus
|
||||
|
||||
|
||||
class GroupAddPlugin(MessagePluginInterface):
|
||||
"""群成员加入欢迎插件"""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "群成员加入欢迎"
|
||||
|
||||
@property
|
||||
def version(self) -> str:
|
||||
return "1.0.0"
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return "当有新成员加入群聊时,自动发送欢迎消息"
|
||||
|
||||
@property
|
||||
def author(self) -> str:
|
||||
return "Trae AI"
|
||||
|
||||
@property
|
||||
def command_prefix(self) -> Optional[str]:
|
||||
return None # 不需要命令前缀,因为只处理系统消息
|
||||
|
||||
@property
|
||||
def commands(self) -> List[str]:
|
||||
return [] # 不支持命令
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||
"""初始化插件"""
|
||||
self.LOG = logging.getLogger(f"Plugin.{self.name}")
|
||||
self.LOG.info(f"正在初始化 {self.name} 插件...")
|
||||
|
||||
# 获取群管理器
|
||||
self.gbm = context.get("gbm")
|
||||
|
||||
# 从配置中获取启用状态
|
||||
plugin_config = self._config.get("GroupAdd", {})
|
||||
self.enable = plugin_config.get("enable", True)
|
||||
|
||||
self.LOG.info(f"[{self.name}] 插件初始化完成,启用状态: {self.enable}")
|
||||
return True
|
||||
|
||||
def start(self) -> bool:
|
||||
"""启动插件"""
|
||||
self.LOG.info(f"[{self.name}] 插件已启动")
|
||||
self.status = PluginStatus.RUNNING
|
||||
return True
|
||||
|
||||
def stop(self) -> bool:
|
||||
"""停止插件"""
|
||||
self.LOG.info(f"[{self.name}] 插件已停止")
|
||||
self.status = PluginStatus.STOPPED
|
||||
return True
|
||||
|
||||
def can_process(self, message: Dict[str, Any]) -> bool:
|
||||
"""检查是否可以处理该消息"""
|
||||
if not self.enable:
|
||||
return False
|
||||
|
||||
# 获取消息类型和群ID
|
||||
msg_type = message.get("type")
|
||||
roomid = message.get("roomid", "")
|
||||
content = message.get("content", "")
|
||||
|
||||
# 只处理群系统消息
|
||||
if msg_type != 10000 or not roomid:
|
||||
return False
|
||||
|
||||
# 检查群权限
|
||||
gbm = message.get("gbm")
|
||||
if gbm and gbm.get_group_permission(roomid, Feature.GROUP_ADD) == PermissionStatus.DISABLED:
|
||||
return False
|
||||
|
||||
# 使用正则表达式提取双引号中的内容,判断是否为加入群聊消息
|
||||
match = re.search(r'"(.*?)".*?加入群聊', content)
|
||||
if not match:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
|
||||
"""处理消息"""
|
||||
content = message.get("content", "")
|
||||
roomid = message.get("roomid", "")
|
||||
|
||||
self.LOG.info(f"插件执行: {self.name}:{content}")
|
||||
|
||||
# 提取昵称
|
||||
match = re.search(r'"(.*?)"', content)
|
||||
if not match:
|
||||
return False, "无法提取昵称"
|
||||
|
||||
# 获取当前时间
|
||||
now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# 提取昵称并发送欢迎消息
|
||||
nickname = match.group(1)
|
||||
welcome_message = f"🎉 欢迎 【{nickname}】 加入群聊👋 \n 🕒 {now_time} 🕒 !"
|
||||
|
||||
# 发送欢迎消息
|
||||
# self.me.send_text(welcome_message, roomid)
|
||||
|
||||
self.LOG.info(f"已发送欢迎消息给新成员 {nickname} 在群 {roomid}")
|
||||
return True, f"已欢迎 {nickname}"
|
||||
|
||||
def get_help(self) -> str:
|
||||
"""获取插件帮助信息"""
|
||||
return """群成员加入欢迎插件:
|
||||
功能:当有新成员加入群聊时,自动发送欢迎消息。
|
||||
无需手动触发,系统自动运行。
|
||||
可在群管理中启用或禁用此功能。"""
|
||||
@@ -129,7 +129,7 @@ class GroupMemberChangePlugin(MessagePluginInterface):
|
||||
gewe_client.client.post_link(gewe_client.client.app_id, sender,
|
||||
title=f"👏欢迎 {nickname} 加入群聊!🎉",
|
||||
description=f"⌚时间:{now}\n",
|
||||
url="",
|
||||
url="https://hot.imsyy.top/#/",
|
||||
thumb_url=profile.data[0].big_head_img_url)
|
||||
return True, "已发送进群欢迎语"
|
||||
return False, "无需执行"
|
||||
|
||||
3
robot.py
3
robot.py
@@ -39,6 +39,9 @@ class Robot(Job):
|
||||
|
||||
def __init__(self, config: Config) -> None:
|
||||
self.client = gewe_client.client
|
||||
if not self.client:
|
||||
logging.getLogger("Robot").error("gewe_client.client 不存在,Robot 初始化失败,程序退出。")
|
||||
return
|
||||
self.config = config
|
||||
self.app_id = gewe_client.app_id
|
||||
self.LOG = logging.getLogger("Robot")
|
||||
|
||||
Reference in New Issue
Block a user