加入权限判断

This commit is contained in:
liuwei
2025-04-09 14:49:27 +08:00
parent df51dc268e
commit 4d434dcb57
10 changed files with 27 additions and 14 deletions

View File

@@ -10,13 +10,14 @@ from db.points_db import PointsDBOperator, PointSource
def points_reward_decorator(points_calculator: Union[int, Callable], source_type: str = "other",
description: str = None):
description: str = None, feature: Feature = None):
"""积分奖励装饰器
Args:
points_calculator: 积分数量或计算函数,如果是函数,接收(self, message, success, response)参数并返回积分数量
source_type: 积分来源类型 (checkin, game, other)
description: 积分奖励描述
feature: 功能权限枚举
Returns:
装饰器函数
@@ -25,6 +26,12 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(self, message: Dict[str, Any]) -> Tuple[bool, str]:
# 检查权限
roomid = message.get("roomid", "")
if feature and roomid and hasattr(self, 'gbm'):
if self.gbm.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 调用原始方法
success, response = func(self, message)
@@ -85,16 +92,16 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
return success, response
return wrapper
return decorator
def plugin_points_cost(points: int, description: str = None):
def plugin_points_cost(points: int, description: str = None, feature: Feature = None):
"""插件积分消费装饰器
Args:
points: 消费积分数量
description: 积分消费描述
feature: 功能权限枚举
Returns:
装饰器函数
@@ -104,6 +111,12 @@ def plugin_points_cost(points: int, description: str = None):
@functools.wraps(func)
def wrapper(self, message: Dict[str, Any]) -> Tuple[bool, str]:
try:
# 检查权限
roomid = message.get("roomid", "")
if feature and roomid and hasattr(self, 'gbm'):
if self.gbm.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 获取消息信息
sender = message.get("sender", "")
roomid = message.get("roomid", "")
@@ -160,5 +173,4 @@ def plugin_points_cost(points: int, description: str = None):
return func(self, message)
return wrapper
return decorator