64 lines
3.0 KiB
Python
64 lines
3.0 KiB
Python
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"""<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>{bot.wxid}</fromusername><scene>0</scene><appinfo><version>1</version><appname/></appinfo><commenturl/>"""
|
|
self.wcf.send_xml(message.sender, xml, 3)
|