Merge pull request #43 from ominkk/main

可配置xiaomuisc激活指令,其他指令需要激活后才能使用
This commit is contained in:
涵曦 2024-05-02 00:21:05 +08:00 committed by GitHub
commit a35cde5d4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 4 deletions

View File

@ -89,6 +89,7 @@ class Config:
"XIAOMUSIC_SEARCH", "ytsearch:" "XIAOMUSIC_SEARCH", "ytsearch:"
) # "bilisearch:" or "ytsearch:" ) # "bilisearch:" or "ytsearch:"
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "")
def __post_init__(self) -> None: def __post_init__(self) -> None:
if self.proxy: if self.proxy:

View File

@ -59,6 +59,7 @@ class XiaoMusic:
self.proxy = config.proxy self.proxy = config.proxy
self.search_prefix = config.search_prefix self.search_prefix = config.search_prefix
self.ffmpeg_location = config.ffmpeg_location self.ffmpeg_location = config.ffmpeg_location
self.active_cmd = config.active_cmd.split(",")
# 下载对象 # 下载对象
self.download_proc = None self.download_proc = None
@ -70,6 +71,7 @@ class XiaoMusic:
self._volume = 50 self._volume = 50
self._all_music = {} self._all_music = {}
self._play_list = [] self._play_list = []
self._playing = False
# 关机定时器 # 关机定时器
self._stop_timer = None self._stop_timer = None
@ -219,6 +221,7 @@ class XiaoMusic:
def set_last_record(self, query): def set_last_record(self, query):
self.last_record = { self.last_record = {
"query": query, "query": query,
"ctrl_panel": True,
} }
self.new_record_event.set() self.new_record_event.set()
@ -444,10 +447,11 @@ class XiaoMusic:
new_record = self.last_record new_record = self.last_record
self.polling_event.clear() # stop polling when processing the question self.polling_event.clear() # stop polling when processing the question
query = new_record.get("query", "").strip() query = new_record.get("query", "").strip()
self.log.debug("收到消息:%s", query) ctrl_panel = new_record.get("ctrl_panel", False)
self.log.debug("收到消息:%s 控制面板:%s", query, ctrl_panel)
# 匹配命令 # 匹配命令
opvalue, oparg = self.match_cmd(query) opvalue, oparg = self.match_cmd(query, ctrl_panel)
if not opvalue: if not opvalue:
await asyncio.sleep(1) await asyncio.sleep(1)
continue continue
@ -459,7 +463,7 @@ class XiaoMusic:
self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}") self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}")
# 匹配命令 # 匹配命令
def match_cmd(self, query): def match_cmd(self, query, ctrl_panel):
for opkey in KEY_MATCH_ORDER: for opkey in KEY_MATCH_ORDER:
patternarg = rf"(.*){opkey}(.*)" patternarg = rf"(.*){opkey}(.*)"
# 匹配参数 # 匹配参数
@ -478,14 +482,24 @@ class XiaoMusic:
) )
oparg = argafter oparg = argafter
opvalue = KEY_WORD_DICT[opkey] opvalue = KEY_WORD_DICT[opkey]
if not ctrl_panel and not self._playing:
if self.active_cmd and opvalue not in self.active_cmd:
self.log.debug(f"不在激活命令中 {opvalue}")
continue
if opkey in KEY_WORD_ARG_BEFORE_DICT: if opkey in KEY_WORD_ARG_BEFORE_DICT:
oparg = argpre oparg = argpre
self.log.info("匹配到指令. opkey:%s opvalue:%s oparg:%s", opkey, opvalue, oparg) self.log.info(
"匹配到指令. opkey:%s opvalue:%s oparg:%s", opkey, opvalue, oparg
)
return (opvalue, oparg) return (opvalue, oparg)
if self._playing:
self.log.info("未匹配到指令,自动停止")
return ("stop", {})
return (None, None) return (None, None)
# 播放歌曲 # 播放歌曲
async def play(self, **kwargs): async def play(self, **kwargs):
self._playing = True
parts = kwargs["arg1"].split("|") parts = kwargs["arg1"].split("|")
search_key = parts[0] search_key = parts[0]
name = parts[1] if len(parts) > 1 else search_key name = parts[1] if len(parts) > 1 else search_key
@ -545,6 +559,7 @@ class XiaoMusic:
await self.play_next() await self.play_next()
async def stop(self, **kwargs): async def stop(self, **kwargs):
self._playing = False
if self._next_timer: if self._next_timer:
self._next_timer.cancel() self._next_timer.cancel()
self.log.info(f"定时器已取消") self.log.info(f"定时器已取消")