美腿功能
This commit is contained in:
82
beautyleg/beauty_leg.py
Normal file
82
beautyleg/beauty_leg.py
Normal file
@@ -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
|
||||
3
beautyleg/config.toml
Normal file
3
beautyleg/config.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[Beautyleg]
|
||||
enable = true
|
||||
command = ["美腿", "腿来"]
|
||||
Reference in New Issue
Block a user