测试auth动态化

This commit is contained in:
liuwei
2025-06-09 15:10:54 +08:00
parent 1fcabfd365
commit aaaf555156
10 changed files with 62 additions and 39 deletions

View File

@@ -284,7 +284,7 @@ class GameTaskPlugin(MessagePluginInterface):
sender
)
@points_reward_decorator(calculate_game_points, "game", "百科答题奖励", Feature.TASK_GAME)
@points_reward_decorator(calculate_game_points, "game", "百科答题奖励", FEATURE_KEY)
async def _handle_submit_answer(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理提交答案请求"""
try:

View File

@@ -259,7 +259,7 @@ class GuessSongPlugin(MessagePluginInterface):
await bot.send_text_message(session_id, f"❌开始猜歌游戏出错,请稍后重试", sender)
return False, f"处理出错: {e}"
@points_reward_decorator(5, "game", "猜歌名游戏", Feature.GUESS_MUSIC)
@points_reward_decorator(5, "game", "猜歌名游戏", FEATURE_KEY)
async def _check_answer(self, message: Dict[str, Any]) -> Tuple[bool, str]:
"""检查答案"""
try:

View File

@@ -154,7 +154,7 @@ class JDTokenPlugin(MessagePluginInterface):
# 功能权限常量
FEATURE_KEY = "JD_TOKEN"
FEATURE_DESCRIPTION = "🔑 京东签到Token设置 [设置京东]"
FEATURE_DESCRIPTION = "JD_京豆token设置 [设置京东 pt_key=xxx;pt_pin=xxx; 备注名称]"
@property
def name(self) -> str:

View File

@@ -1,26 +1,29 @@
import os
import random
from datetime import datetime, timedelta
from loguru import logger
import pytz
from typing import Dict, Any, List, Optional, Tuple
from db.connection import DBConnectionManager
import pytz
from loguru import logger
from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.revoke.message_auto_revoke import MessageAutoRevoke
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from db.connection import DBConnectionManager
from db.sign_in import SignInDB
from db.sign_in_redis import SignInRedisDB
import random
import os
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.decorator.points_decorator import points_reward_decorator
from utils.revoke.message_auto_revoke import MessageAutoRevoke
from utils.robot_cmd.robot_command import PermissionStatus, GroupBotManager
from wechat_ipad import WechatAPIClient
class MessageSignPlugin(MessagePluginInterface):
"""签到插件"""
# 功能权限常量
FEATURE_KEY = "SIGN_IN"
FEATURE_DESCRIPTION = "✅ 签到功能 [签到, 每日签到, qd, Qd, QD, 上班, 牛马]"
@property
def name(self) -> str:
return "签到系统"
@@ -207,7 +210,7 @@ class MessageSignPlugin(MessagePluginInterface):
self.bot = message.get("bot")
# 检查权限
if roomid and gbm.get_group_permission(roomid, Feature.SIGNIN) == PermissionStatus.DISABLED:
if roomid and gbm.get_group_permission(roomid, self.feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 处理补签命令
@@ -221,7 +224,7 @@ class MessageSignPlugin(MessagePluginInterface):
return False, "不支持的命令"
# 添加签到处理方法,应用积分奖励装饰器
@points_reward_decorator(calculate_sign_in_points, "checkin", "每日签到奖励", Feature.SIGNIN)
@points_reward_decorator(calculate_sign_in_points, "checkin", "每日签到奖励", FEATURE_KEY)
async def _handle_sign_in(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理签到请求"""
sender = message.get("sender")

View File

@@ -19,6 +19,10 @@ from wechat_ipad.models.appmsg_xml import MUSIC_XML
class MusicPlugin(MessagePluginInterface):
"""音乐点播插件"""
# 功能权限常量
FEATURE_KEY = "MUSIC"
FEATURE_DESCRIPTION = "🎵 点歌功能 [点歌, 音乐, 音乐点播, 点播音乐, 音乐点歌]"
@property
def name(self) -> str:
return "音乐点播"
@@ -45,6 +49,7 @@ class MusicPlugin(MessagePluginInterface):
def __init__(self):
super().__init__()
self.feature = self.register_feature()
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""
@@ -84,7 +89,7 @@ class MusicPlugin(MessagePluginInterface):
return command in self._commands
@plugin_stats_decorator(plugin_name="音乐点播")
@plugin_points_cost(2, "音乐点播消耗积分", Feature.MUSIC)
@plugin_points_cost(2, "音乐点播消耗积分", FEATURE_KEY)
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理消息"""
content = str(message.get("content", "")).strip()
@@ -102,7 +107,7 @@ class MusicPlugin(MessagePluginInterface):
return False, "命令格式错误"
# 检查权限
if roomid and gbm.get_group_permission(roomid, Feature.MUSIC) == PermissionStatus.DISABLED:
if roomid and gbm.get_group_permission(roomid, self.feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 提取歌曲名

View File

@@ -13,18 +13,6 @@ from db.connection import DBConnectionManager
class StatsCollectorPlugin(PluginInterface):
"""统计收集插件"""
# 功能权限常量
FEATURE_KEY = "STATS_COLLECTOR"
FEATURE_DESCRIPTION = "📊 指令记录功能 [自动记录指令使用情况]"
@property
def feature_key(self) -> Optional[str]:
return self.FEATURE_KEY
@property
def feature_description(self) -> Optional[str]:
return self.FEATURE_DESCRIPTION
@property
def name(self) -> str:
return "指令记录"
@@ -63,7 +51,6 @@ class StatsCollectorPlugin(PluginInterface):
self.db_manager = DBConnectionManager.get_instance()
self.stats_db = StatsDBOperator(self.db_manager)
# 注册功能权限
self.feature = self.register_feature()
def initialize(self, config: Dict[str, Any]) -> bool:
"""初始化插件"""

View File

@@ -57,6 +57,7 @@ class VideoPlugin(MessagePluginInterface):
def __init__(self):
super().__init__()
self.bot: WechatAPIClient = None
self.feature = self.register_feature()
# 使用Path对象处理路径自动适应不同操作系统
self.download_dir = str(Path(Path(__file__).parent, "down_load_dir"))
@@ -112,7 +113,7 @@ class VideoPlugin(MessagePluginInterface):
return command in self._commands
@plugin_stats_decorator(plugin_name="视频插件")
@plugin_points_cost(2, "视频插件消耗积分", Feature.VIDEO)
@plugin_points_cost(2, "视频插件消耗积分", FEATURE_KEY)
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理消息"""
content = str(message.get("content", "")).strip()
@@ -123,7 +124,7 @@ class VideoPlugin(MessagePluginInterface):
self.bot: WechatAPIClient = message.get("bot")
# 检查权限
if roomid and gbm.get_group_permission(roomid, Feature.VIDEO) == PermissionStatus.DISABLED:
if roomid and gbm.get_group_permission(roomid, self.feature) == PermissionStatus.DISABLED:
return False, "没有权限"
try:

View File

@@ -13,7 +13,7 @@ from base.plugin_common.plugin_interface import PluginStatus
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.decorator.points_decorator import plugin_points_cost
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from base.wechat_api.wechat_api_client import WechatAPIClient
from wechat_ipad import WechatAPIClient
class VideoManPlugin(MessagePluginInterface):
@@ -21,7 +21,7 @@ class VideoManPlugin(MessagePluginInterface):
# 功能权限常量
FEATURE_KEY = "VIDEO_MAN"
FEATURE_DESCRIPTION = "💪 猛男视频功能 [猛男视频, 猛男, 来个猛男, 搞个猛男]"
FEATURE_DESCRIPTION = "💪 肌肉视频 [猛男, 肌肉, 帅哥]"
@property
def name(self) -> str:

View File

@@ -13,14 +13,14 @@ from wechat_ipad import WechatAPIClient
def points_reward_decorator(points_calculator: Union[int, Callable], source_type: str = "other",
description: str = None, feature: Feature = None):
description: str = None, feature_key: str = None):
"""积分奖励装饰器
Args:
points_calculator: 积分数量或计算函数,如果是函数,接收(self, message, success, response)参数并返回积分数量
source_type: 积分来源类型 (checkin, game, other)
description: 积分奖励描述
feature: 功能权限枚举
feature_key: 功能权限键名
Returns:
装饰器函数
@@ -34,8 +34,10 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
bot: WechatAPIClient = message.get("bot")
revoke: MessageAutoRevoke = message.get("revoke")
if feature and roomid:
if GroupBotManager.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
if feature_key and roomid:
# 获取功能权限枚举
feature = getattr(Feature, feature_key, None)
if feature and GroupBotManager.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 调用原始异步方法
@@ -106,8 +108,10 @@ def points_reward_decorator(points_calculator: Union[int, Callable], source_type
# 检查权限
roomid = message.get("roomid", "")
if feature and roomid:
if GroupBotManager.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
if feature_key and roomid:
# 获取功能权限枚举
feature = getattr(Feature, feature_key, None)
if feature and GroupBotManager.get_group_permission(roomid, feature) == PermissionStatus.DISABLED:
return False, "没有权限"
# 调用原始同步方法

View File

@@ -27,6 +27,29 @@ class PermissionStatus(Enum):
class Feature(Enum):
"""功能权限枚举,带序号"""
ROBOT = 1, "🔧 群机器人 [总开关]"
# DAILY_NEWS = 2, "📰 每日新闻自动播报 [每日8:30定时发送]"
# DAILY_SUMMARY = 3, "🕤 每日群发言总结 [每日9:30定时发送]"
# AI_CAPABILITY = 4, "🤖 AI对话 [ai, 聊天, AI] 用法ai 如何写一个机器人?"
# SUMMARY_CAPABILITY = 5, "📝 群总结能力 [#总结]"
# PDF_CAPABILITY = 6, "📄 sehuatang PDF能力 [无]"
# EPIC = 7, "📊 EPIC自动播报 [每周五自动发送]" # 新增的功能
# PIC = 8, "🖼️ 图来能力 [图来, 秀人]"
# TASK_GAME = 9, "📚 百科答题 [/t, /s, /a 任务ID 答案]"
# MUSIC = 10, "🎵 点歌功能 [点歌, 音乐, 音乐点播, 点播音乐, 音乐点歌]"
# SIGNIN = 11, "✅ 签到功能 [签到, 每日签到, qd, Qd, QD, 上班, 牛马]"
# POINT_TRADE = 12, "🎁 积分赠送 [积分赠送 1 @XX, 积分排行, 打劫 @XX, 保释 @XX]"
# BEAUTY_LEG = 13, "🦵 腿来能力 [美腿, 腿来]"
# VIDEO = 14, "🎥 黑丝视频 [黑丝视频, 黑丝, 来个黑丝, 搞个黑丝]"
# VIDEO_MAN = 15, "💪 肌肉视频 [猛男, 肌肉, 帅哥]"
# # GROUP_ADD = 16, "加群提醒"
# DOUYIN_PARSER = 17, "🎥 抖音链接转视频"
# GROUP_MEMBER_CHANGE = 18, "👥 群成员变更提醒 [自动触发]"
# # KID_PHOTO_EXTRACT = 19, "儿童照片提取转发功能" # 小朋友照片提取功能
# NEWS = 20, "🌍 全球政治经济新闻"
# WEATHER = 21, "🌤️ 天气查询 [上海天气, 天气上海]"
# JD_TOKEN = 22, "🔑 JD_京豆token设置 [设置京东 pt_key=xxx;pt_pin=xxx; 备注名称]"
# AI_AUTO = 23, "💬 仿真对话"
# GUESS_MUSIC = 24, "🎤 猜歌名游戏 [猜歌名 - 开始 | 猜歌名 歌手名 - 指定歌手 | 猜歌名 歌名 - 提交答案]"
def __new__(cls, value, description):
obj = object.__new__(cls)