优化原神语音合成

This commit is contained in:
CMHopeSunshine 2022-09-25 14:53:10 +08:00
parent 1f6ba5a2e3
commit 3ee1bfb0c4
2 changed files with 28 additions and 13 deletions

View File

@ -2,9 +2,7 @@ from typing import Union
from nonebot import on_regex
from nonebot.plugin import PluginMetadata
from nonebot.params import RegexDict
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent, MessageSegment
from nonebot.typing import T_State
from LittlePaimon.utils.tool import freq_limiter
from LittlePaimon.utils.filter import filter_msg
from LittlePaimon.manager.plugin_manager import plugin_manager as pm
@ -28,23 +26,14 @@ SUPPORTS_CHARA = ['派蒙', '凯亚', '安柏', '丽莎', '琴', '香菱', '枫
CHARA_RE = '|'.join(SUPPORTS_CHARA)
def is_paimon(event: Union[GroupMessageEvent, PrivateMessageEvent], state: T_State) -> bool:
if '_matched_dict' in state:
if not state['_matched_dict']['chara'] and event.is_tome():
state['_matched_dict']['chara'] = '派蒙'
return True
return False
voice_cmd = on_regex(rf'^(?P<chara>({CHARA_RE})?)说(?P<text>[\w“”——!\?,\.`\'"\(\)\[\]{{}}~\s]+)',
voice_cmd = on_regex(rf'^(?P<chara>{CHARA_RE})说(?P<text>[\w“”——!\?,\.`\'"\(\)\[\]{{}}~\s]+)',
priority=90, block=True,
state={
'pm_name': '原神语音合成',
'pm_description': 'AI语音合成让原神角色说任何话',
'pm_usage': '<角色名>说<话>',
'pm_priority': 10
}, rule=Rule(is_paimon))
})
@voice_cmd.handle()

View File

@ -1,7 +1,10 @@
import re
from typing import Union, Tuple, Set
from nonebot import on_command, on_regex, on_endswith, on_keyword, on_startswith
import nonebot
from nonebot.adapters.onebot.v11 import MessageEvent, Bot, log
from nonebot.adapters.onebot import v11
"""
通过猴子补丁为nonebot的部分matcher注入其命令到默认state中
@ -48,9 +51,32 @@ def on_keyword_(keywords: Set[str], state: dict = None, *args, **kwargs):
return on_keyword(keywords=keywords, state=state, _depth=1, *args, **kwargs)
def _check_nickname(bot: Bot, event: MessageEvent) -> None:
"""检查消息开头是否存在昵称,去除并赋值 `event.to_me`。
参数:
bot: Bot 对象
event: MessageEvent 对象
"""
first_msg_seg = event.message[0]
if first_msg_seg.type != "text":
return
if nicknames := set(filter(lambda n: n, bot.config.nickname)):
# check if the user is calling me with my nickname
nickname_regex = "|".join(nicknames)
first_text = first_msg_seg.data["text"]
if m := re.search(rf"^({nickname_regex})([\s,]*|$)", first_text, re.IGNORECASE):
nickname = m[1]
log("DEBUG", f"User is calling me {nickname}")
event.to_me = True
# first_msg_seg.data["text"] = first_text[m.end():]
nonebot.on_command = on_command_
nonebot.on_regex = on_regex_
nonebot.on_startswith = on_startswith_
nonebot.on_endswith = on_endswith_
nonebot.on_keyword = on_keyword_
v11.bot._check_nickname = _check_nickname