move decorators to plugin module

This commit is contained in:
yanyongyu
2020-04-20 13:50:38 +08:00
parent 9fbd09331c
commit 4f9a9136f9
19 changed files with 327 additions and 259 deletions

View File

@ -4,5 +4,6 @@ from nonebot.typing import Message_T
class ValidateError(ValueError):
def __init__(self, message: Optional[Message_T] = None):
self.message = message

View File

@ -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

View File

@ -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

View File

@ -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]:

View File

@ -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.