From 4cc800c832a4babe041ff60d362792f64c0db3e7 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Mon, 16 Jun 2025 09:21:41 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0bot=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8F=91=E9=80=81=E8=AE=B0=E5=BD=95=20(#1893)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: 添加bot消息发送记录 * :speech_balloon: 文本内容修正 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- zhenxun/builtin_plugins/hooks/__init__.py | 10 +++ zhenxun/builtin_plugins/hooks/call_hook.py | 84 +++++++++++++++++++--- zhenxun/models/bot_message_store.py | 29 ++++++++ zhenxun/utils/enum.py | 5 ++ 4 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 zhenxun/models/bot_message_store.py diff --git a/zhenxun/builtin_plugins/hooks/__init__.py b/zhenxun/builtin_plugins/hooks/__init__.py index 3ad29d71..2f8c79de 100644 --- a/zhenxun/builtin_plugins/hooks/__init__.py +++ b/zhenxun/builtin_plugins/hooks/__init__.py @@ -49,4 +49,14 @@ Config.add_plugin_config( type=bool, ) +Config.add_plugin_config( + "hook", + "RECORD_BOT_SENT_MESSAGES", + True, + help="记录bot消息发送", + default_value=True, + type=bool, +) + + nonebot.load_plugins(str(Path(__file__).parent.resolve())) diff --git a/zhenxun/builtin_plugins/hooks/call_hook.py b/zhenxun/builtin_plugins/hooks/call_hook.py index 2ff4d39c..1893754d 100644 --- a/zhenxun/builtin_plugins/hooks/call_hook.py +++ b/zhenxun/builtin_plugins/hooks/call_hook.py @@ -1,23 +1,85 @@ from typing import Any -from nonebot.adapters import Bot +from nonebot.adapters import Bot, Message +from zhenxun.configs.config import Config +from zhenxun.models.bot_message_store import BotMessageStore from zhenxun.services.log import logger +from zhenxun.utils.enum import BotSentType from zhenxun.utils.manager.message_manager import MessageManager +from zhenxun.utils.platform import PlatformUtils + + +def replace_message(message: Message) -> str: + """将消息中的at、image、record、face替换为字符串 + + 参数: + message: Message + + 返回: + str: 文本消息 + """ + result = "" + for msg in message: + if isinstance(msg, str): + result += msg + elif msg.type == "at": + result += f"@{msg.data['qq']}" + elif msg.type == "image": + result += "[image]" + elif msg.type == "record": + result += "[record]" + elif msg.type == "face": + result += f"[face:{msg.data['id']}]" + elif msg.type == "reply": + result += "" + else: + result += str(msg) + return result @Bot.on_called_api async def handle_api_result( bot: Bot, exception: Exception | None, api: str, data: dict[str, Any], result: Any ): - if not exception and api == "send_msg": - try: - if (uid := data.get("user_id")) and (msg_id := result.get("message_id")): - MessageManager.add(str(uid), str(msg_id)) - logger.debug( - f"收集消息id,user_id: {uid}, msg_id: {msg_id}", "msg_hook" - ) - except Exception as e: - logger.warning( - f"收集消息id发生错误...data: {data}, result: {result}", "msg_hook", e=e + if exception or api != "send_msg": + return + user_id = data.get("user_id") + group_id = data.get("group_id") + message_id = result.get("message_id") + message: Message = data.get("message", "") + message_type = data.get("message_type") + try: + # 记录消息id + if user_id and message_id: + MessageManager.add(str(user_id), str(message_id)) + logger.debug( + f"收集消息id,user_id: {user_id}, msg_id: {message_id}", "msg_hook" ) + except Exception as e: + logger.warning( + f"收集消息id发生错误...data: {data}, result: {result}", "msg_hook", e=e + ) + if not Config.get_config("hook", "RECORD_BOT_SENT_MESSAGES"): + return + try: + await BotMessageStore.create( + bot_id=bot.self_id, + user_id=user_id, + group_id=group_id, + sent_type=BotSentType.GROUP + if message_type == "group" + else BotSentType.PRIVATE, + text=replace_message(message), + plain_text=message.extract_plain_text() + if isinstance(message, Message) + else replace_message(message), + platform=PlatformUtils.get_platform(bot), + ) + logger.debug(f"消息发送记录,message: {message}") + except Exception as e: + logger.warning( + f"消息发送记录发生错误...data: {data}, result: {result}", + "msg_hook", + e=e, + ) diff --git a/zhenxun/models/bot_message_store.py b/zhenxun/models/bot_message_store.py new file mode 100644 index 00000000..fa1244f9 --- /dev/null +++ b/zhenxun/models/bot_message_store.py @@ -0,0 +1,29 @@ +from tortoise import fields + +from zhenxun.services.db_context import Model +from zhenxun.utils.enum import BotSentType + + +class BotMessageStore(Model): + id = fields.IntField(pk=True, generated=True, auto_increment=True) + """自增id""" + bot_id = fields.CharField(255, null=True) + """bot id""" + user_id = fields.CharField(255, null=True) + """目标id""" + group_id = fields.CharField(255, null=True) + """群组id""" + sent_type = fields.CharEnumField(BotSentType) + """类型""" + text = fields.TextField(null=True) + """文本内容""" + plain_text = fields.TextField(null=True) + """纯文本""" + platform = fields.CharField(255, null=True) + """平台""" + create_time = fields.DatetimeField(auto_now_add=True) + """创建时间""" + + class Meta: # pyright: ignore [reportIncompatibleVariableOverride] + table = "bot_message_store" + table_description = "Bot发送消息列表" diff --git a/zhenxun/utils/enum.py b/zhenxun/utils/enum.py index ba012617..db527fc3 100644 --- a/zhenxun/utils/enum.py +++ b/zhenxun/utils/enum.py @@ -13,6 +13,11 @@ class PriorityLifecycleType(StrEnum): """关闭""" +class BotSentType(StrEnum): + GROUP = "GROUP" + PRIVATE = "PRIVATE" + + class BankHandleType(StrEnum): DEPOSIT = "DEPOSIT" """存款"""