mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 03:56:45 +00:00
move decorators to plugin module
This commit is contained in:
@ -15,8 +15,7 @@ from .typing import CommandName_T, CommandArgs_T
|
||||
|
||||
class NLProcessor:
|
||||
__slots__ = ('func', 'keywords', 'permission', 'only_to_me',
|
||||
'only_short_message', 'allow_empty_message', '__name__', '__qualname__', '__doc__',
|
||||
'__annotations__', '__dict__')
|
||||
'only_short_message', 'allow_empty_message')
|
||||
|
||||
def __init__(self, *, func: Callable, keywords: Optional[Iterable],
|
||||
permission: int, only_to_me: bool, only_short_message: bool,
|
||||
@ -101,44 +100,6 @@ class NLPManager:
|
||||
return False
|
||||
|
||||
|
||||
def on_natural_language(
|
||||
keywords: Union[Optional[Iterable], str, Callable] = None,
|
||||
*,
|
||||
permission: int = perm.EVERYBODY,
|
||||
only_to_me: bool = True,
|
||||
only_short_message: bool = True,
|
||||
allow_empty_message: bool = False) -> Callable:
|
||||
"""
|
||||
Decorator to register a function as a natural language processor.
|
||||
|
||||
:param keywords: keywords to respond to, if None, respond to all messages
|
||||
:param permission: permission required by the processor
|
||||
:param only_to_me: only handle messages to me
|
||||
:param only_short_message: only handle short messages
|
||||
:param allow_empty_message: handle empty messages
|
||||
"""
|
||||
|
||||
def deco(func: Callable) -> NLProcessor:
|
||||
nl_processor = NLProcessor(
|
||||
func=func,
|
||||
keywords=keywords, # type: ignore
|
||||
permission=permission,
|
||||
only_to_me=only_to_me,
|
||||
only_short_message=only_short_message,
|
||||
allow_empty_message=allow_empty_message)
|
||||
NLPManager.add_nl_processor(nl_processor)
|
||||
update_wrapper(wrapper=nl_processor, wrapped=func) # type: ignore
|
||||
return nl_processor
|
||||
|
||||
if isinstance(keywords, Callable):
|
||||
# here "keywords" is the function to be decorated
|
||||
return on_natural_language()(keywords)
|
||||
else:
|
||||
if isinstance(keywords, str):
|
||||
keywords = (keywords,)
|
||||
return deco
|
||||
|
||||
|
||||
class NLPSession(BaseSession):
|
||||
__slots__ = ('msg', 'msg_text', 'msg_images')
|
||||
|
||||
|
Reference in New Issue
Block a user