134 lines
4.6 KiB
Python
134 lines
4.6 KiB
Python
"""
|
|
舔狗日记插件
|
|
|
|
用户发送"舔狗"或"舔狗日记"触发,返回随机舔狗日记
|
|
"""
|
|
|
|
import tomllib
|
|
import aiohttp
|
|
from pathlib import Path
|
|
from loguru import logger
|
|
from utils.plugin_base import PluginBase
|
|
from utils.decorators import on_text_message
|
|
from WechatHook import WechatHookClient
|
|
|
|
|
|
class DogDiary(PluginBase):
|
|
"""舔狗日记插件"""
|
|
|
|
# 插件元数据
|
|
description = "舔狗日记插件,发送舔狗日记"
|
|
author = "ShiHao"
|
|
version = "1.0.0"
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config = None
|
|
|
|
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("[DogDiary] 舔狗日记插件已加载")
|
|
|
|
@on_text_message(priority=50)
|
|
async def handle_dog_diary(self, bot: WechatHookClient, message: dict):
|
|
"""处理舔狗日记请求"""
|
|
content = message.get("Content", "").strip()
|
|
from_wxid = message.get("FromWxid", "")
|
|
is_group = message.get("IsGroup", False)
|
|
|
|
# 检查是否是触发关键词(精确匹配)
|
|
if content not in self.config["behavior"]["keywords"]:
|
|
return
|
|
|
|
# 检查群聊/私聊过滤
|
|
if is_group:
|
|
if not self.config["behavior"]["enable_group"]:
|
|
return
|
|
if not self._should_handle_group(from_wxid):
|
|
return
|
|
else:
|
|
if not self.config["behavior"]["enable_private"]:
|
|
return
|
|
|
|
logger.info(f"[DogDiary] 收到舔狗日记请求")
|
|
|
|
# 调用 API 获取舔狗日记
|
|
try:
|
|
diary_text = await self._fetch_dog_diary()
|
|
if diary_text:
|
|
await bot.send_text(from_wxid, diary_text)
|
|
logger.success(f"[DogDiary] 发送成功: {diary_text[:30]}...")
|
|
else:
|
|
await bot.send_text(from_wxid, "❌ 获取舔狗日记失败,请稍后再试")
|
|
logger.warning("[DogDiary] 获取舔狗日记失败")
|
|
except Exception as e:
|
|
logger.error(f"[DogDiary] 处理请求失败: {e}")
|
|
import traceback
|
|
logger.error(f"详细错误: {traceback.format_exc()}")
|
|
await bot.send_text(from_wxid, "❌ 获取舔狗日记失败,请稍后再试")
|
|
|
|
# 返回 False 阻止消息继续传播
|
|
return False
|
|
|
|
async def _fetch_dog_diary(self) -> str:
|
|
"""调用 API 获取舔狗日记"""
|
|
api_url = self.config["api"]["url"]
|
|
timeout = self.config["api"]["timeout"]
|
|
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(
|
|
api_url,
|
|
timeout=aiohttp.ClientTimeout(total=timeout)
|
|
) as response:
|
|
if response.status != 200:
|
|
logger.error(f"[DogDiary] API 请求失败: HTTP {response.status}")
|
|
return None
|
|
|
|
result = await response.json()
|
|
logger.debug(f"[DogDiary] API 返回: {result}")
|
|
|
|
# 检查返回状态
|
|
code = result.get("code")
|
|
if code != 200:
|
|
logger.error(f"[DogDiary] API 返回错误: {result.get('msg', '未知错误')}")
|
|
return None
|
|
|
|
# 提取舔狗日记内容
|
|
diary_text = result.get("data", "")
|
|
if not diary_text:
|
|
logger.error("[DogDiary] API 返回数据为空")
|
|
return None
|
|
|
|
return diary_text
|
|
|
|
except aiohttp.ClientError as e:
|
|
logger.error(f"[DogDiary] 网络请求失败: {e}")
|
|
return None
|
|
except Exception as e:
|
|
logger.error(f"[DogDiary] 获取舔狗日记失败: {e}")
|
|
import traceback
|
|
logger.error(f"详细错误: {traceback.format_exc()}")
|
|
return None
|
|
|
|
def _should_handle_group(self, room_wxid: str) -> bool:
|
|
"""判断是否应该在该群处理请求"""
|
|
enabled_groups = self.config["behavior"]["enabled_groups"]
|
|
disabled_groups = self.config["behavior"]["disabled_groups"]
|
|
|
|
# 如果在禁用列表中,不处理
|
|
if room_wxid in disabled_groups:
|
|
return False
|
|
|
|
# 如果启用列表为空,对所有群生效
|
|
if not enabled_groups:
|
|
return True
|
|
|
|
# 否则只对启用列表中的群生效
|
|
return room_wxid in enabled_groups
|