1.删除原有music内容,使用插件music;
2.新建插件,秀人图功能
This commit is contained in:
143
plugins/xiuren_image/main.py
Normal file
143
plugins/xiuren_image/main.py
Normal file
@@ -0,0 +1,143 @@
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
from wcferry import Wcf
|
||||
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from plugins.stats_collector.decorators import plugin_stats_decorator
|
||||
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||
|
||||
|
||||
class XiurenImagePlugin(MessagePluginInterface):
|
||||
"""秀人图片插件"""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "秀人图片[xiuren_image]"
|
||||
|
||||
@property
|
||||
def version(self) -> str:
|
||||
return "1.0.0"
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return "提供随机秀人网图片功能"
|
||||
|
||||
@property
|
||||
def author(self) -> str:
|
||||
return "Trae AI"
|
||||
|
||||
@property
|
||||
def command_prefix(self) -> Optional[str]:
|
||||
return "" # 不需要前缀,直接匹配命令
|
||||
|
||||
@property
|
||||
def commands(self) -> List[str]:
|
||||
return self._commands
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.image_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "xiuren", "pics")
|
||||
|
||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||
"""初始化插件"""
|
||||
self.LOG = logging.getLogger(f"Plugin.{self.name}")
|
||||
self.LOG.info(f"正在初始化 {self.name} 插件...")
|
||||
|
||||
# 保存上下文对象
|
||||
self.wcf = context.get("wcf")
|
||||
self.event_system = context.get("event_system")
|
||||
self.message_util = context.get("message_util")
|
||||
|
||||
self._commands = self._config.get("XiurenImage", {}).get("command", ["图来", "秀人"])
|
||||
self.command_format = self._config.get("XiurenImage", {}).get("command-format", "图来")
|
||||
self.enable = self._config.get("XiurenImage", {}).get("enable", True)
|
||||
|
||||
# 检查图片文件夹是否存在
|
||||
if not os.path.exists(self.image_folder):
|
||||
self.LOG.warning(f"图片文件夹不存在: {self.image_folder}")
|
||||
os.makedirs(self.image_folder, exist_ok=True)
|
||||
|
||||
self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands}")
|
||||
return True
|
||||
|
||||
def start(self) -> bool:
|
||||
"""启动插件"""
|
||||
self.LOG.info(f"[{self.name}] 插件已启动")
|
||||
self.status = PluginStatus.RUNNING
|
||||
return True
|
||||
|
||||
def stop(self) -> bool:
|
||||
"""停止插件"""
|
||||
self.LOG.info(f"[{self.name}] 插件已停止")
|
||||
self.status = PluginStatus.STOPPED
|
||||
return True
|
||||
|
||||
def can_process(self, message: Dict[str, Any]) -> bool:
|
||||
"""检查是否可以处理该消息"""
|
||||
if not self.enable:
|
||||
return False
|
||||
|
||||
content = str(message.get("content", "")).strip()
|
||||
command = content.split(" ")[0]
|
||||
|
||||
return command in self._commands
|
||||
|
||||
@plugin_stats_decorator(plugin_name="秀人图片")
|
||||
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
|
||||
"""处理消息"""
|
||||
content = str(message.get("content", "")).strip()
|
||||
self.LOG.info(f"插件执行: {self.name}:{content}")
|
||||
sender = message.get("sender")
|
||||
roomid = message.get("roomid", "")
|
||||
wcf: Wcf = message.get("wcf")
|
||||
gbm: GroupBotManager = message.get("gbm")
|
||||
|
||||
# 检查权限
|
||||
if roomid and gbm.get_group_permission(roomid, Feature.PIC) == PermissionStatus.DISABLED:
|
||||
return False, "没有权限"
|
||||
|
||||
try:
|
||||
# 获取随机图片
|
||||
pic_path = self._get_random_pic()
|
||||
if not pic_path:
|
||||
wcf.send_text(f"-----Bot-----\n❌未找到图片资源",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, "未找到图片资源"
|
||||
|
||||
# 发送图片
|
||||
result = wcf.send_file(pic_path, (roomid if roomid else sender))
|
||||
self.LOG.info(f"发送图片结果: {result}")
|
||||
return True, "发送成功"
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"处理图片请求出错: {e}")
|
||||
return True, f"处理出错: {e}"
|
||||
|
||||
def _get_random_pic(self) -> Optional[str]:
|
||||
"""获取随机图片路径"""
|
||||
try:
|
||||
# 获取图片文件夹中的所有图片
|
||||
if not os.path.exists(self.image_folder):
|
||||
self.LOG.error(f"图片文件夹不存在: {self.image_folder}")
|
||||
return None
|
||||
|
||||
image_files = [f for f in os.listdir(self.image_folder)
|
||||
if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp'))]
|
||||
|
||||
if not image_files:
|
||||
self.LOG.error("图片文件夹中没有图片")
|
||||
return None
|
||||
|
||||
# 随机选择一张图片
|
||||
random_pic = random.choice(image_files)
|
||||
pic_path = os.path.join(self.image_folder, random_pic)
|
||||
|
||||
return pic_path
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"获取随机图片出错: {e}")
|
||||
return None
|
||||
Reference in New Issue
Block a user