mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-29 17:27:41 +00:00
move decorators to plugin module
This commit is contained in:
@ -4,5 +4,6 @@ from nonebot.typing import Message_T
|
||||
|
||||
|
||||
class ValidateError(ValueError):
|
||||
|
||||
def __init__(self, message: Optional[Message_T] = None):
|
||||
self.message = message
|
||||
|
@ -11,8 +11,8 @@ def handle_cancellation(session: CommandSession):
|
||||
|
||||
def control(value):
|
||||
if _is_cancellation(value) is True:
|
||||
session.finish(render_expression(
|
||||
session.bot.config.SESSION_CANCEL_EXPRESSION))
|
||||
session.finish(
|
||||
render_expression(session.bot.config.SESSION_CANCEL_EXPRESSION))
|
||||
return value
|
||||
|
||||
return control
|
||||
|
@ -15,13 +15,15 @@ def _simple_chinese_to_bool(text: str) -> Optional[bool]:
|
||||
"""
|
||||
text = text.strip().lower().replace(' ', '') \
|
||||
.rstrip(',.!?~,。!?~了的呢吧呀啊呗啦')
|
||||
if text in {'要', '用', '是', '好', '对', '嗯', '行',
|
||||
'ok', 'okay', 'yeah', 'yep',
|
||||
'当真', '当然', '必须', '可以', '肯定', '没错', '确定', '确认'}:
|
||||
if text in {
|
||||
'要', '用', '是', '好', '对', '嗯', '行', 'ok', 'okay', 'yeah', 'yep',
|
||||
'当真', '当然', '必须', '可以', '肯定', '没错', '确定', '确认'
|
||||
}:
|
||||
return True
|
||||
if text in {'不', '不要', '不用', '不是', '否', '不好', '不对', '不行', '别',
|
||||
'no', 'nono', 'nonono', 'nope', '不ok', '不可以', '不能',
|
||||
'不可以'}:
|
||||
if text in {
|
||||
'不', '不要', '不用', '不是', '否', '不好', '不对', '不行', '别', 'no', 'nono',
|
||||
'nonono', 'nope', '不ok', '不可以', '不能', '不可以'
|
||||
}:
|
||||
return False
|
||||
return None
|
||||
|
||||
@ -31,8 +33,8 @@ def _split_nonempty_lines(text: str) -> List[str]:
|
||||
|
||||
|
||||
def _split_nonempty_stripped_lines(text: str) -> List[str]:
|
||||
return list(filter(lambda x: x,
|
||||
map(lambda x: x.strip(), text.splitlines())))
|
||||
return list(filter(lambda x: x, map(lambda x: x.strip(),
|
||||
text.splitlines())))
|
||||
|
||||
|
||||
simple_chinese_to_bool = _simple_chinese_to_bool
|
||||
|
@ -14,8 +14,11 @@ def _extract_text(arg: Message_T) -> str:
|
||||
def _extract_image_urls(arg: Message_T) -> List[str]:
|
||||
"""Extract all image urls from a message-like object."""
|
||||
arg_as_msg = Message(arg)
|
||||
return [s.data['url'] for s in arg_as_msg
|
||||
if s.type == 'image' and 'url' in s.data]
|
||||
return [
|
||||
s.data['url']
|
||||
for s in arg_as_msg
|
||||
if s.type == 'image' and 'url' in s.data
|
||||
]
|
||||
|
||||
|
||||
def _extract_numbers(arg: Message_T) -> List[float]:
|
||||
|
@ -6,6 +6,7 @@ from nonebot.typing import Filter_T
|
||||
|
||||
|
||||
class BaseValidator:
|
||||
|
||||
def __init__(self, message=None):
|
||||
self.message = message
|
||||
|
||||
@ -69,8 +70,7 @@ def match_regex(pattern: str, message=None, *, flags=0,
|
||||
return validate
|
||||
|
||||
|
||||
def ensure_true(bool_func: Callable[[Any], bool],
|
||||
message=None) -> Filter_T:
|
||||
def ensure_true(bool_func: Callable[[Any], bool], message=None) -> Filter_T:
|
||||
"""
|
||||
Validate any object to ensure the result of applying
|
||||
a boolean function to it is True.
|
||||
|
Reference in New Issue
Block a user