Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可配置xiaomuisc激活指令,其他指令需要激活后才能使用 #43

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 19 additions & 4 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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}(.*)"
# 匹配参数
Expand All @@ -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
Expand Down Expand Up @@ -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"定时器已取消")
Expand Down