93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""
|
|
复读姬插件
|
|
|
|
检测群聊中连续相同的消息,达到指定次数后自动复读
|
|
"""
|
|
|
|
import tomllib
|
|
from pathlib import Path
|
|
from loguru import logger
|
|
from typing import Dict
|
|
from utils.plugin_base import PluginBase
|
|
from utils.decorators import on_text_message
|
|
|
|
|
|
class Repeater(PluginBase):
|
|
"""复读姬插件"""
|
|
|
|
description = "检测群聊连续相同消息并自动复读"
|
|
author = "Assistant"
|
|
version = "1.0.0"
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config = None
|
|
self.group_messages: Dict[str, Dict] = {} # {group_id: {"content": str, "count": int}}
|
|
|
|
async def async_init(self):
|
|
"""异步初始化"""
|
|
config_path = Path(__file__).parent / "config.toml"
|
|
with open(config_path, "rb") as f:
|
|
self.config = tomllib.load(f)
|
|
logger.success("复读姬插件已加载")
|
|
|
|
@on_text_message(priority=40)
|
|
async def handle_text(self, bot, message: dict):
|
|
"""处理文本消息"""
|
|
content = message.get("Content", "").strip()
|
|
from_wxid = message.get("FromWxid", "")
|
|
sender_wxid = message.get("SenderWxid", "")
|
|
is_group = message.get("IsGroup", False)
|
|
|
|
# 只处理群聊消息
|
|
if not is_group:
|
|
return True
|
|
|
|
# 检查是否启用
|
|
if not self.config["behavior"]["enabled"]:
|
|
return True
|
|
|
|
# 检查群聊过滤
|
|
enabled_groups = self.config["behavior"]["enabled_groups"]
|
|
disabled_groups = self.config["behavior"]["disabled_groups"]
|
|
|
|
if from_wxid in disabled_groups:
|
|
return True
|
|
if enabled_groups and from_wxid not in enabled_groups:
|
|
return True
|
|
|
|
# 忽略空消息
|
|
if not content:
|
|
return True
|
|
|
|
# 获取触发次数
|
|
repeat_count = self.config["behavior"]["repeat_count"]
|
|
|
|
# 检查是否是机器人自己的消息(避免无限循环)
|
|
bot_wxid = getattr(bot, 'wxid', None)
|
|
if sender_wxid == bot_wxid:
|
|
return True
|
|
|
|
# 获取该群的消息记录
|
|
if from_wxid not in self.group_messages:
|
|
self.group_messages[from_wxid] = {"content": content, "count": 1}
|
|
return True
|
|
|
|
group_data = self.group_messages[from_wxid]
|
|
|
|
# 如果消息相同,计数+1
|
|
if group_data["content"] == content:
|
|
group_data["count"] += 1
|
|
|
|
# 达到触发次数,复读
|
|
if group_data["count"] == repeat_count:
|
|
await bot.send_text(from_wxid, content)
|
|
logger.info(f"复读消息: {from_wxid} - {content[:20]}...")
|
|
# 重置计数,避免重复复读
|
|
group_data["count"] = 0
|
|
else:
|
|
# 消息不同,重置记录
|
|
self.group_messages[from_wxid] = {"content": content, "count": 1}
|
|
|
|
return True
|