2022-02-06 14:52:50 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
from contextlib import AsyncExitStack
|
2022-09-08 10:37:16 +08:00
|
|
|
|
from typing import Set, Tuple, Union, NoReturn, Optional
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
from nonebot.dependencies import Dependent
|
2022-04-04 10:35:14 +08:00
|
|
|
|
from nonebot.utils import run_coro_with_catch
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.exception import SkippedException
|
|
|
|
|
from nonebot.typing import T_DependencyCache, T_PermissionChecker
|
|
|
|
|
|
2022-02-06 17:08:11 +08:00
|
|
|
|
from .adapter import Bot, Event
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from .params import BotParam, EventParam, DependParam, DefaultParam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Permission:
|
|
|
|
|
"""{ref}`nonebot.matcher.Matcher` 权限类。
|
|
|
|
|
|
|
|
|
|
当事件传递时,在 {ref}`nonebot.matcher.Matcher` 运行前进行检查。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
checkers: PermissionChecker
|
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```python
|
|
|
|
|
Permission(async_function) | sync_function
|
|
|
|
|
# 等价于
|
|
|
|
|
Permission(async_function, sync_function)
|
|
|
|
|
```
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ("checkers",)
|
|
|
|
|
|
|
|
|
|
HANDLER_PARAM_TYPES = [
|
|
|
|
|
DependParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
DefaultParam,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def __init__(self, *checkers: Union[T_PermissionChecker, Dependent[bool]]) -> None:
|
2022-08-14 19:41:00 +08:00
|
|
|
|
self.checkers: Set[Dependent[bool]] = {
|
2022-02-06 14:52:50 +08:00
|
|
|
|
checker
|
|
|
|
|
if isinstance(checker, Dependent)
|
|
|
|
|
else Dependent[bool].parse(
|
|
|
|
|
call=checker, allow_types=self.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
for checker in checkers
|
2022-08-14 19:41:00 +08:00
|
|
|
|
}
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""存储 `PermissionChecker`"""
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"Permission({', '.join(repr(checker) for checker in self.checkers)})"
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def __call__(
|
|
|
|
|
self,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""检查是否满足某个权限
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
"""
|
|
|
|
|
if not self.checkers:
|
|
|
|
|
return True
|
|
|
|
|
results = await asyncio.gather(
|
|
|
|
|
*(
|
2022-04-04 10:35:14 +08:00
|
|
|
|
run_coro_with_catch(
|
2022-02-06 14:52:50 +08:00
|
|
|
|
checker(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
2022-04-04 10:35:14 +08:00
|
|
|
|
),
|
|
|
|
|
(SkippedException,),
|
|
|
|
|
False,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
for checker in self.checkers
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
return any(results)
|
|
|
|
|
|
2022-03-17 21:11:37 +08:00
|
|
|
|
def __and__(self, other: object) -> NoReturn:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
raise RuntimeError("And operation between Permissions is not allowed.")
|
|
|
|
|
|
|
|
|
|
def __or__(
|
|
|
|
|
self, other: Optional[Union["Permission", T_PermissionChecker]]
|
|
|
|
|
) -> "Permission":
|
|
|
|
|
if other is None:
|
|
|
|
|
return self
|
|
|
|
|
elif isinstance(other, Permission):
|
|
|
|
|
return Permission(*self.checkers, *other.checkers)
|
|
|
|
|
else:
|
|
|
|
|
return Permission(*self.checkers, other)
|
|
|
|
|
|
2022-03-17 21:11:37 +08:00
|
|
|
|
def __ror__(
|
|
|
|
|
self, other: Optional[Union["Permission", T_PermissionChecker]]
|
|
|
|
|
) -> "Permission":
|
|
|
|
|
if other is None:
|
|
|
|
|
return self
|
|
|
|
|
elif isinstance(other, Permission):
|
|
|
|
|
return Permission(*other.checkers, *self.checkers)
|
|
|
|
|
else:
|
|
|
|
|
return Permission(other, *self.checkers)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
class User:
|
|
|
|
|
"""检查当前事件是否属于指定会话
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
users: 会话 ID 元组
|
|
|
|
|
perm: 需同时满足的权限
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ("users", "perm")
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self, users: Tuple[str, ...], perm: Optional[Permission] = None
|
|
|
|
|
) -> None:
|
|
|
|
|
self.users = users
|
|
|
|
|
self.perm = perm
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f"User(users={self.users}"
|
|
|
|
|
+ (f", permission={self.perm})" if self.perm else "")
|
|
|
|
|
+ ")"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def __call__(self, bot: Bot, event: Event) -> bool:
|
2022-09-29 16:56:06 +08:00
|
|
|
|
try:
|
|
|
|
|
session = event.get_session_id()
|
|
|
|
|
except Exception:
|
|
|
|
|
return False
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return bool(
|
2022-09-29 16:56:06 +08:00
|
|
|
|
session in self.users and (self.perm is None or await self.perm(bot, event))
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def USER(*users: str, perm: Optional[Permission] = None):
|
|
|
|
|
"""匹配当前事件属于指定会话
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
user: 会话白名单
|
|
|
|
|
perm: 需要同时满足的权限
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return Permission(User(users, perm))
|