Skip to main content

提供协议适配时常用的注入函数

Project description

nonebot-plugin-params

此插件为插件编写者提供便利的函数用于实现多适配器兼容。

可以避免一长串的 try...except 语句来实现的多适配器兼容代码。

比如说我的插件只需要发送 text 和 at 类型的消息,这是基本所有适配器都实现的功能。为了拿到 MessageSegment 目前只能使用 try...import... 于是此插件提供了一行式的注入函数来拿到当前适配器对应的 MessageSegment。

比如说我只想匹配私聊消息类型,我可以在 handler 使用 try...import 然后再 isinstance,但这无疑很繁琐而且没有任何可读性。但是不用 isinstance 好像又做不成,因为不是所有私聊消息类型都叫 message.private,QQ 里有子类型 friend 和 group,飞书里则叫做 message.p2p,去阅读这些东西会耗费大量时间。

此插件并没有完全避免上述情况发生,但是提供了一些便捷的方式去访问适配器类型,提供了一些便捷的函数如 is_private_message 来适配所有私聊类型。

安装 Install

pip install nonebot-plugin-params

注意 Warning

引用本插件前,在 __init__.py 头部使用 require 保证插件加载。

from nonebot import require

require("nonebot_plugin_params")

# writing main code
import os

from nonebot_plugin_params import ONEBOT

插件示例 Example

先来看一个简单的示例程序,这是 示例插件项目地址,这里只提取了一部分用作示例。

from typing import TYPE_CHECKING, Type, cast

from nonebot import on_command
from nonebot.adapters import Event, MessageSegment
from nonebot.matcher import Matcher

from nonebot_plugin_params import (
    FEISHU,
    ONEBOT,
    AdapterName,
    MessageSegmentClass,
    allow_adapters,
    is_private_message,
)

if TYPE_CHECKING:
    from nonebot.adapters.feishu import MessageSegment as Feishu_MessageSegment
    from nonebot.adapters.onebot.v11 import MessageSegment as Onebot_MessageSegment


wordle = on_command("wordle", rule=allow_adapters(ONEBOT, FEISHU) & is_private_message)


@wordle.handle()
async def _(
    matcher: Matcher,
    event: Event,
    adapter_name: str = AdapterName(),
    MS: Type[MessageSegment] = MessageSegmentClass(),
) -> None:
    await matcher.send("欢迎来到 wordle")
    if adapter_name == ONEBOT:
        MS = cast("Type[Onebot_MessageSegment]", MS)  # only for type hint
        await matcher.send(MS.at(event.get_user_id()) + MS.text("mua~"))
        # user id like "1748272409"

    elif adapter_name == FEISHU:
        MS = cast("Type[Feishu_MessageSegment]", MS)  # only for type hint
        await matcher.send(MS.at(event.get_user_id()) + MS.text("mua~"))
        # user id like "3e3cf96b"

如果你有 QQ 和 Feishu 两个机器人,可以尝试一下上面这个程序。

下面解释一些代码的作用。

from nonebot_plugin_params import ONEBOT, FEISHU, TELEGRAM, QQGUILD

导入适配器名称,可以用于便利的条件判断。

wordle = on_command("wordle", rule=allow_adapters(ONEBOT, FEISHU) & is_private_message)

其中 rule 作用是使得当前这个 matcher 仅接受来自 QQ 或者 Feishu 的事件,并且都是私聊事件。

@wordle.handle()
async def _(
    matcher: Matcher,
    adapter_name: str = AdapterName(),
    MS: Type[MessageSegment] = MessageSegmentClass(),
) -> None:
    await matcher.send("欢迎来到 wordle")

其中 MessageSegmentClass() 获取当前适配器对应的 MessageSegment 类。

可用 API

  • ONEBOT

  • FEISHU

  • TELEGRAM

  • QQGUILD

  • AdapterName

  • EventName

  • ImageSegmentMethod

  • MessageSegmentClass

  • PRIVATEMESSAGE

  • allow_adapters

  • is_private_message

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-params-0.1.1.tar.gz (6.0 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_params-0.1.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file nonebot-plugin-params-0.1.1.tar.gz.

File metadata

  • Download URL: nonebot-plugin-params-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/3.10.0 keyring/23.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for nonebot-plugin-params-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3123bdc8c2bae97245076db4d69e499079d0521ddb0a3b7353ed93ecfce4bb53
MD5 479fc118748fd0629d49270c4f4ee2fd
BLAKE2b-256 6aeee696d9391db983322fd5983d4c7b25f3255ac9df5572d89c0c6420721439

See more details on using hashes here.

File details

Details for the file nonebot_plugin_params-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nonebot_plugin_params-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/3.10.0 keyring/23.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for nonebot_plugin_params-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 870c210cf2239e4ec361b637574e42838a8c2f3b762c6af0f1cf279d71b445e4
MD5 f2cf82d6dd8b2c36b0d41f469caf0de0
BLAKE2b-256 fe8021488bf03fb8b1e8c5d79ca732cf57f9ffc06e5b901cf24772ada4800cc9

See more details on using hashes here.

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