Skip to content

Commit

Permalink
📝 添加使用方法及PluginMetadata (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Well2333 authored Apr 8, 2024
1 parent 9a1b18a commit 3126092
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# nonebot-plugin-auto-bot-selector

## 使用

```python
from nonebot import on_message
from nonebot.exception import ActionFailed
from nonebot_plugin_alconna.uniseg import Target, UniMessage
from nonebot_plugin_auto_bot_selector import get_bots
from nonebot_plugin_auto_bot_selector.expection import NoBotFoundError
from nonebot_plugin_auto_bot_selector.target import PlatformTarget, TargetQQGroup
from nonebot_plugin_auto_bot_selector.utils.alconna import create_target, extract_target

foo = on_message()


@foo.handle()
async def _recive_target(alc_target: Target):
# 使用 alconna 提取目标
abs_target = extract_target(alc_target)
# 手动创建目标
qq_group = TargetQQGroup(group_id=123456)
# do something
await foo.finish(f"用户 {abs_target} 已保存")


async def _send_msg_to_target(abs_target: PlatformTarget, msg: UniMessage):
alc_target = create_target(abs_target)
try:
bots = get_bots(abs_target)
except NoBotFoundError:
return "没有可用的推送 Bot"

for bot in get_bots(abs_target):
try:
await msg.send(target=alc_target, bot=bot)
return "发送成功"
except ActionFailed:
# 发送失败
continue
else:
return "全部推送 Bot 发送失败"
```
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "nonebot-plugin-auto-bot-selector"
version = "0.2.0"
description = "Default template for PDM package"
version = "0.2.1"
description = "自动Bot选择器,消息推送好帮手"
authors = [{ name = "Well404", email = "[email protected]" }]
dependencies = ["nonebot2>=2.2.1", "typing-extensions>=4.11.0"]
requires-python = ">=3.8"
Expand Down
32 changes: 32 additions & 0 deletions src/nonebot_plugin_auto_bot_selector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,44 @@
import nonebot
from nonebot import logger
from nonebot.adapters import Bot
from nonebot.plugin import PluginMetadata

from . import adapters # noqa: F401
from .expection import NoBotFoundError
from .registries import BOT_CACHE, BOT_CACHE_LOCK, info_current, refresh_bot
from .target import PlatformTarget

try:
from importlib.metadata import version
except ImportError:
from importlib_metadata import version

try:
__version__ = version("nonebot_plugin_auto_bot_selector")
except Exception:
__version__ = None

__plugin_meta__ = PluginMetadata(
name="nonebot-plugin-auto-bot-selector",
description="自动Bot选择器,消息推送好帮手",
usage="如果你需要主动发送消息,那么这款插件将会免去你找不到合适推送 Bot 的烦恼",
homepage="https://github.com/MountainDash/nonebot-plugin-auto-bot-selector",
type="library",
supported_adapters={
"~onebot.v11",
"~onebot.v12",
"~qq",
"~kaiheila",
"~red",
"~dodo",
"~satori",
},
extra={
"author": "Well404",
"version": __version__,
},
)

driver = nonebot.get_driver()


Expand Down

0 comments on commit 3126092

Please sign in to comment.