如果用户在群里面了,则不发送邀请

This commit is contained in:
liuwei
2025-04-15 16:56:02 +08:00
parent 89af94e29c
commit 37c98d1fff

View File

@@ -9,6 +9,7 @@ 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
class GroupAutoInvitePlugin(MessagePluginInterface):
@@ -88,11 +89,11 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
content = str(message.get("content", "")).strip()
roomid = message.get("roomid", "")
# 处理加群配置命令
if content.startswith("#加群配置|"):
return True
# 处理加群请求 - 只在私聊中处理
if re.search(r"^#加群\s+(\w+)$", content):
# 如果是在群聊中发送的加群请求,不处理
@@ -107,7 +108,7 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
"""处理消息"""
content = str(message.get("content", "")).strip()
self.LOG.info(f"插件执行: {self.name}{content}")
sender = message.get("sender")
roomid = message.get("roomid", "")
wcf: Wcf = message.get("wcf")
@@ -116,53 +117,61 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
# 处理加群配置命令
if content.startswith("#加群配置|"):
return self._handle_config_command(content, sender, roomid, wcf, gbm)
# 处理加群请求
match = re.search(r"^#加群\s+(\w+)$", content)
if match:
return self._handle_join_request(match.group(1), sender, roomid, wcf, gbm)
return False, "无法处理的消息"
def _handle_config_command(self, content: str, sender: str, roomid: str, wcf: Wcf, gbm: GroupBotManager) -> Tuple[bool, Optional[str]]:
def _handle_config_command(self, content: str, sender: str, roomid: str, wcf: Wcf, gbm: GroupBotManager) -> Tuple[
bool, Optional[str]]:
"""处理配置命令"""
# 检查是否为管理员
admin_list = self.gbm.get_admin_list()
if sender not in admin_list:
wcf.send_text("⚠️ 权限不足,只有管理员才能配置群邀请功能",
(roomid if roomid else sender), sender)
wcf.send_text("⚠️ 权限不足,只有管理员才能配置群邀请功能",
(roomid if roomid else sender), sender)
return True, "权限不足"
# 解析命令
command = content.replace("#加群配置|", "").strip()
result = self.process_command(command)
# 发送结果
wcf.send_text(result, (roomid if roomid else sender), sender)
return True, "配置命令处理成功"
def _handle_join_request(self, key: str, sender: str, roomid: str, wcf: Wcf, gbm: GroupBotManager) -> Tuple[bool, Optional[str]]:
def _handle_join_request(self, key: str, sender: str, roomid: str, wcf: Wcf, gbm: GroupBotManager) -> Tuple[
bool, Optional[str]]:
"""处理加群请求"""
try:
# 获取对应的群ID
group_id = self.get_first_group_id(key)
# 检查是否找到群ID
if isinstance(group_id, str) and "没有关联的群ID" in group_id:
wcf.send_text(f"⚠️ 未找到关键词 '{key}' 对应的群聊", sender)
return True, "未找到群聊"
# 判断是否在群里面,如果在,则不添加
con = ContactManager.get_instance()
members = con.get_group_members(group_id)
# 如果在群里面,则不添加
if sender in members:
wcf.send_text(f"⚠️ 你已经在群聊中了,无需重复添加", sender)
return True, "你已经在群聊中了"
# 发送邀请
self.LOG.info(f"邀请用户 {sender} 加入群 {group_id}")
result = wcf.invite_chatroom_members(group_id, sender)
if result:
wcf.send_text(f"✅ 已发送邀请,请查看群聊邀请通知", sender)
return True, "邀请发送成功"
else:
wcf.send_text(f"❌ 邀请发送失败,请稍后再试", sender)
return True, "邀请发送失败"
except Exception as e:
self.LOG.error(f"处理加群请求出错: {e}")
wcf.send_text(f"❌ 处理加群请求出错: {e}", sender)
@@ -261,4 +270,4 @@ class GroupAutoInvitePlugin(MessagePluginInterface):
return """自动加群功能插件:
1. 管理员可以通过 #加群配置 命令管理群聊映射
2. 用户可以通过 #加群 关键词 命令请求加入指定群聊
3. 使用 #加群配置|help 获取详细命令说明"""
3. 使用 #加群配置|help 获取详细命令说明"""