加入限流策略,一个群一分钟只能获取5张图来

This commit is contained in:
liuwei
2025-06-25 16:31:36 +08:00
parent e2d0e57bbe
commit 11661906c4
2 changed files with 54 additions and 5 deletions

View File

@@ -10,6 +10,7 @@ from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.plugin_interface import PluginStatus
from plugins.xiuren_image.images_cache import ImageCacheManager
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.decorator.rate_limit_decorator import group_feature_rate_limit
from utils.revoke.message_auto_revoke import MessageAutoRevoke
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from utils.decorator.points_decorator import plugin_points_cost
@@ -95,7 +96,8 @@ class XiurenImagePlugin(MessagePluginInterface):
# 初始化图片缓存管理器
self.image_cache_manager = ImageCacheManager(self.image_folder, cache_size)
self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands},图片目录:{self.image_folder},缓存大小:{cache_size}")
self.LOG.info(
f"[{self.name}] 插件初始化完成,指令:{self._commands},图片目录:{self.image_folder},缓存大小:{cache_size}")
return True
def start(self) -> bool:
@@ -122,6 +124,7 @@ class XiurenImagePlugin(MessagePluginInterface):
@plugin_stats_decorator(plugin_name="秀人网图片")
@plugin_points_cost(5, "秀人网图片消耗积分", FEATURE_KEY)
@group_feature_rate_limit(max_per_minute=5, feature_key=FEATURE_KEY)
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理消息"""
content = str(message.get("content", "")).strip()
@@ -148,7 +151,7 @@ class XiurenImagePlugin(MessagePluginInterface):
# 发送图片
image_data = cached_image['bytes']
image_path = cached_image['path']
# 记录缓存状态
cache_count = self.image_cache_manager.get_cached_image_count()
self.LOG.info(f"从缓存获取图片成功,路径:{image_path},当前缓存数量:{cache_count}")
@@ -175,7 +178,7 @@ class XiurenImagePlugin(MessagePluginInterface):
cached_image = self.image_cache_manager.get_cached_image_bytes()
if cached_image:
return cached_image
# 如果缓存中没有,回退到原来的方式
self.LOG.warning("缓存中没有图片,回退到磁盘读取")
pic_path = self._get_random_pic()
@@ -191,9 +194,9 @@ class XiurenImagePlugin(MessagePluginInterface):
except Exception as e:
self.LOG.error(f"读取图片文件失败: {e}")
return None
return None
except Exception as e:
self.LOG.error(f"获取缓存图片失败: {e}")
return None