From 83d0e02eb4c002b2441f745a8b465c82defe3dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sun, 30 Jun 2024 06:31:47 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/config.py | 2 +- xiaomusic/xiaomusic.py | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 4d84d08ebb..fa41197dc5 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -104,7 +104,7 @@ def __post_init__(self) -> None: self.append_keyword(keywords_playlocal, "playlocal") keywords_play = os.getenv("XIAOMUSIC_KEYWORDS_PLAY", "播放歌曲,放歌曲") self.append_keyword(keywords_play, "play") - keywords_stop = os.getenv("XIAOMUSIC_KEYWORDS_STOP", "关机,暂停,停止") + keywords_stop = os.getenv("XIAOMUSIC_KEYWORDS_STOP", "关机,暂停,停止,停止播放") self.append_keyword(keywords_stop, "stop") # 保存配置到 config-example.json 文件 diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index a5e79ec43e..51e787a056 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -610,8 +610,26 @@ async def run_forever(self): except Exception as e: self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}") + # 检查是否匹配到完全一样的指令 + def check_full_match_cmd(self, query, ctrl_panel): + if query in self.config.key_match_order: + opkey = query + opvalue = self.config.key_word_dict.get(opkey) + if ctrl_panel or self._playing: + return opvalue + else: + if not self.active_cmd or opvalue in self.active_cmd: + return opvalue + return None + # 匹配命令 def match_cmd(self, query, ctrl_panel): + # 优先处理完全匹配 + opvalue = self.check_full_match_cmd(query, ctrl_panel) + if opvalue: + self.log.info(f"完全匹配指令. query:{query} opvalue:{opvalue}") + return (opvalue, None) + for opkey in self.config.key_match_order: patternarg = rf"(.*){opkey}(.*)" # 匹配参数 @@ -629,16 +647,14 @@ def match_cmd(self, query, ctrl_panel): argafter, ) oparg = argafter + if opkey in KEY_WORD_ARG_BEFORE_DICT: + oparg = argpre opvalue = self.config.key_word_dict.get(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}") + self.log.ifno(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(f"匹配到指令. opkey:{opkey} opvalue:{opvalue} oparg:{oparg}") return (opvalue, oparg) if self._playing: self.log.info("未匹配到指令,自动停止")