From 55053199185b1bbe9704ca1c1a3680129b40ec7c Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 10 Mar 2025 09:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E8=85=BF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beautyleg/beauty_leg.py | 82 ++++++++++++++++++++++++++++++++++++++ beautyleg/config.toml | 3 ++ robot.py | 9 +++++ robot_cmd/robot_command.py | 1 + 4 files changed, 95 insertions(+) create mode 100644 beautyleg/beauty_leg.py create mode 100644 beautyleg/config.toml diff --git a/beautyleg/beauty_leg.py b/beautyleg/beauty_leg.py new file mode 100644 index 0000000..dbe81e4 --- /dev/null +++ b/beautyleg/beauty_leg.py @@ -0,0 +1,82 @@ +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}") + + import requests + + 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 len(command) == 1: + self.wcf.send_text(f"-----Bot-----\n❌命令格式错误!{self.command}", + (message.roomid if message.from_group() else message.sender), message.sender) + 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) + if random_file_path: + return self.wcf.send_file(os.path.abspath(random_file_path), message.roomid) + else: + return None diff --git a/beautyleg/config.toml b/beautyleg/config.toml new file mode 100644 index 0000000..1cda299 --- /dev/null +++ b/beautyleg/config.toml @@ -0,0 +1,3 @@ +[Beautyleg] +enable = true +command = ["美腿", "腿来"] diff --git a/robot.py b/robot.py index c4424fb..46a0dec 100644 --- a/robot.py +++ b/robot.py @@ -24,6 +24,7 @@ from base.func_news import News from base.func_tigerbot import TigerBot from base.func_xinghuo_web import XinghuoWeb from base.func_claude import Claude +from beautyleg.beauty_leg import BeautyLeg from configuration import Config from constants import ChatType from game_task.game_task_encyclopedia import game_process_message, setup_schedule, get_group_ids, \ @@ -90,6 +91,8 @@ class Robot(Job): self.video = BotVideo(wcf, self.gbm) # 秀人模块 self.xiuren = Xiuren(wcf, self.gbm) + # 美腿模块 + self.beautyleg = BeautyLeg(wcf, self.gbm) if ChatType.is_in_chat_types(chat_type): if chat_type == ChatType.TIGER_BOT.value and TigerBot.value_check(self.config.TIGERBOT): @@ -351,6 +354,12 @@ class Robot(Job): except Exception as e: self.LOG.error(f"xiuren.handle_message error: {e}") + # 美腿功能 + try: + self.beautyleg.handle_message(message=msg) + except Exception as e: + self.LOG.error(f"beautyleg.handle_text error: {e}") + if msg.is_at(self.wxid): # 被@ self.toAt(msg) return # 处理完群聊信息,后面就不需要处理了 diff --git a/robot_cmd/robot_command.py b/robot_cmd/robot_command.py index 3a5112c..b7431a3 100644 --- a/robot_cmd/robot_command.py +++ b/robot_cmd/robot_command.py @@ -34,6 +34,7 @@ class Feature(Enum): MUSIC = 10, "点歌功能" SIGNIN = 11, "签到功能" POINT_TRADE = 12, "积分赠送功能" + BEAUTY_LEG = 13, "腿来能力" VIDEO = 14, "视频点播功能" def __new__(cls, value, description):