import logging import os import random import tomllib import requests from wcferry import WxMsg, Wcf from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager import lz4.block as lb class BeautyLeg: def __init__(self, wcf: Wcf, gbm: GroupBotManager): self.LOG = logging.getLogger(__name__) self.wcf = wcf # 假设 wcf 对象在此类中初始化 self.gbm = gbm # 权限功能 with open("beautyleg/config.toml", "rb") as f: plugin_config = tomllib.load(f) config = plugin_config["Beautyleg"] self.enable = config["enable"] self.command = config["command"] self.LOG.info(f"[美腿] 组件初始化完成,指令: {self.command}") def get_random_file_from_dir(self, directory): image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'} image_files = [] if not os.path.exists(directory): print(f"Error: Directory '{directory}' does not exist.") return None if not os.access(directory, os.R_OK): print(f"Error: No read access to directory '{directory}'.") return None print(f"Scanning directory: {directory} (including subdirectories)") # 使用 os.walk() 递归遍历所有子目录 for root, _, files in os.walk(directory): for file in files: _, ext = os.path.splitext(file) if ext.lower() in image_extensions: full_path = os.path.join(root, file) image_files.append(full_path) if not image_files: print("No image files found in the directory (including subdirectories).") return None return random.choice(image_files) def handle_message(self, message: WxMsg): if not self.enable: return content = str(message.content).strip() command = content.split(" ") if command[0] not in self.command: return # 如果触发了指令,但是没有权限,则返回权限不足 if self.gbm.get_group_permission(message.roomid, Feature.BEAUTY_LEG) == PermissionStatus.DISABLED: return # 使用示例 directory = 'beautyleg/download_dir' # 替换为你的目录路径 random_file_path = self.get_random_file_from_dir(directory) self.LOG.info(f"BeautyLeg.random_file_path: {random_file_path}") if random_file_path: return self.wcf.send_file(os.path.abspath(random_file_path), message.roomid) else: return None