855 协议版本-调整完毕内容
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
import logging
|
||||
from loguru import logger
|
||||
import redis
|
||||
import re
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
from gewechat_client import GewechatClient
|
||||
|
||||
from message_util import MessageUtil
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
||||
from utils.robot_cmd.robot_command import GroupBotManager
|
||||
from utils.wechat.contact_manager import ContactManager
|
||||
from wechat_ipad import WechatAPIClient
|
||||
|
||||
|
||||
class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
@@ -45,10 +43,10 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
# Redis 中存储群组映射的前缀
|
||||
self.mapping_prefix = "group:group_mapping:"
|
||||
self._commands = []
|
||||
self.bot: WechatAPIClient = None
|
||||
|
||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||
"""初始化插件"""
|
||||
self.LOG = logging.getLogger(f"Plugin.{self.name}")
|
||||
self.LOG.info(f"正在初始化 {self.name} 插件...")
|
||||
|
||||
# 获取群管理器
|
||||
@@ -56,8 +54,6 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
# 获取Redis连接池
|
||||
self.redis_pool = context.get("redis_pool")
|
||||
|
||||
self.clent:GewechatClient = context.get("clent")
|
||||
self.message_util: MessageUtil = context.get("message_util")
|
||||
# 从配置中获取命令和启用状态
|
||||
plugin_config = self._config.get("GroupAutoInvite", {})
|
||||
self._commands = plugin_config.get("command", ["#加群配置"])
|
||||
@@ -105,7 +101,7 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
return False
|
||||
|
||||
@plugin_stats_decorator(plugin_name="自动加群功能")
|
||||
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
|
||||
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
|
||||
"""处理消息"""
|
||||
content = str(message.get("content", "")).strip()
|
||||
self.LOG.info(f"插件执行: {self.name}:{content}")
|
||||
@@ -114,25 +110,26 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
roomid = message.get("roomid", "")
|
||||
gbm: GroupBotManager = message.get("gbm")
|
||||
|
||||
self.bot: WechatAPIClient = message.get("bot")
|
||||
# 处理加群配置命令
|
||||
if content.startswith("#加群配置|"):
|
||||
return self._handle_config_command(content, sender, roomid, gbm)
|
||||
return await self._handle_config_command(content, sender, roomid, gbm)
|
||||
|
||||
# 处理加群请求
|
||||
match = re.search(r"^#加群\s+(\w+)$", content)
|
||||
if match:
|
||||
return self._handle_join_request(match.group(1), sender, roomid, gbm)
|
||||
return await self._handle_join_request(match.group(1), sender, roomid, gbm)
|
||||
|
||||
return False, "无法处理的消息"
|
||||
|
||||
def _handle_config_command(self, content: str, sender: str, roomid: str, gbm: GroupBotManager) -> Tuple[
|
||||
async def _handle_config_command(self, content: str, sender: str, roomid: str, gbm: GroupBotManager) -> Tuple[
|
||||
bool, Optional[str]]:
|
||||
"""处理配置命令"""
|
||||
# 检查是否为管理员
|
||||
admin_list = self.gbm.get_admin_list()
|
||||
if sender not in admin_list:
|
||||
self.message_util.send_text("⚠️ 权限不足,只有管理员才能配置群邀请功能",
|
||||
(roomid if roomid else sender), sender)
|
||||
await self.bot.send_text_message((roomid if roomid else sender), "⚠️ 权限不足,只有管理员才能配置群邀请功能",
|
||||
sender)
|
||||
return True, "权限不足"
|
||||
|
||||
# 解析命令
|
||||
@@ -140,10 +137,10 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
result = self.process_command(command)
|
||||
|
||||
# 发送结果
|
||||
self.message_util.send_text(result, (roomid if roomid else sender), sender)
|
||||
await self.bot.send_text_message((roomid if roomid else sender), result, sender)
|
||||
return True, "配置命令处理成功"
|
||||
|
||||
def _handle_join_request(self, key: str, sender: str, roomid: str, gbm: GroupBotManager) -> Tuple[
|
||||
async def _handle_join_request(self, key: str, sender: str, roomid: str, gbm: GroupBotManager) -> Tuple[
|
||||
bool, Optional[str]]:
|
||||
"""处理加群请求"""
|
||||
try:
|
||||
@@ -152,29 +149,29 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
|
||||
|
||||
# 检查是否找到群ID
|
||||
if isinstance(group_id, str) and "没有关联的群ID" in group_id:
|
||||
self.message_util.send_text(f"⚠️ 未找到关键词 '{key}' 对应的群聊", sender)
|
||||
await self.bot.send_text_message(sender, f"⚠️ 未找到关键词 '{key}' 对应的群聊")
|
||||
return True, "未找到群聊"
|
||||
# 判断是否在群里面,如果在,则不添加
|
||||
con = ContactManager.get_instance()
|
||||
members = con.get_group_members(group_id)
|
||||
# 如果在群里面,则不添加
|
||||
if sender in members:
|
||||
self.message_util.send_text(f"⚠️ 你已经在群聊中了,无需重复添加", sender)
|
||||
await self.bot.send_text_message(sender, f"⚠️ 你已经在群聊中了,无需重复添加")
|
||||
return True, "你已经在群聊中了"
|
||||
# 发送邀请
|
||||
self.LOG.info(f"邀请用户 {sender} 加入群 {group_id}")
|
||||
result = self.message_util.invite_member(group_id, sender)
|
||||
result = await self.bot.invite_chatroom_member(sender, group_id)
|
||||
|
||||
if result:
|
||||
self.message_util.send_text(f"✅ 已发送邀请,请查看群聊邀请通知", sender)
|
||||
await self.bot.send_text_message(sender, f"✅ 已发送邀请,请查看群聊邀请通知")
|
||||
return True, "邀请发送成功"
|
||||
else:
|
||||
self.message_util.send_text(f"❌ 邀请发送失败,请稍后再试", sender)
|
||||
await self.bot.send_text_message(sender, f"❌ 邀请发送失败,请稍后再试")
|
||||
return False, "邀请发送失败"
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"处理加群请求出错: {e}")
|
||||
self.message_util.send_text(f"❌ 处理加群请求出错: {e}", sender)
|
||||
await self.bot.send_text_message(sender, f"❌ 处理加群请求出错: {e}")
|
||||
return False, f"处理出错: {e}"
|
||||
|
||||
def add_mapping(self, key, group_id):
|
||||
|
||||
Reference in New Issue
Block a user