Skip to main content
nonebot

nonebot-plugin-uniref

NoneBot2 可持久化的跨平台实体引用插件 ✨

license Python NoneBot pypi

本插件提供可比较、可序列化的 UserRefSceneRef,用于在 NoneBot 插件中持久化用户和场景身份。

安装

  • 使用 nb-cli
nb plugin install nonebot-plugin-uniref
  • 使用 pip
pip install nonebot-plugin-uniref

使用

获取事件中的 Ref

from nonebot_plugin_uniref import EventSceneRef, EventUserRef


@matcher.handle()
async def handle(user_ref: EventUserRef, scene_ref: EventSceneRef): ...

或调用 get_user_ref(bot, session, event=None)get_scene_ref(bot, session, event=None)。 无法建立 Ref 时抛出 RefUnavailableError

构造 Ref

from nonebot_plugin_alconna import At, UniMsg
from nonebot_plugin_uniref import RefContext


@matcher.handle()
async def handle(message: UniMsg, refs: RefContext):
    user_ids = [at.target for at in message[At] if at.flag == "user"]
    users = [refs.user(user_id) for user_id in user_ids]

编码与恢复 Ref

from nonebot_plugin_uniref import UserRef, decode_ref, encode_ref

ref = UserRef(namespace="QQClient", id="123")
value = encode_ref(ref)

assert value == "user:QQClient:123"
assert decode_ref(value) == ref

构造 Alconna Target

from nonebot_plugin_uniref import decode_ref, ref_to_target

ref = decode_ref("scene:QQClient:group:456")
target = ref_to_target(ref, bot=bot)

省略 bot 时由 Ref 限定候选;传入 bot 时严格绑定同一逻辑 Bot,不回退到其他账号。 ref_to_target() 只构造发送地址,不保证 Bot 当前在线、具有权限或能够触达目标。

模型定义

UserRef

属性 类型 含义
namespace str 用户 ID 所属的身份命名空间
id str 用户在该范围内的 ID

SceneRef

属性 类型 含义
namespace str 场景 ID 所属的身份命名空间
type SceneKind UniRef 定义的场景种类
id str 场景在该范围内的完整 ID

SceneKind 包含 privategroupguildchannel_textchannel_categorychannel_voice;codec 仍将它们编码为对应的可读字符串。

namespace 表示 ID 的相等范围;同一真人在不同 namespace 中可以拥有不同的 UserRef。Ref 不绑定运行中 的 Bot,也不是数据库行引用。平台 ID 仅在某个 App 内有效时,对应平台的 AppID 会进入 namespace; 它表示身份作用域,不表示 Bot 实例。仅按 Bot 隔离业务数据时,仍由业务插件加入 Bot 维度。

支持的 Adapter

{...} 表示决定 ID 相等范围的平台值;id= 后才是实际的 Ref.id。Scene 类型已经由 SceneRef.type 保存,所以 namespace 不再重复 privategroupchannel

已验证 Adapter / 场景 UserRef:namespace;ID SceneRef:namespace;type;ID
OneBot V11、Milky QQClient;id=QQ 号 QQClientprivate:QQ 号;group:群号
Telegram Telegram;id=User ID Telegramprivategroup;id=Chat ID
Discord Discord;id=User Snowflake Discordprivateguild、Channel;id=Snowflake
QQ C2C QQAPI:c2c:{QQ AppID};id=user_openid QQAPI:{QQ AppID}private;id=user_openid
QQ 群聊 QQAPI:group:{QQ AppID}:{group_openid};id=member_openid QQAPI:{QQ AppID}group;id=group_openid
QQ 公开文字子频道 QQAPI:guild:{QQ AppID}:{guild_id};id=author.id QQAPI:{QQ AppID}channel_text;id=channel_id
飞书私聊 Feishu:{Feishu AppID};id=open_id 同 namespace;private;id=open_id
飞书群聊 Feishu:{Feishu AppID};id=open_id Feishugroup;id=chat_id

QQ UserRef 用 c2c / group / guild 区分三套用户 ID。群成员按 App 和群隔离,不能直接转换为发送 Target;频道用户按 App 和 Guild 隔离,可以转换为频道私信 Target。

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_uniref-0.3.0.tar.gz (14.1 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_uniref-0.3.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_uniref-0.3.0.tar.gz.

File metadata

  • Download URL: nonebot_plugin_uniref-0.3.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for nonebot_plugin_uniref-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4755b89ad6541f8ab1f7a0c4b3c117c572f1f1f9ad36d372c2885bec0fd408ba
MD5 d3317ecedb853f7746c2d7c96c780e03
BLAKE2b-256 b3f8f31f360ce7f32ba30fc5652db2ce5aada0a15977b5efb465819a423fc019

See more details on using hashes here.

File details

Details for the file nonebot_plugin_uniref-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: nonebot_plugin_uniref-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for nonebot_plugin_uniref-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bcb5c7139f98a7b9e2acf9f228b9393fdfd872a12129cb8fe40964696753b46
MD5 1c8fb59eeccf538f9c19e9a33ca99f51
BLAKE2b-256 b75a4cba45b0e1156b976f811c843abb2dcd90bf5844445a8d8a40404e8ee57d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.1

2 files

0.4.0

2 files

This release

0.3.0 This release

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page