2024-02-25 03:18:34 +08:00
|
|
|
from nonebot import on_message
|
|
|
|
|
from nonebot.plugin import PluginMetadata
|
|
|
|
|
from nonebot_plugin_alconna import UniMsg
|
2025-07-14 22:35:29 +08:00
|
|
|
from nonebot_plugin_apscheduler import scheduler
|
|
|
|
|
from nonebot_plugin_uninfo import Uninfo
|
2024-02-25 03:18:34 +08:00
|
|
|
|
|
|
|
|
from zhenxun.configs.config import Config
|
|
|
|
|
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
|
|
|
|
from zhenxun.models.chat_history import ChatHistory
|
|
|
|
|
from zhenxun.services.log import logger
|
|
|
|
|
from zhenxun.utils.enum import PluginType
|
2025-07-14 22:35:29 +08:00
|
|
|
from zhenxun.utils.utils import get_entity_ids
|
2024-02-25 03:18:34 +08:00
|
|
|
|
|
|
|
|
__plugin_meta__ = PluginMetadata(
|
|
|
|
|
name="消息存储",
|
|
|
|
|
description="消息存储,被动存储群消息",
|
|
|
|
|
usage="",
|
|
|
|
|
extra=PluginExtraData(
|
|
|
|
|
author="HibiKier",
|
|
|
|
|
version="0.1",
|
|
|
|
|
plugin_type=PluginType.HIDDEN,
|
|
|
|
|
configs=[
|
|
|
|
|
RegisterConfig(
|
|
|
|
|
module="chat_history",
|
|
|
|
|
key="FLAG",
|
|
|
|
|
value=True,
|
|
|
|
|
help="是否开启消息自从存储",
|
|
|
|
|
default_value=True,
|
|
|
|
|
type=bool,
|
|
|
|
|
)
|
|
|
|
|
],
|
2025-01-07 14:20:30 +08:00
|
|
|
).to_dict(),
|
2024-02-25 03:18:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rule(message: UniMsg) -> bool:
|
|
|
|
|
return bool(Config.get_config("chat_history", "FLAG") and message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_history = on_message(rule=rule, priority=1, block=False)
|
|
|
|
|
|
2025-07-14 22:35:29 +08:00
|
|
|
TEMP_LIST = []
|
|
|
|
|
|
2024-02-25 03:18:34 +08:00
|
|
|
|
|
|
|
|
@chat_history.handle()
|
2025-07-14 22:35:29 +08:00
|
|
|
async def _(message: UniMsg, session: Uninfo):
|
|
|
|
|
entity = get_entity_ids(session)
|
|
|
|
|
TEMP_LIST.append(
|
|
|
|
|
ChatHistory(
|
|
|
|
|
user_id=entity.user_id,
|
|
|
|
|
group_id=entity.group_id,
|
2024-02-25 03:18:34 +08:00
|
|
|
text=str(message),
|
|
|
|
|
plain_text=message.extract_plain_text(),
|
2025-07-14 22:35:29 +08:00
|
|
|
bot_id=session.self_id,
|
2024-02-25 03:18:34 +08:00
|
|
|
platform=session.platform,
|
|
|
|
|
)
|
2025-07-14 22:35:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@scheduler.scheduled_job(
|
|
|
|
|
"interval",
|
|
|
|
|
minutes=1,
|
|
|
|
|
)
|
|
|
|
|
async def _():
|
|
|
|
|
try:
|
|
|
|
|
message_list = TEMP_LIST.copy()
|
|
|
|
|
TEMP_LIST.clear()
|
|
|
|
|
if message_list:
|
|
|
|
|
await ChatHistory.bulk_create(message_list)
|
|
|
|
|
logger.debug(f"批量添加聊天记录 {len(message_list)} 条", "定时任务")
|
2024-02-25 03:18:34 +08:00
|
|
|
except Exception as e:
|
2025-04-04 20:24:12 +08:00
|
|
|
logger.warning("存储聊天记录失败", "chat_history", e=e)
|