import logging import tomllib import requests from wcferry import WxMsg, Wcf from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager import lz4.block as lb 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}") 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❌命令格式错误!\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.MUSIC) == PermissionStatus.DISABLED: return user_song_name = content[len(command[0]):].strip() try: short_play_api = f"https://qqmusic.qqovo.cn/getSearchByKey?key={user_song_name}&page=1&limit=1" fallback_api = f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={user_song_name}&n=1&num=1&type=json" response = requests.get(short_play_api) if response.status_code == 400: response = requests.get(fallback_api) if response.status_code != 200: print(f"API 请求失败,状态码: {response.status_code}") return json_data = response.json() result = json_data.get('response', {}).get('data', {}).get('song', {}).get('list', []) if not result: print("未找到匹配的歌曲") return first_song = result[0] song_name = first_song.get('songname', '') song_mid = first_song.get('songmid', '') first_singer_name = first_song.get('singer', [{}])[0].get('name', '') zhida_singer = json_data.get('response', {}).get('data', {}).get('zhida', {}).get('zhida_singer', {}) singer_pic = zhida_singer.get('singerPic', '') if zhida_singer else None music_play_api = f"https://qqmusic.qqovo.cn/getMusicPlay?songmid={song_mid}&quality=m4a" music_response = requests.get(music_play_api) if music_response.status_code == 400: print("获取播放链接失败,状态码 400,尝试备用接口") music_response = requests.get(fallback_api) if music_response.status_code != 200: print(f"获取播放链接失败,状态码: {music_response.status_code}") return music_data = music_response.json() play_url = music_data.get('data', {}).get('playUrl', {}).get(song_mid, {}).get('url', '') if not play_url: print("主接口play_url为空,尝试备用接口") music_response = requests.get(fallback_api).json() song_name = music_response.get('title', song_name) first_singer_name = music_response.get('singer', first_singer_name) play_url = music_response.get('music_url', '') singer_pic = music_response.get('cover', singer_pic) dataurl = music_response.get('link', '') else: dataurl = f"https://y.qq.com/n/ryqq/songDetail/{song_mid}" if not play_url: print("未获取到音乐播放链接") return except requests.RequestException as e: self.wcf.send_text(f"-----Bot-----\n❌请求出错:{e}", (message.roomid if message.from_group() else message.sender), message.sender) return xml_message = f""" {song_name} {first_singer_name}\n❤Bot-祝您天天开心❤ view 3 0 {dataurl} {play_url} 0 0 0 {singer_pic} 0 49 """ # 修改消息数据库里面的消息content 内容 text_bytes = xml_message.encode('utf-8') compressed_data = lb.compress(text_bytes, store_size=False).hex() data = self.wcf.query_sql('MSG0.db', "SELECT * FROM MSG where type = 49 limit 1") self.wcf.query_sql('MSG0.db', f"""UPDATE MSG SET CompressContent = x'{compressed_data}', BytesExtra=x'', type=49, SubType=3, IsSender=0, TalkerId=2 WHERE MsgSvrID={data[0]['MsgSvrID']}""" ) result = self.wcf.forward_msg(data[0]["MsgSvrID"], message.roomid) print(f"点歌发送:{result}")