From bbdf41f33458cd0e0be906c9c20ad9125ced3fac Mon Sep 17 00:00:00 2001 From: lv99 <540776546@qq.com> Date: Wed, 1 May 2024 09:35:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AExiaomuisc=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E6=8C=87=E4=BB=A4,=E5=85=B6=E4=BB=96=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E9=9C=80=E8=A6=81=E6=BF=80=E6=B4=BB=E5=90=8E=E6=89=8D?= =?UTF-8?q?=E8=83=BD=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/config.py | 1 + xiaomusic/xiaomusic.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 5aa0bb745..1af78f12f 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -89,6 +89,7 @@ class Config: "XIAOMUSIC_SEARCH", "ytsearch:" ) # "bilisearch:" or "ytsearch:" ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") + active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "") def __post_init__(self) -> None: if self.proxy: diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index 28f8bf442..3f7b679ad 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -59,6 +59,7 @@ def __init__(self, config: Config): self.proxy = config.proxy self.search_prefix = config.search_prefix self.ffmpeg_location = config.ffmpeg_location + self.active_cmd = config.active_cmd.split(",") # 下载对象 self.download_proc = None @@ -70,6 +71,7 @@ def __init__(self, config: Config): self._volume = 50 self._all_music = {} self._play_list = [] + self._playing = False # 关机定时器 self._stop_timer = None @@ -219,6 +221,7 @@ def _get_last_query(self, data): def set_last_record(self, query): self.last_record = { "query": query, + "ctrl_panel": True, } self.new_record_event.set() @@ -444,10 +447,11 @@ async def run_forever(self): new_record = self.last_record self.polling_event.clear() # stop polling when processing the question 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: await asyncio.sleep(1) continue @@ -459,7 +463,7 @@ async def run_forever(self): 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: patternarg = rf"(.*){opkey}(.*)" # 匹配参数 @@ -478,14 +482,24 @@ def match_cmd(self, query): ) oparg = argafter 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: 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) + if self._playing: + self.log.info("未匹配到指令,自动停止") + return ("stop", {}) return (None, None) # 播放歌曲 async def play(self, **kwargs): + self._playing = True parts = kwargs["arg1"].split("|") search_key = parts[0] name = parts[1] if len(parts) > 1 else search_key @@ -545,6 +559,7 @@ async def random_play(self, **kwargs): await self.play_next() async def stop(self, **kwargs): + self._playing = False if self._next_timer: self._next_timer.cancel() self.log.info(f"定时器已取消")