加入了级别功能。
This commit is contained in:
@@ -8,6 +8,7 @@ from typing import Callable, Dict, Any, Tuple
|
||||
|
||||
from db.stats_db import StatsDBOperator
|
||||
from db.connection import DBConnectionManager
|
||||
from db.levels_db import LevelsDBOperator
|
||||
|
||||
|
||||
def plugin_stats_decorator(plugin_name: str) -> Callable:
|
||||
@@ -68,6 +69,9 @@ def plugin_stats_decorator(plugin_name: str) -> Callable:
|
||||
process_time_ms=process_time_ms
|
||||
)
|
||||
logger.info(f"[{plugin_name}] 成功记录插件调用: {command}, 耗时: {process_time_ms:.2f}ms")
|
||||
if success and sender:
|
||||
levels_db = LevelsDBOperator(db_manager)
|
||||
levels_db.add_exp(sender, roomid or sender, 2, "plugin_call")
|
||||
|
||||
# 定义不需要记录错误的正常业务状态
|
||||
normal_responses = {
|
||||
@@ -95,44 +99,35 @@ def plugin_stats_decorator(plugin_name: str) -> Callable:
|
||||
|
||||
return success, response
|
||||
except Exception as e:
|
||||
# 计算执行时间(毫秒)
|
||||
end_time = time.time()
|
||||
process_time_ms = (end_time - start_time) * 1000
|
||||
|
||||
# 记录错误
|
||||
error_message = str(e)
|
||||
stack_trace = traceback.format_exc()
|
||||
logger.error(f"[{plugin_name}] 执行出错: {error_message}")
|
||||
logger.debug(f"[{plugin_name}] 错误堆栈: {stack_trace}")
|
||||
|
||||
try:
|
||||
# 记录插件调用(失败)
|
||||
logger.debug(f"[{plugin_name}] 记录插件调用失败统计")
|
||||
stats_db.record_plugin_call(
|
||||
plugin_name=plugin_name,
|
||||
command=command, # 使用提取的指令而不是完整内容
|
||||
command=command,
|
||||
user_id=sender,
|
||||
group_id=roomid,
|
||||
success=False,
|
||||
process_time_ms=process_time_ms
|
||||
)
|
||||
|
||||
# 记录错误详情
|
||||
logger.debug(f"[{plugin_name}] 记录错误详情")
|
||||
stats_db.record_error(
|
||||
plugin_name=plugin_name,
|
||||
command=command, # 使用提取的指令而不是完整内容
|
||||
command=command,
|
||||
user_id=sender,
|
||||
group_id=roomid,
|
||||
error_message=error_message[:500] if error_message else "未知错误", # 限制长度并确保不为空
|
||||
stack_trace=stack_trace[:2000] if stack_trace else "无堆栈信息" # 限制长度并确保不为空
|
||||
error_message=error_message[:500] if error_message else "未知错误",
|
||||
stack_trace=stack_trace[:2000] if stack_trace else "无堆栈信息"
|
||||
)
|
||||
logger.info(f"[{plugin_name}] 成功记录插件错误: {command}, 错误: {error_message}")
|
||||
except Exception as db_error:
|
||||
logger.error(f"[{plugin_name}] 记录插件统计数据失败: {db_error}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
# 重新抛出异常,让上层处理
|
||||
raise
|
||||
except Exception as outer_error:
|
||||
logger.error(f"[{plugin_name}] 装饰器外层错误: {outer_error}")
|
||||
|
||||
@@ -7,6 +7,7 @@ from typing import Callable, Dict, Any, Tuple, Union
|
||||
|
||||
from db.connection import DBConnectionManager
|
||||
from db.points_db import PointsDBOperator, PointSource
|
||||
from db.levels_db import LevelsDBOperator
|
||||
from utils.revoke.message_auto_revoke import MessageAutoRevoke
|
||||
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||
from wechat_ipad import WechatAPIClient
|
||||
@@ -86,10 +87,15 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
|
||||
logger.info(f"PointsReward.{self.name if hasattr(self, 'name') else 'Unknown'}")
|
||||
if reward_success:
|
||||
logger.info(f"用户 {sender} 获得 {points} 积分奖励")
|
||||
levels_db = LevelsDBOperator(db_manager)
|
||||
ok, lvl = levels_db.add_exp(sender, roomid, points, source.value)
|
||||
|
||||
# 如果响应中没有提到积分,添加积分信息
|
||||
if "积分" not in response:
|
||||
response += f"\n🎁 恭喜获得 {points} 积分奖励!"
|
||||
if ok and isinstance(lvl, dict) and "level" in lvl and "exp" in lvl:
|
||||
title = levels_db.level_title(int(lvl['level']))
|
||||
response += f"\n🔰 当前等级: {lvl['level']}({title}) 经验: {lvl['exp']}"
|
||||
client_msg_id, create_time, new_msg_id = await bot.send_at_message(
|
||||
(roomid if roomid else sender),
|
||||
response, [sender]
|
||||
@@ -160,10 +166,15 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
|
||||
logger.info(f"PointsReward.{self.name if hasattr(self, 'name') else 'Unknown'}")
|
||||
if reward_success:
|
||||
logger.info(f"用户 {sender} 获得 {points} 积分奖励")
|
||||
levels_db = LevelsDBOperator(db_manager)
|
||||
ok, lvl = levels_db.add_exp(sender, roomid, points, source.value)
|
||||
|
||||
# 如果响应中没有提到积分,添加积分信息
|
||||
if "积分" not in response:
|
||||
response += f"\n🎁 恭喜获得 {points} 积分奖励!"
|
||||
if ok and isinstance(lvl, dict) and "level" in lvl and "exp" in lvl:
|
||||
title = levels_db.level_title(int(lvl['level']))
|
||||
response += f"\n🔰 当前等级: {lvl['level']}({title}) 经验: {lvl['exp']}"
|
||||
else:
|
||||
logger.warning(f"用户 {sender} 积分奖励失败: {reward_result}")
|
||||
except Exception as e:
|
||||
|
||||
@@ -6,6 +6,7 @@ import concurrent.futures # 添加线程池支持
|
||||
import os
|
||||
|
||||
from db.connection import DBConnectionManager
|
||||
from db.levels_db import LevelsDBOperator
|
||||
from db.message_storage import MessageStorageDB
|
||||
# 导入积分系统
|
||||
from db.points_db import PointsDBOperator, PointSource
|
||||
@@ -237,6 +238,13 @@ class MessageStorage:
|
||||
logging.info(f"成功写入发言统计: {group_id}, {wx_id}, {yesterday}, {count}")
|
||||
else:
|
||||
logging.error(f"写入发言统计失败: {group_id}, {wx_id}, {yesterday}, {count}")
|
||||
try:
|
||||
levels_db = LevelsDBOperator(self.db_manager)
|
||||
delta = int(0.5 * min(count, 10))
|
||||
if delta > 0:
|
||||
levels_db.add_exp(wx_id, group_id, delta, "speech_count")
|
||||
except Exception as e2:
|
||||
logging.error(f"写入等级经验失败: {group_id}, {wx_id}, {yesterday}, {count} - {e2}")
|
||||
except Exception as e:
|
||||
logging.error(f"写入发言统计出错: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user