From 36e99bc3eae7e56550faae339a90b80c71af3c9c Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Sun, 19 Mar 2023 15:45:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feature:=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=86=85=E7=BD=AE=E5=93=8D=E5=BA=94=E8=A7=84=E5=88=99=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=B1=BB=E5=9E=8B=E9=99=90=E5=88=B6=20(#1824)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/internal/matcher/matcher.py | 9 ++------- nonebot/internal/permission.py | 17 +++++++++++++++++ nonebot/rule.py | 10 ---------- tests/test_rule.py | 9 +++++++-- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/nonebot/internal/matcher/matcher.py b/nonebot/internal/matcher/matcher.py index baba2f28..f91d1a5f 100644 --- a/nonebot/internal/matcher/matcher.py +++ b/nonebot/internal/matcher/matcher.py @@ -21,7 +21,7 @@ from typing import ( from nonebot.log import logger from nonebot.internal.rule import Rule from nonebot.dependencies import Dependent -from nonebot.internal.permission import USER, User, Permission +from nonebot.internal.permission import User, Permission from nonebot.internal.adapter import ( Bot, Event, @@ -682,12 +682,7 @@ class Matcher(metaclass=MatcherMeta): stack=stack, dependency_cache=dependency_cache, ) - permission = self.permission - if len(permission.checkers) == 1 and isinstance( - user_perm := tuple(permission.checkers)[0].call, User - ): - permission = user_perm.perm - return USER(event.get_session_id(), perm=permission) + return Permission(User.from_event(event, perm=self.permission)) async def resolve_reject(self): handler = current_handler.get() diff --git a/nonebot/internal/permission.py b/nonebot/internal/permission.py index f1be4b37..0fe465ab 100644 --- a/nonebot/internal/permission.py +++ b/nonebot/internal/permission.py @@ -1,4 +1,5 @@ import asyncio +from typing_extensions import Self from contextlib import AsyncExitStack from typing import Set, Tuple, Union, NoReturn, Optional @@ -140,6 +141,22 @@ class User: session in self.users and (self.perm is None or await self.perm(bot, event)) ) + @classmethod + def from_event(cls, event: Event, perm: Optional[Permission] = None) -> Self: + """从事件中获取会话 ID + + 参数: + event: Event 对象 + perm: 需同时满足的权限 + """ + if ( + perm + and len(perm.checkers) == 1 + and isinstance(user_perm := tuple(perm.checkers)[0].call, cls) + ): + perm = user_perm.perm + return cls((event.get_session_id(),), perm) + def USER(*users: str, perm: Optional[Permission] = None): """匹配当前事件属于指定会话 diff --git a/nonebot/rule.py b/nonebot/rule.py index 98ea0418..4cd1b7bc 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -164,8 +164,6 @@ class StartswithRule: return hash((frozenset(self.msg), self.ignorecase)) async def __call__(self, event: Event, state: T_State) -> bool: - if event.get_type() != "message": - return False try: text = event.get_plaintext() except Exception: @@ -221,8 +219,6 @@ class EndswithRule: return hash((frozenset(self.msg), self.ignorecase)) async def __call__(self, event: Event, state: T_State) -> bool: - if event.get_type() != "message": - return False try: text = event.get_plaintext() except Exception: @@ -278,8 +274,6 @@ class FullmatchRule: return hash((frozenset(self.msg), self.ignorecase)) async def __call__(self, event: Event, state: T_State) -> bool: - if event.get_type() != "message": - return False try: text = event.get_plaintext() except Exception: @@ -330,8 +324,6 @@ class KeywordsRule: return hash(frozenset(self.keywords)) async def __call__(self, event: Event, state: T_State) -> bool: - if event.get_type() != "message": - return False try: text = event.get_plaintext() except Exception: @@ -649,8 +641,6 @@ class RegexRule: return hash((self.regex, self.flags)) async def __call__(self, event: Event, state: T_State) -> bool: - if event.get_type() != "message": - return False try: msg = event.get_message() except Exception: diff --git a/tests/test_rule.py b/tests/test_rule.py index 727e6ef6..f6a17d79 100644 --- a/tests/test_rule.py +++ b/tests/test_rule.py @@ -133,6 +133,7 @@ async def test_trie(app: App): ("prefix", False, "message", "fooprefix", False), ("prefix", False, "message", None, False), (("prefix", "foo"), False, "message", "fooprefix", True), + ("prefix", False, "notice", "prefix", True), ("prefix", False, "notice", "foo", False), ], ) @@ -172,6 +173,7 @@ async def test_startswith( ("suffix", False, "message", "suffixfoo", False), ("suffix", False, "message", None, False), (("suffix", "foo"), False, "message", "suffixfoo", True), + ("suffix", False, "notice", "suffix", True), ("suffix", False, "notice", "foo", False), ], ) @@ -211,6 +213,7 @@ async def test_endswith( ("fullmatch", False, "message", "_fullmatch_", False), ("fullmatch", False, "message", None, False), (("fullmatch", "foo"), False, "message", "fullmatchfoo", False), + ("fullmatch", False, "notice", "fullmatch", True), ("fullmatch", False, "notice", "foo", False), ], ) @@ -245,8 +248,9 @@ async def test_fullmatch( (("key",), "message", "_key_", True), (("key", "foo"), "message", "_foo_", True), (("key",), "message", None, False), - (("key",), "notice", "foo", False), (("key",), "message", "foo", False), + (("key",), "notice", "_key_", True), + (("key",), "notice", "foo", False), ], ) async def test_keyword( @@ -410,7 +414,8 @@ async def test_shell_command(): {"key": "key1"}, ), (r"foo", "message", None, False, None, None, None, None), - (r"foo", "notice", "foo", False, None, None, None, None), + (r"foo", "notice", "foo", True, "foo", "foo", tuple(), {}), + (r"foo", "notice", "bar", False, None, None, None, None), ], ) async def test_regex(