新增webhook url参数
This commit is contained in:
@@ -33,12 +33,25 @@ class Database:
|
||||
def conn(self) -> sqlite3.Connection:
|
||||
"""获取数据库连接(懒加载)"""
|
||||
if self._conn is None:
|
||||
self._conn = sqlite3.connect(
|
||||
self.db_path,
|
||||
check_same_thread=False, # 允许多线程访问
|
||||
isolation_level=None # 自动提交
|
||||
)
|
||||
self._conn.row_factory = sqlite3.Row # 支持字典式访问
|
||||
try:
|
||||
self._conn = sqlite3.connect(
|
||||
self.db_path,
|
||||
check_same_thread=False, # 允许多线程访问
|
||||
isolation_level=None, # 自动提交
|
||||
timeout=30.0 # 增加超时时间
|
||||
)
|
||||
self._conn.row_factory = sqlite3.Row # 支持字典式访问
|
||||
|
||||
# 启用WAL模式以提高并发性能
|
||||
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||
self._conn.execute("PRAGMA synchronous=NORMAL")
|
||||
self._conn.execute("PRAGMA cache_size=1000")
|
||||
self._conn.execute("PRAGMA temp_store=MEMORY")
|
||||
|
||||
logger.info(f"数据库连接成功: {self.db_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"数据库连接失败: {e}", exc_info=True)
|
||||
raise
|
||||
return self._conn
|
||||
|
||||
def init_tables(self):
|
||||
@@ -416,12 +429,16 @@ class Database:
|
||||
是否成功
|
||||
"""
|
||||
if points <= 0:
|
||||
logger.warning(f"积分数量无效: {points}")
|
||||
return False
|
||||
|
||||
cursor = self.conn.cursor()
|
||||
current_time = int(time.time())
|
||||
|
||||
try:
|
||||
# 确保用户存在
|
||||
self.get_or_create_user(user_id)
|
||||
|
||||
# 更新用户积分
|
||||
cursor.execute("""
|
||||
INSERT INTO user_points (user_id, total_points, available_points, created_at, updated_at)
|
||||
@@ -433,17 +450,24 @@ class Database:
|
||||
updated_at = ?
|
||||
""", (user_id, points, points, current_time, current_time, points, points, current_time))
|
||||
|
||||
# 验证积分是否真的更新了
|
||||
cursor.execute("SELECT available_points FROM user_points WHERE user_id = ?", (user_id,))
|
||||
updated_points = cursor.fetchone()
|
||||
if not updated_points:
|
||||
logger.error(f"积分更新后查询失败: user_id={user_id}")
|
||||
return False
|
||||
|
||||
# 记录积分变动
|
||||
cursor.execute(
|
||||
"INSERT INTO points_records (user_id, points, source, description, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
(user_id, points, source, description, current_time)
|
||||
)
|
||||
|
||||
logger.debug(f"用户 {user_id} 获得 {points} 积分,来源:{source}")
|
||||
logger.info(f"用户 {user_id} 成功获得 {points} 积分,来源:{source},当前积分:{updated_points[0]}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"添加积分失败: {e}")
|
||||
logger.error(f"添加积分失败: user_id={user_id}, points={points}, error={e}", exc_info=True)
|
||||
return False
|
||||
|
||||
def consume_points(self, user_id: int, points: int, source: str, description: str = None) -> bool:
|
||||
@@ -554,6 +578,10 @@ class Database:
|
||||
# 确保用户存在
|
||||
self.get_or_create_user(user_id)
|
||||
|
||||
# 获取签到前积分
|
||||
points_before = self.get_user_points(user_id)
|
||||
logger.info(f"用户 {user_id} 签到前积分: {points_before['available_points']}")
|
||||
|
||||
# 记录签到
|
||||
cursor.execute(
|
||||
"INSERT INTO daily_checkins (user_id, checkin_date, points_earned, created_at) VALUES (?, ?, ?, ?)",
|
||||
@@ -562,8 +590,17 @@ class Database:
|
||||
|
||||
# 添加积分
|
||||
result = self.add_points(user_id, points, "daily_checkin", f"每日签到奖励")
|
||||
logger.info(f"用户 {user_id} 签到结果: {result}, 积分: {points}")
|
||||
return result
|
||||
|
||||
# 验证积分是否真的增加了
|
||||
points_after = self.get_user_points(user_id)
|
||||
logger.info(f"用户 {user_id} 签到后积分: {points_after['available_points']}")
|
||||
|
||||
if points_after['available_points'] > points_before['available_points']:
|
||||
logger.info(f"用户 {user_id} 签到成功,积分增加: {points_after['available_points'] - points_before['available_points']}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"用户 {user_id} 签到失败,积分未增加")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"每日签到失败: {e}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user