Skip to main content

Nonebot2 会话级配置信息存储插件

Project description

logo

✨ nonebot-plugin-session-config ✨

LICENSE pypi python uv
ruff pre-commit

📖 介绍

本插件为每个会话(群聊、私聊等场景)提供了独立的持久化配置存储功能。

💿 安装

使用 nb-cli 安装

nb plugin install nonebot-plugin-session-config --upgrade

使用 uv 安装

uv add nonebot-plugin-session-config

安装仓库 master 分支

uv add git+https://github.com/USTC-XeF2/nonebot-plugin-session-config@master

打开 nonebot2 项目根目录下的 pyproject.toml 文件, 在 [tool.nonebot.plugins] 部分追加写入

"@local" = ["nonebot_plugin_session_config"]

⚙️ 配置

所有配置项均以 SESSION_CONFIG_ 为前缀(下文省略),且均为选填项。

配置项 默认值 说明
BASE_DIR None 配置存储的根目录,值为 None 时使用 localstore 提供的配置文件目录,一般不需要更改
DIR_FORMAT bot-{bot_id} 各机器人所属目录的命名格式,不使用 bot_id 模板参数时所有机器人共用同一目录
FILE_FORMAT {scene_type}-{scene_id}.yaml 配置文件的命名格式
USE_GLOBAL False 是否尝试使用全局配置作为默认值,开启此项时请确保会话配置与全局配置间没有意料外的重复键
ENABLE_PARAM False 是否启用会话配置参数注入功能,启用后可直接指定会话配置类作为消息处理函数参数类型,在插件冲突时可能会注入失败

🎉 使用

from nonebot import on_message

from nonebot_plugin_session_config import (
    BaseSessionConfig,
    check_enabled,
    get_session_config,
)


# 所有会话配置类均应继承自 BaseSessionConfig
class SessionConfig(BaseSessionConfig):
    test_enabled: bool = False
    test_key: int = 0


message_handler = on_message(
    rule=check_enabled(SessionConfig, "test_enabled"),
)


@message_handler.handle()
async def _(session_config: SessionConfig = get_session_config(SessionConfig)):
    await message_handler.finish(f"Test key value is: {session_config.test_key}")


# 使用自动参数注入(需在配置中启用 ENABLE_PARAM 选项)
@message_handler.handle()
async def _(session_config: SessionConfig):
    await message_handler.finish(f"Test key value is: {session_config.test_key}")

本插件提供的配置不提供在程序中动态修改的接口,若需要修改请手动或自动编辑对应的文件,无需重启即可更新。

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

nonebot_plugin_session_config-1.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_session_config-1.0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_session_config-1.0.1.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_session_config-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f235f053f442e593aa1744b614fd5f2415b03acd36aa0247719535ee0d3a8bad
MD5 db3fbaa03f555d6064ff2f6b56cecce6
BLAKE2b-256 486a703c4312681f2c75ac12368391386c499094103e3d72d1739cdd61115e1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_session_config-1.0.1.tar.gz:

Publisher: release.yml on USTC-XeF2/nonebot-plugin-session-config

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_session_config-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_session_config-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f681954a483633eb67cd73611bb4ff2c2c112b5a167a5795842c5593beb5449a
MD5 cabee122fa489bf603b2b02ff4c01b5d
BLAKE2b-256 fd3fdc0cf6ccae33552c58d1c1922942981c84cf2d203193082c534160bcb7d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_session_config-1.0.1-py3-none-any.whl:

Publisher: release.yml on USTC-XeF2/nonebot-plugin-session-config

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page