From 2921ba0120a02dc322de54376016d25fc6e59839 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 27 Sep 2020 23:52:03 +0800 Subject: [PATCH] :white_check_mark: add command alias/group test --- nonebot/plugin.py | 10 +++++----- tests/test_plugins/test_group/__init__.py | 6 ++++++ tests/test_plugins/test_group/commands.py | 11 +++++++++++ tests/test_plugins/test_message.py | 22 ---------------------- 4 files changed, 22 insertions(+), 27 deletions(-) create mode 100644 tests/test_plugins/test_group/__init__.py create mode 100644 tests/test_plugins/test_group/commands.py delete mode 100644 tests/test_plugins/test_message.py diff --git a/nonebot/plugin.py b/nonebot/plugin.py index 4936dc8e..d3db0631 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -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, ** diff --git a/tests/test_plugins/test_group/__init__.py b/tests/test_plugins/test_group/__init__.py new file mode 100644 index 00000000..1f47bd5c --- /dev/null +++ b/tests/test_plugins/test_group/__init__.py @@ -0,0 +1,6 @@ +from nonebot.rule import to_me +from nonebot import CommandGroup + +test = CommandGroup("test", rule=to_me()) + +from . import commands diff --git a/tests/test_plugins/test_group/commands.py b/tests/test_plugins/test_group/commands.py new file mode 100644 index 00000000..e825329d --- /dev/null +++ b/tests/test_plugins/test_group/commands.py @@ -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) diff --git a/tests/test_plugins/test_message.py b/tests/test_plugins/test_message.py deleted file mode 100644 index e683b630..00000000 --- a/tests/test_plugins/test_message.py +++ /dev/null @@ -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)