104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
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"""<appmsg appid="wx79f2c4418704b4f8" sdkver="0">
|
|
<title>{title}</title>
|
|
<des>{singer}</des>
|
|
<action>view</action>
|
|
<type>3</type>
|
|
<showtype>0</showtype>
|
|
<content/>
|
|
<url>{url}</url>
|
|
<dataurl>{music_url}</dataurl>
|
|
<lowurl>{url}</lowurl>
|
|
<lowdataurl>{music_url}</lowdataurl>
|
|
<recorditem/>
|
|
<thumburl>{cover_url}</thumburl>
|
|
<messageaction/>
|
|
<laninfo/>
|
|
<extinfo/>
|
|
<sourceusername/>
|
|
<sourcedisplayname/>
|
|
<songlyric>{lyric}</songlyric>
|
|
<commenturl/>
|
|
<appattach><totallen>0</totallen><attachid/><emoticonmd5/><fileext/><aeskey/></appattach>
|
|
<webviewshared><publisherId/><publisherReqId>0</publisherReqId></webviewshared>
|
|
<weappinfo><pagepath/><username/><appid/><appservicetype>0</appservicetype></weappinfo>
|
|
<websearch/>
|
|
<songalbumurl>{cover_url}</songalbumurl>
|
|
</appmsg>
|
|
<fromusername>{message.sender}</fromusername>
|
|
<scene>0</scene>
|
|
<appinfo><version>1</version><appname/></appinfo>
|
|
<commenturl/>"""
|
|
|
|
self.wcf.forward_msg(message.sender, xml, 3)
|