Nonebot 2 的权限与访问管理。
Project description
Nonebot 2 的权限管理系统
Rule 编写
规则文件一般放置于 "pam_config.config_path"/ 文件夹下面(仿造文件夹例子)
格式是 plugin_name.yaml,或者 plugin_name/xxx.yaml
plugin_name 是具体限制的插件名字,特别的,__all__.yaml 表示在全局启用(即,每一个指令都会检测)
Yaml 规范
pam:
- rule: user.id != '1919810'
reason: 你是?
这个是判断发送消息的 id 是不是 1919810,如果不是,就发送“你是?”并终止指令 pam 的匹配。特别的,这里指约束指令的首个元素,即,cmd = ('pam', 'reload', ) 也会被这个匹配到。
pam:
- rule: len(plugin.command) > 1 and plugin.command[1] == 'help'
ratelimit: limit.bucket(f'{plugin.command}', 60, 3)
reason: "{user.name},你怎么一直要查看帮助呢?"
要匹配 cmd = ('pam', 'help', ),或者类似的,则可以对 plugin.command 约束。具体的规则请查看规范。
并且,reason 是 f-string,也就是说,你可以在里面使用 {} 包裹一些变量,函数等,进行更加丰富的提示返回。这三个字段所运行的上下文都是一致的。不过可能缺失部分内建函数/类。
ratelimit 则是令牌桶管理,具体可以查看限速配置。
__all__
位置 "pam_config.config_path"/nonebot_plugin_mysticism.yaml
__all__:
- ratelimit: limit.bucket(f"{user.id}_{plugin.name}", max_burst=60)
reason: 你话好像有点多了?
表示对于插件 nonebot_plugin_mysticism 下面的所有指令进行限速(同一个限速桶)。
位置 "pam_config.config_path"/__all__.yaml
__all__:
- ratelimit: limit.bucket(user.id, 10, 1)
reason: 发太快了喵。
表示在全局,某一个人的发言频率过快,至多 10s 一次,无论是什么插件,什么指令都统一计算。
规则支持
Top Level
变量:
- bot
- event
- state
- message
- limit
- group
- plugin
- user
- bucket
模块/类:
- int
- datetime,这个是 datetime.datetime
- str
- re
部分字段算是语法糖,例如,group.name 在 Python 中需要 await 的。
bot
对 Bot 的包装,和 Nonebot2 的没啥区别。
event
对 Evnet 的包装,和 Nonebot2 的没啥区别,但是多了字段 type
- type: event 的类型,str
state
对 T_State 的包装,大概的区别就是可以 state._prefix 这么用。
message
event.get_plaintext(),没啥差别。
bucket
快捷访问 key 为 {user.id}_{plugin.name}_{plugin.command} 的 limit 桶.
group
群组信息,Onebot V11 适配器专属(其他适配器 pr wellcome)
- id: 群号,int
- name: 群名字,str
user
- id: 用户标识码,str
- superuser: 是否为 SuperUser,bool
Onebot V11 专属:
- name: 名字,群昵称优先,str
plugin
- name: 插件名字,str
- command: 当前执行的命令,tuple[str] | None
- bucket: 属于当前插件的桶(对应用户),limit
limit
限流,具体请查看限速配置。
限速配置
具体的实现是令牌桶。
py:
- ratelimit: limit.bucket(f"{user.id}_{plugin.name}_{plugin.command}")
reason: 还有 {limit.status(f"{user.id}_{plugin.name}_{plugin.command}"):.2f} 秒哦。
等价于
py:
- ratelimit: bucket.bucket()
reason: 还有 {bucket.status():.2f} 秒哦。
limit.bucket(key: Hashable, period: int, max_burst: int, count_pre_period: int) -> bool
- key: 限定是什么桶,注意,key = 1 和 key = '1' 是两个桶。
- period: 令牌添加间隔,也就是经过多少秒添加 count_pre_period 个令牌,默认 60.
- max_burst: 令牌桶最大容量,默认 3.
- count_pre_period: 每个周期添加的令牌数量
返回值为 True 意味着当前处于速率限制。
limit.status(key: Hashable) -> float
- key: 桶的key
返回值为还有多久的时间(单位 s)添加下一批令牌,如果为0则是当前还有令牌。
TODO LIST
Main
- 自动提取指令
- 添加给其他插件使用的接口
Checker
- 完善 alias 的识别
Server
- 新增 Change API
- fetch 返回全部可用指令,而不单单是COMMAND RULE中的。
WebUI
- 完善 Detail 展示
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nonebot_plugin_pam-0.2.3.tar.gz.
File metadata
- Download URL: nonebot_plugin_pam-0.2.3.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b441d7df955b3e84bece7e2ffaa237da1804a1fe9dad7e7b15ddb0ce6412bc2e
|
|
| MD5 |
7bd1c55b30ee593c7d955fcafdae7b40
|
|
| BLAKE2b-256 |
6696e97e7fa4840b278ace87ea636f50fd0710a2aa79887f3c226f9e2442141e
|
File details
Details for the file nonebot_plugin_pam-0.2.3-py3-none-any.whl.
File metadata
- Download URL: nonebot_plugin_pam-0.2.3-py3-none-any.whl
- Upload date:
- Size: 19.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8774533ad5b6bddf44d4d2f5f3cd216ef79c839878cf9a6c9d1d0a5b4527ec6f
|
|
| MD5 |
7f3f33938809ecf835b68aebbb743a8f
|
|
| BLAKE2b-256 |
d0d481748b2907794052096b2a66ac35bdd510fa0dbeb2fe9fa2d58d9fe57fd2
|