🐛 更改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:
keyword_list = keyword
entry = []
_pp = deepcopy(self.plugins.prefix)
if "" in _pp:
_pp.remove("")
@ -347,22 +346,34 @@ class SV:
# 去重
_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:
if tr not in self.TL:
logger.trace(f'载入{type}触发器【{tr}】!')
for _k in keyword_list:
if _k not in self.TL:
if type not in self.TL:
self.TL[type] = {}
self.TL[type][tr] = Trigger(
type, tr, modify_func(func), block, to_me
)
if prefix and _pp:
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)
async def wrapper(bot: Bot, msg) -> Optional[Callable]:

View File

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

View File

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