mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 09:38:21 +08:00
⚗️ add command alias and group #11
This commit is contained in:
parent
ecbe465232
commit
3dc95b904f
@ -9,9 +9,9 @@ from dataclasses import dataclass
|
|||||||
from importlib._bootstrap import _load
|
from importlib._bootstrap import _load
|
||||||
|
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.matcher import Matcher
|
|
||||||
from nonebot.permission import Permission
|
from nonebot.permission import Permission
|
||||||
from nonebot.typing import Handler, RuleChecker
|
from nonebot.typing import Handler, RuleChecker
|
||||||
|
from nonebot.matcher import Matcher, MatcherGroup
|
||||||
from nonebot.rule import Rule, startswith, endswith, command, regex
|
from nonebot.rule import Rule, startswith, endswith, command, regex
|
||||||
from nonebot.typing import Set, List, Dict, Type, Tuple, Union, Optional, ModuleType
|
from nonebot.typing import Set, List, Dict, Type, Tuple, Union, Optional, ModuleType
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class Plugin(object):
|
|||||||
matcher: Set[Type[Matcher]]
|
matcher: Set[Type[Matcher]]
|
||||||
|
|
||||||
|
|
||||||
def on(rule: Union[Rule, RuleChecker] = Rule(),
|
def on(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
*,
|
*,
|
||||||
handlers: Optional[List[Handler]] = None,
|
handlers: Optional[List[Handler]] = None,
|
||||||
@ -47,7 +47,7 @@ def on(rule: Union[Rule, RuleChecker] = Rule(),
|
|||||||
return matcher
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
def on_metaevent(rule: Union[Rule, RuleChecker] = Rule(),
|
def on_metaevent(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
*,
|
*,
|
||||||
handlers: Optional[List[Handler]] = None,
|
handlers: Optional[List[Handler]] = None,
|
||||||
temp: bool = False,
|
temp: bool = False,
|
||||||
@ -66,7 +66,7 @@ def on_metaevent(rule: Union[Rule, RuleChecker] = Rule(),
|
|||||||
return matcher
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
def on_message(rule: Union[Rule, RuleChecker] = Rule(),
|
def on_message(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
*,
|
*,
|
||||||
handlers: Optional[List[Handler]] = None,
|
handlers: Optional[List[Handler]] = None,
|
||||||
@ -86,7 +86,7 @@ def on_message(rule: Union[Rule, RuleChecker] = Rule(),
|
|||||||
return matcher
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
def on_notice(rule: Union[Rule, RuleChecker] = Rule(),
|
def on_notice(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
*,
|
*,
|
||||||
handlers: Optional[List[Handler]] = None,
|
handlers: Optional[List[Handler]] = None,
|
||||||
temp: bool = False,
|
temp: bool = False,
|
||||||
@ -105,7 +105,7 @@ def on_notice(rule: Union[Rule, RuleChecker] = Rule(),
|
|||||||
return matcher
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
def on_request(rule: Union[Rule, RuleChecker] = Rule(),
|
def on_request(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
*,
|
*,
|
||||||
handlers: Optional[List[Handler]] = None,
|
handlers: Optional[List[Handler]] = None,
|
||||||
temp: bool = False,
|
temp: bool = False,
|
||||||
@ -125,7 +125,7 @@ def on_request(rule: Union[Rule, RuleChecker] = Rule(),
|
|||||||
|
|
||||||
|
|
||||||
def on_startswith(msg: str,
|
def on_startswith(msg: str,
|
||||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
rule: Optional[Optional[Union[Rule, RuleChecker]]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
**kwargs) -> Type[Matcher]:
|
**kwargs) -> Type[Matcher]:
|
||||||
return on_message(startswith(msg) &
|
return on_message(startswith(msg) &
|
||||||
@ -134,7 +134,7 @@ def on_startswith(msg: str,
|
|||||||
|
|
||||||
|
|
||||||
def on_endswith(msg: str,
|
def on_endswith(msg: str,
|
||||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
rule: Optional[Optional[Union[Rule, RuleChecker]]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
**kwargs) -> Type[Matcher]:
|
**kwargs) -> Type[Matcher]:
|
||||||
return on_message(endswith(msg) &
|
return on_message(endswith(msg) &
|
||||||
@ -143,9 +143,10 @@ def on_endswith(msg: str,
|
|||||||
|
|
||||||
|
|
||||||
def on_command(cmd: Union[str, Tuple[str, ...]],
|
def on_command(cmd: Union[str, Tuple[str, ...]],
|
||||||
|
alias: Set[Union[str, Tuple[str, ...]]] = None,
|
||||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Permission = Permission(),
|
||||||
**kwargs) -> Type[Matcher]:
|
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||||
if isinstance(cmd, str):
|
if isinstance(cmd, str):
|
||||||
cmd = (cmd,)
|
cmd = (cmd,)
|
||||||
|
|
||||||
@ -157,10 +158,21 @@ def on_command(cmd: Union[str, Tuple[str, ...]],
|
|||||||
handlers = kwargs.pop("handlers", [])
|
handlers = kwargs.pop("handlers", [])
|
||||||
handlers.insert(0, _strip_cmd)
|
handlers.insert(0, _strip_cmd)
|
||||||
|
|
||||||
return on_message(
|
if alias:
|
||||||
command(cmd) &
|
alias = set(map(lambda x: (x,) if isinstance(x, str) else x, alias))
|
||||||
rule, permission, handlers=handlers, **kwargs) if rule else on_message(
|
group = MatcherGroup("message",
|
||||||
command(cmd), permission, handlers=handlers, **kwargs)
|
Rule() & rule,
|
||||||
|
permission,
|
||||||
|
handlers=handlers,
|
||||||
|
**kwargs)
|
||||||
|
for cmd_ in [cmd, *alias]:
|
||||||
|
group.new(rule=command(cmd_))
|
||||||
|
return group
|
||||||
|
else:
|
||||||
|
return on_message(
|
||||||
|
command(cmd) & rule, permission, handlers=handlers, **
|
||||||
|
kwargs) if rule else on_message(
|
||||||
|
command(cmd), permission, handlers=handlers, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def on_regex(pattern: str,
|
def on_regex(pattern: str,
|
||||||
@ -234,3 +246,21 @@ def load_builtin_plugins():
|
|||||||
|
|
||||||
def get_loaded_plugins() -> Set[Plugin]:
|
def get_loaded_plugins() -> Set[Plugin]:
|
||||||
return set(plugins.values())
|
return set(plugins.values())
|
||||||
|
|
||||||
|
|
||||||
|
class CommandGroup:
|
||||||
|
|
||||||
|
def __init__(self, name: Union[str, Tuple[str, ...]], **kwargs):
|
||||||
|
self.basename = (name,) if isinstance(name, str) else name
|
||||||
|
if "aliases" in kwargs:
|
||||||
|
del kwargs["aliases"]
|
||||||
|
self.base_kwargs = kwargs
|
||||||
|
|
||||||
|
def command(self, name: Union[str, Tuple[str, ...]],
|
||||||
|
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||||
|
sub_name = (name,) if isinstance(name, str) else name
|
||||||
|
name = self.basename + sub_name
|
||||||
|
|
||||||
|
final_kwargs = self.base_kwargs.copy()
|
||||||
|
final_kwargs.update(kwargs)
|
||||||
|
return on_command(name, **final_kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user