import logging import tomllib import aiohttp from wcferry import WxMsg, Wcf from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager class BotMusic: def __init__(self, wcf: Wcf, gbm: GroupBotManager): self.LOG = logging.getLogger(__name__) self.wcf = wcf # 假设 wcf 对象在此类中初始化 self.gbm = gbm # 权限功能 with open("music/config.toml", "rb") as f: plugin_config = tomllib.load(f) config = plugin_config["Music"] self.enable = config["enable"] self.command = config["command"] self.command_format = config["command-format"] self.LOG.info(f"[点歌台] 组件初始化完成,指令: {self.command}") async def get_music(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_format}", (message.roomid if message.from_group() else message.sender), message.sender) return # 如果触发了指令,但是没有权限,则返回权限不足 if self.gbm.get_group_permission(message.roomid, Feature.TASK_GAME) == PermissionStatus.DISABLED: return song_name = content[len(command[0]):].strip() async with aiohttp.ClientSession() as session: async with session.get( f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json") as resp: data = await resp.json() if data["code"] != 200: self.wcf.send_text(f"-----Bot-----\n❌点歌失败!\n{data}", (message.roomid if message.from_group() else message.sender), message.sender) return title = data["title"] singer = data["singer"] url = data["link"] music_url = data["music_url"].split("?")[0] cover_url = data["cover"] lyric = data["lrc"] xml = f"""{title}{singer}view30{url}{music_url}{url}{music_url}{cover_url}{lyric}000{cover_url}{bot.wxid}01""" self.wcf.send_xml(message.sender, xml, 3)