🐛 更改prefix在不同触发器的行为

This commit is contained in:
KimigaiiWuyi 2024-10-10 04:20:38 +08:00
parent 96ddc7ee64
commit 51baa9b511
3 changed files with 46 additions and 21 deletions

View File

@ -333,7 +333,6 @@ class SV:
else: else:
keyword_list = keyword keyword_list = keyword
entry = []
_pp = deepcopy(self.plugins.prefix) _pp = deepcopy(self.plugins.prefix)
if "" in _pp: if "" in _pp:
_pp.remove("") _pp.remove("")
@ -347,22 +346,34 @@ class SV:
# 去重 # 去重
_pp = list(set(_pp)) _pp = list(set(_pp))
for _k in keyword_list:
if prefix and _pp:
for _p in _pp:
entry.append(f'{_p}{_k}')
else:
entry.append(_k)
for tr in entry: for _k in keyword_list:
if tr not in self.TL: if _k not in self.TL:
logger.trace(f'载入{type}触发器【{tr}】!')
if type not in self.TL: if type not in self.TL:
self.TL[type] = {} self.TL[type] = {}
self.TL[type][tr] = Trigger( if prefix and _pp:
type, tr, modify_func(func), block, to_me for _p in _pp:
) _pk = _p + _k
self.TL[type][_pk] = Trigger(
type,
_k,
modify_func(func),
_p,
block,
to_me,
)
logger.trace(f'载入{type}触发器【{_pk}】!')
else:
self.TL[type][_k] = Trigger(
type,
_k,
modify_func(func),
"",
block,
to_me,
)
logger.trace(f'载入{type}触发器【{_k}】!')
@wraps(func) @wraps(func)
async def wrapper(bot: Bot, msg) -> Optional[Callable]: async def wrapper(bot: Bot, msg) -> Optional[Callable]:

View File

@ -19,10 +19,12 @@ class Trigger:
], ],
keyword: str, keyword: str,
func: Callable, func: Callable,
prefix: str = '',
block: bool = False, block: bool = False,
to_me: bool = False, to_me: bool = False,
): ):
self.type = type self.type = type
self.prefix = prefix
self.keyword = keyword self.keyword = keyword
self.func = func self.func = func
self.block = block self.block = block
@ -40,27 +42,33 @@ class Trigger:
return getattr(self, f'_check_{self.type}')(self.keyword, msg) return getattr(self, f'_check_{self.type}')(self.keyword, msg)
def _check_prefix(self, prefix: str, msg: str) -> bool: def _check_prefix(self, prefix: str, msg: str) -> bool:
if msg.startswith(prefix) and not self._check_fullmatch(prefix, msg): if msg.startswith(self.prefix + prefix) and not self._check_fullmatch(
prefix, msg
):
return True return True
return False return False
def _check_command(self, command: str, msg: str) -> bool: def _check_command(self, command: str, msg: str) -> bool:
if msg.startswith(command): if msg.startswith(self.prefix + command):
return True return True
return False return False
def _check_suffix(self, suffix: str, msg: str) -> bool: def _check_suffix(self, suffix: str, msg: str) -> bool:
if msg.endswith(suffix) and not self._check_fullmatch(suffix, msg): if (
msg.startswith(self.prefix)
and msg.endswith(suffix)
and not self._check_fullmatch(suffix, msg)
):
return True return True
return False return False
def _check_keyword(self, keyword: str, msg: str) -> bool: def _check_keyword(self, keyword: str, msg: str) -> bool:
if keyword in msg: if keyword in msg and msg.startswith(self.prefix):
return True return True
return False return False
def _check_fullmatch(self, keyword: str, msg: str) -> bool: def _check_fullmatch(self, keyword: str, msg: str) -> bool:
if msg == keyword: if msg == keyword and msg.startswith(self.prefix):
return True return True
return False return False
@ -71,9 +79,11 @@ class Trigger:
return False return False
def _check_regex(self, pattern: str, msg: str) -> bool: def _check_regex(self, pattern: str, msg: str) -> bool:
command_list = re.findall(pattern, msg) if msg.startswith(self.prefix):
if command_list: _msg = msg.replace(self.prefix, '')
return True command_list = re.findall(pattern, _msg)
if command_list:
return True
return False return False
def _check_message(self, keyword: str, msg: str): def _check_message(self, keyword: str, msg: str):
@ -83,6 +93,8 @@ class Trigger:
if self.type != 'regex': if self.type != 'regex':
msg.command = self.keyword msg.command = self.keyword
msg.text = msg.raw_text.replace(self.keyword, '') msg.text = msg.raw_text.replace(self.keyword, '')
if self.prefix:
msg.text = msg.text.replace(self.prefix, '')
else: else:
command_group = re.search(self.keyword, msg.raw_text) command_group = re.search(self.keyword, msg.raw_text)
if command_group: if command_group:

View File

@ -70,6 +70,7 @@ class StringConfig:
for i in self.config_list: for i in self.config_list:
_config[i] = self.config[i] _config[i] = self.config[i]
self.config = _config self.config = _config
self.write_config() self.write_config()
def write_config(self): def write_config(self):
@ -83,6 +84,7 @@ class StringConfig:
f.read(), f.read(),
type=Dict[str, GSC], type=Dict[str, GSC],
) )
# 对没有的值,添加默认值 # 对没有的值,添加默认值
for key in self.config_list: for key in self.config_list:
_defalut = self.config_list[key] _defalut = self.config_list[key]