Rule-based authorization manager worked with nonebot2
Project description
前言
由于本模块的性质,未来即使提交更新也仅对本仓库进行。
正在使用的 PyPI 发行版来源也应择日改为使用本 Git 来源。
Highlights
为 Matcher 或 MatcherGroup 配置一条或多条 Rule 来实现功能的授权管理
# MatcherGroup
from nonebot.plugin import MatcherGroup
from nonebot_plugin_rauthman import isInService
workers = MatcherGroup(
type="message", rule=isInService("module_name", 10)
) # Rule 自动套用到 MatcherGroup 下所有 Matcher
worker_1 = workers.on_regex(...)
worker_2 = workers.on_regex(...)
worker_3 = workers.on_regex(...)
worker_4 = workers.on_regex(...)
worker_5 = workers.on_regex(...)
worker_6 = workers.on_regex(...)
...
# Matcher
from nonebot.plugin import on_command
from nonebot_plugin_rauthman import isInService
worker = on_command(
"test", rule=isInService("module_name_A", 10) & isInService("module_name_B", 10)
) # 同时满足多个 Rule 才可触发
...
# Matcher
from nonebot_plugin_rauthman import isInService
from nonebot_plugin_translator import translator
translator.rule.checkers.add(
isInService("translator", 10)
) # 为具名的 Matcher 附加 RuleChecker
...
授权策略可选 根据可用功能 或 根据服务级别
参数可完全自定义
class Config:
savedata: str = getattr(_config, "savedata", "") or ""
ram_policy: int = getattr(_config, "ram_policy", 0) or 0
ram_cmd: str = getattr(_config, "ram_cmd", "ram") or "ram"
ram_add: str = getattr(_config, "ram_add", "-a") or "-a"
ram_rm: str = getattr(_config, "ram_rm", "-r") or "-r"
ram_show: str = getattr(_config, "ram_show", "-s") or "-s"
ram_available: str = getattr(_config, "ram_available", "-v") or "-v"
开始使用
对于 PyPI 发行版来源
通过 poetry 添加到 NoneBot2 项目的 pyproject.toml
poetry add nonebot-plugin-rauthman
也可以通过 pip 从 PyPI 安装
pip install nonebot-plugin-rauthman
参照下文在 NoneBot2 项目的环境文件 .env.* 中添加配置项
对于 Git 来源
配置项
# .env.prod
savedata = Yuni/savedata # 保存路径,相对路径,此处为保存至运行目录下的 "Yuni/savedata/" 下,默认为 ""
ram_policy = 0 # 授权策略 0 为根据可用功能 1 为根据服务级别,默认为 0
ram_cmd = ram # 指令名,或者叫触发词,默认为 ram
ram_add = -a # 启用功能(根据可用功能),默认为 -a
ram_rm = -r # 禁用功能(根据可用功能),默认为 -r
ram_show = -s # 展示群功能状态(根据可用功能),默认为 -s
ram_available = -v # 展示全局可用功能(根据可用功能),默认为 -v
from nonebot.plugin import on_command
from nonebot_plugin_rauthman import isInService
command = on_command("cmd", rule=isInService("module_name", 1))
小白案例
# .env.prod
ram_cmd = 功能 # 指令名,默认为 ram
ram_add = 开启 # 启用功能(根据可用功能),默认为 -a
ram_rm = 关闭 # 禁用功能(根据可用功能),默认为 -r
ram_show = 查询 # 展示群功能状态(根据可用功能),默认为 -s
ram_available = 全局查询 # 展示全局可用功能(根据可用功能),默认为 -v
from nonebot.plugin import on_notice
from nonebot_plugin_rauthman import isInService
notice = on_notice(rule=to_me() & isInService("戳一戳", 1))
常见问题
- 这个插件可以做到什么?
- RAM 可以实现对不同群,不同功能的控制
- 提示群聊未注册是怎么回事?
- 本地 JSON 文件中不存在该群群号,则会提示为群聊未注册进行一次授权变更操作即可生成,如 ram 0
- 谁可以开启/关闭功能?
- 批量对群进行授权修改?
- 私聊 Bot 直接发送 ram 并根据提示操作
- 我设置了 ram_policy = 1,怎么设置群 Level?
- 例如在 Bot 所在群聊中发送 ram 10这样这个群的 Level 就被设定成 10 默认的 Level 为 0
授权修改操作与当前授权策略无关
- 如果我希望在一个群中,管理员和群主可以修改开关/设置群 Level 我该怎么办?
- 对源代码第 101 行进行修改
permission: Permission = SUPERUSER # 参考 NoneBot2 文档 - 进阶 - 权限控制
特别感谢
Sichongzou 对 README.md 小白案例 和 常见问题 的贡献
优化建议
Project details
Release history Release notifications | RSS feed
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_rauthman-2.0.0rc1.post3.tar.gz.
File metadata
- Download URL: nonebot_plugin_rauthman-2.0.0rc1.post3.tar.gz
- Upload date:
- Size: 26.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.10 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7973e7c9b0968221894cc799449ee361bb90708e06602917b8cdd6d446f8948c
|
|
| MD5 |
c368f4200a3c383b98135ae4fd44a97f
|
|
| BLAKE2b-256 |
c73dfc1fb546f0877653dff8d8f3118ef32f45a06c015dca7bfa0872c0e3f029
|
File details
Details for the file nonebot_plugin_rauthman-2.0.0rc1.post3-py3-none-any.whl.
File metadata
- Download URL: nonebot_plugin_rauthman-2.0.0rc1.post3-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.10 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
067389599b61eceb37613cbb50e066d3fa562cebb599138bb74ef9f8cd3b9f0c
|
|
| MD5 |
deb3714c1173a1becae385e50e7fd318
|
|
| BLAKE2b-256 |
ed706629031da5d7a3f13d2d36563192911489a7ec93309e65b77c54de88f24b
|