import logging
import tomllib
import aiohttp
import requests
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}")
import requests
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.MUSIC) == PermissionStatus.DISABLED:
return
song_name = content[len(command[0]):].strip()
try:
response = requests.get(
f"https://www.hhlqilongzhu.cn/api/dg_wyymusic.php?gm={song_name}&n=1&br=2&type=json",
timeout=5 # 设置超时,防止阻塞
)
data = response.json()
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
if data.get("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}
view
3
0
{url}
{music_url}
{url}
{music_url}
{cover_url}
{lyric}
0
0
0
{cover_url}
{message.sender}
0
1
"""
self.wcf.forward_msg(message.sender, xml, 3)