mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 00:55:07 +08:00
✅ add command alias/group test
This commit is contained in:
parent
4e4d494ffd
commit
2921ba0120
@ -139,8 +139,8 @@ def on_endswith(msg: str,
|
||||
|
||||
|
||||
def on_command(cmd: Union[str, Tuple[str, ...]],
|
||||
alias: Set[Union[str, Tuple[str, ...]]] = None,
|
||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
aliases: Set[Union[str, Tuple[str, ...]]] = None,
|
||||
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||
if isinstance(cmd, str):
|
||||
cmd = (cmd,)
|
||||
@ -153,14 +153,14 @@ def on_command(cmd: Union[str, Tuple[str, ...]],
|
||||
handlers = kwargs.pop("handlers", [])
|
||||
handlers.insert(0, _strip_cmd)
|
||||
|
||||
if alias:
|
||||
alias = set(map(lambda x: (x,) if isinstance(x, str) else x, alias))
|
||||
if aliases:
|
||||
aliases = set(map(lambda x: (x,) if isinstance(x, str) else x, aliases))
|
||||
group = MatcherGroup("message",
|
||||
Rule() & rule,
|
||||
handlers=handlers,
|
||||
**kwargs)
|
||||
for cmd_ in [cmd, *alias]:
|
||||
group.new(rule=command(cmd_))
|
||||
for cmd_ in [cmd, *aliases]:
|
||||
_tmp_matchers.add(group.new(rule=command(cmd_)))
|
||||
return group
|
||||
else:
|
||||
return on_message(command(cmd) & rule, handlers=handlers, **
|
||||
|
6
tests/test_plugins/test_group/__init__.py
Normal file
6
tests/test_plugins/test_group/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from nonebot.rule import to_me
|
||||
from nonebot import CommandGroup
|
||||
|
||||
test = CommandGroup("test", rule=to_me())
|
||||
|
||||
from . import commands
|
11
tests/test_plugins/test_group/commands.py
Normal file
11
tests/test_plugins/test_group/commands.py
Normal file
@ -0,0 +1,11 @@
|
||||
from nonebot.typing import Bot, Event
|
||||
from nonebot.permission import GROUP_OWNER
|
||||
|
||||
from . import test
|
||||
|
||||
test_1 = test.command("1", aliases={"test"}, permission=GROUP_OWNER)
|
||||
|
||||
|
||||
@test_1.handle()
|
||||
async def test1(bot: Bot, event: Event, state: dict):
|
||||
await test_1.finish(event.raw_message)
|
@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import Event
|
||||
from nonebot.plugin import on_message
|
||||
from nonebot.adapters.cqhttp import Bot
|
||||
|
||||
test_message = on_message(to_me(), state={"default": 1})
|
||||
|
||||
|
||||
@test_message.handle()
|
||||
async def test_handler(bot: Bot, event: Event, state: dict):
|
||||
print("[*] Test Matcher Received:", event)
|
||||
state["event"] = event
|
||||
await bot.send(message="Received", event=event)
|
||||
|
||||
|
||||
@test_message.receive()
|
||||
async def test_receive(bot: Bot, event: Event, state: dict):
|
||||
print("[*] Test Matcher Received next time:", event)
|
||||
print("[*] Current State:", state)
|
Loading…
Reference in New Issue
Block a user