mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🐛 Refactor chat message handling to improve message storage and error logging
This commit is contained in:
parent
2007530a03
commit
d62670d353
@ -1,7 +1,6 @@
|
|||||||
from nonebot import on_message
|
from nonebot import on_message
|
||||||
from nonebot.plugin import PluginMetadata
|
from nonebot.plugin import PluginMetadata
|
||||||
from nonebot_plugin_alconna import UniMsg
|
from nonebot_plugin_alconna import UniMsg
|
||||||
from nonebot_plugin_apscheduler import scheduler
|
|
||||||
from nonebot_plugin_session import EventSession
|
from nonebot_plugin_session import EventSession
|
||||||
|
|
||||||
from zhenxun.configs.config import Config
|
from zhenxun.configs.config import Config
|
||||||
@ -39,45 +38,17 @@ def rule(message: UniMsg) -> bool:
|
|||||||
chat_history = on_message(rule=rule, priority=1, block=False)
|
chat_history = on_message(rule=rule, priority=1, block=False)
|
||||||
|
|
||||||
|
|
||||||
TEMP_LIST = []
|
|
||||||
|
|
||||||
|
|
||||||
@chat_history.handle()
|
@chat_history.handle()
|
||||||
async def _(message: UniMsg, session: EventSession):
|
async def handle_message(message: UniMsg, session: EventSession):
|
||||||
# group_id = session.id3 or session.id2
|
"""处理消息存储"""
|
||||||
group_id = session.id2
|
try:
|
||||||
TEMP_LIST.append(
|
await ChatHistory.create(
|
||||||
ChatHistory(
|
|
||||||
user_id=session.id1,
|
user_id=session.id1,
|
||||||
group_id=group_id,
|
group_id=session.id2,
|
||||||
text=str(message),
|
text=str(message),
|
||||||
plain_text=message.extract_plain_text(),
|
plain_text=message.extract_plain_text(),
|
||||||
bot_id=session.bot_id,
|
bot_id=session.bot_id,
|
||||||
platform=session.platform,
|
platform=session.platform,
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@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)} 条", "定时任务")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("定时批量添加聊天记录", "定时任务", e=e)
|
logger.warning("存储聊天记录失败", "chat_history", e=e)
|
||||||
|
|
||||||
|
|
||||||
# @test.handle()
|
|
||||||
# async def _(event: MessageEvent):
|
|
||||||
# print(await ChatHistory.get_user_msg(event.user_id, "private"))
|
|
||||||
# print(await ChatHistory.get_user_msg_count(event.user_id, "private"))
|
|
||||||
# print(await ChatHistory.get_user_msg(event.user_id, "group"))
|
|
||||||
# print(await ChatHistory.get_user_msg_count(event.user_id, "group"))
|
|
||||||
# print(await ChatHistory.get_group_msg(event.group_id))
|
|
||||||
# print(await ChatHistory.get_group_msg_count(event.group_id))
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user