2022-01-19 16:16:56 +08:00
|
|
|
|
"""本模块是 {ref}`nonebot.matcher.Matcher.permission` 的类型定义。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
每个 {ref}`nonebot.matcher.Matcher` 拥有一个 {ref}`nonebot.permission.Permission` ,
|
|
|
|
|
其中是 `PermissionChecker` 的集合,只要有一个 `PermissionChecker` 检查结果为 `True` 时就会继续运行。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 6
|
|
|
|
|
description: nonebot.permission 模块
|
2020-09-14 20:48:03 +08:00
|
|
|
|
"""
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.params import EventType
|
2021-11-14 18:51:23 +08:00
|
|
|
|
from nonebot.adapters import Bot, Event
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.internal.permission import USER as USER
|
|
|
|
|
from nonebot.internal.permission import User as User
|
|
|
|
|
from nonebot.internal.permission import Permission as Permission
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
class Message:
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""检查是否为消息事件"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "Message()"
|
|
|
|
|
|
2021-12-23 17:50:59 +08:00
|
|
|
|
async def __call__(self, type: str = EventType()) -> bool:
|
|
|
|
|
return type == "message"
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
class Notice:
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""检查是否为通知事件"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "Notice()"
|
|
|
|
|
|
2021-12-23 17:50:59 +08:00
|
|
|
|
async def __call__(self, type: str = EventType()) -> bool:
|
|
|
|
|
return type == "notice"
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
class Request:
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""检查是否为请求事件"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "Request()"
|
|
|
|
|
|
2021-12-23 17:50:59 +08:00
|
|
|
|
async def __call__(self, type: str = EventType()) -> bool:
|
|
|
|
|
return type == "request"
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
class MetaEvent:
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""检查是否为元事件"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "MetaEvent()"
|
|
|
|
|
|
2021-12-23 17:50:59 +08:00
|
|
|
|
async def __call__(self, type: str = EventType()) -> bool:
|
|
|
|
|
return type == "meta_event"
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
MESSAGE: Permission = Permission(Message())
|
|
|
|
|
"""匹配任意 `message` 类型事件
|
|
|
|
|
|
|
|
|
|
仅在需要同时捕获不同类型事件时使用,优先使用 message type 的 Matcher。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
"""
|
2022-01-19 16:16:56 +08:00
|
|
|
|
NOTICE: Permission = Permission(Notice())
|
|
|
|
|
"""匹配任意 `notice` 类型事件
|
|
|
|
|
|
|
|
|
|
仅在需要同时捕获不同类型事件时使用,优先使用 notice type 的 Matcher。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
"""
|
2022-01-19 16:16:56 +08:00
|
|
|
|
REQUEST: Permission = Permission(Request())
|
|
|
|
|
"""匹配任意 `request` 类型事件
|
|
|
|
|
|
|
|
|
|
仅在需要同时捕获不同类型事件时使用,优先使用 request type 的 Matcher。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
"""
|
2022-01-19 16:16:56 +08:00
|
|
|
|
METAEVENT: Permission = Permission(MetaEvent())
|
|
|
|
|
"""匹配任意 `meta_event` 类型事件
|
|
|
|
|
|
|
|
|
|
仅在需要同时捕获不同类型事件时使用,优先使用 meta_event type 的 Matcher。
|
2020-09-14 20:48:03 +08:00
|
|
|
|
"""
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
class SuperUser:
|
2022-01-19 16:16:56 +08:00
|
|
|
|
"""检查当前事件是否是消息事件且属于超级管理员"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return "Superuser()"
|
|
|
|
|
|
2021-12-06 10:10:51 +08:00
|
|
|
|
async def __call__(self, bot: Bot, event: Event) -> bool:
|
2022-09-18 22:33:36 +08:00
|
|
|
|
return (
|
2021-12-26 18:46:54 +08:00
|
|
|
|
f"{bot.adapter.get_name().split(maxsplit=1)[0].lower()}:{event.get_user_id()}"
|
|
|
|
|
in bot.config.superusers
|
|
|
|
|
or event.get_user_id() in bot.config.superusers # 兼容旧配置
|
2021-12-06 10:10:51 +08:00
|
|
|
|
)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
|
SUPERUSER: Permission = Permission(SuperUser())
|
2022-09-18 22:33:36 +08:00
|
|
|
|
"""匹配任意超级用户事件"""
|
2022-01-19 16:16:56 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
__autodoc__ = {
|
|
|
|
|
"Permission": True,
|
|
|
|
|
"Permission.__call__": True,
|
|
|
|
|
"User": True,
|
|
|
|
|
"USER": True,
|
|
|
|
|
}
|