nonebot-plugin-clovers 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nonebot-plugin-clovers might be problematic. Click here for more details.
- nonebot_plugin_clovers/__init__.py +64 -0
- nonebot_plugin_clovers/adapters/main.py +33 -0
- nonebot_plugin_clovers/adapters/qq.py +52 -0
- nonebot_plugin_clovers/adapters/v11.py +104 -0
- nonebot_plugin_clovers-0.1.0.dist-info/LICENSE +21 -0
- nonebot_plugin_clovers-0.1.0.dist-info/METADATA +116 -0
- nonebot_plugin_clovers-0.1.0.dist-info/RECORD +8 -0
- nonebot_plugin_clovers-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from nonebot import get_driver
|
|
5
|
+
from clovers.core.adapter import AdapterMethod
|
|
6
|
+
from clovers.core.plugin import PluginLoader
|
|
7
|
+
from .adapters.main import extract_command, new_adapter
|
|
8
|
+
|
|
9
|
+
# 加载配置
|
|
10
|
+
driver = get_driver()
|
|
11
|
+
global_config = driver.config
|
|
12
|
+
clovers_config_file = getattr(global_config, "clovers_config_file", "clovers.toml")
|
|
13
|
+
os.environ["clovers_config_file"] = clovers_config_file
|
|
14
|
+
|
|
15
|
+
# 添加环境变量之后加载config
|
|
16
|
+
from clovers.core.config import config as clovers_config
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# 加载clovers配置
|
|
20
|
+
class Config(BaseModel):
|
|
21
|
+
plugins_path: str = "./clovers_library"
|
|
22
|
+
plugins_list: list = []
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
config_key = __package__
|
|
26
|
+
config = Config.parse_obj(clovers_config.get(config_key, {}))
|
|
27
|
+
clovers_config[config_key] = config.dict()
|
|
28
|
+
clovers_config.save()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
plugins_path = Path(config.plugins_path)
|
|
32
|
+
plugins_path.mkdir(exist_ok=True, parents=True)
|
|
33
|
+
|
|
34
|
+
loader = PluginLoader(plugins_path, config.plugins_list)
|
|
35
|
+
adapter = new_adapter(loader.plugins)
|
|
36
|
+
|
|
37
|
+
driver.on_startup(adapter.startup)
|
|
38
|
+
|
|
39
|
+
from nonebot import on_message
|
|
40
|
+
from nonebot.matcher import Matcher
|
|
41
|
+
|
|
42
|
+
main = on_message(priority=50, block=True)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def add_response(Bot, Event, adapter_method: AdapterMethod, adapter_key: str):
|
|
46
|
+
adapter.methods[adapter_key] = adapter_method
|
|
47
|
+
|
|
48
|
+
@main.handle()
|
|
49
|
+
async def _(matcher: Matcher, bot: Bot, event: Event):
|
|
50
|
+
command = extract_command(event.get_plaintext())
|
|
51
|
+
if await adapter.response(adapter_key, command, bot=bot, event=event):
|
|
52
|
+
matcher.stop_propagation()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
from .adapters import qq
|
|
56
|
+
from nonebot.adapters.qq import Bot as QQBot, MessageEvent as QQMsgEvent
|
|
57
|
+
|
|
58
|
+
add_response(QQBot, QQMsgEvent, qq.initializer(main), "QQ")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
from .adapters import v11
|
|
62
|
+
from nonebot.adapters.onebot.v11 import Bot as v11Bot, MessageEvent as v11MsgEvent
|
|
63
|
+
|
|
64
|
+
add_response(v11Bot, v11MsgEvent, v11.initializer(main), "v11")
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from nonebot import get_driver
|
|
2
|
+
from clovers.core.plugin import Plugin
|
|
3
|
+
from clovers.core.adapter import Adapter
|
|
4
|
+
|
|
5
|
+
# 加载配置
|
|
6
|
+
driver = get_driver()
|
|
7
|
+
global_config = driver.config
|
|
8
|
+
|
|
9
|
+
command_start = {x for x in global_config.command_start if x}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def extract_command(msg: str):
|
|
13
|
+
for command in command_start:
|
|
14
|
+
if msg.startswith(command):
|
|
15
|
+
return msg[len(command) :]
|
|
16
|
+
return msg
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Bot_NICKNAME = list(global_config.nickname)
|
|
20
|
+
Bot_NICKNAME = Bot_NICKNAME[0] if Bot_NICKNAME else "bot"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def new_adapter(plugins: list[Plugin] | None = None):
|
|
24
|
+
"""创建新的adapter,包括注入插件和全局方法"""
|
|
25
|
+
adapter = Adapter()
|
|
26
|
+
if plugins:
|
|
27
|
+
adapter.plugins = plugins
|
|
28
|
+
|
|
29
|
+
@adapter.method.kwarg("Bot_Nickname")
|
|
30
|
+
async def _():
|
|
31
|
+
return Bot_NICKNAME
|
|
32
|
+
|
|
33
|
+
return adapter
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from io import BytesIO
|
|
2
|
+
from collections.abc import Callable, AsyncGenerator
|
|
3
|
+
from clovers.core.adapter import AdapterMethod
|
|
4
|
+
from clovers.core.plugin import Result
|
|
5
|
+
from nonebot.matcher import Matcher
|
|
6
|
+
from nonebot.adapters.qq import MessageEvent, Message, MessageSegment
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def initializer(main: type[Matcher]) -> AdapterMethod:
|
|
10
|
+
method = AdapterMethod()
|
|
11
|
+
|
|
12
|
+
@method.send("text")
|
|
13
|
+
async def _(message: str):
|
|
14
|
+
"""发送纯文本"""
|
|
15
|
+
await main.send(message)
|
|
16
|
+
|
|
17
|
+
@method.send("image_url")
|
|
18
|
+
async def _(message: str, main: type[Matcher]):
|
|
19
|
+
"""发送图片"""
|
|
20
|
+
await main.send(MessageSegment.image(message))
|
|
21
|
+
|
|
22
|
+
@method.send("image")
|
|
23
|
+
async def _(message: BytesIO, main: type[Matcher]):
|
|
24
|
+
"""发送图片"""
|
|
25
|
+
await main.send(MessageSegment.file_image(message))
|
|
26
|
+
|
|
27
|
+
@method.send("list")
|
|
28
|
+
async def _(message: list[Result], main: type[Matcher]):
|
|
29
|
+
"""发送图片文本混合信息"""
|
|
30
|
+
msg = Message()
|
|
31
|
+
for seg in message:
|
|
32
|
+
if seg.send_method == "text":
|
|
33
|
+
msg += seg.data
|
|
34
|
+
elif seg.send_method == "image":
|
|
35
|
+
msg += MessageSegment.image(seg.data)
|
|
36
|
+
await main.send(msg)
|
|
37
|
+
|
|
38
|
+
@method.send("segmented")
|
|
39
|
+
async def _(message: AsyncGenerator[Result, None]):
|
|
40
|
+
"""发送分段信息"""
|
|
41
|
+
async for seg in message:
|
|
42
|
+
await method.send_dict[seg.send_method](seg.data)
|
|
43
|
+
|
|
44
|
+
@method.kwarg("user_id")
|
|
45
|
+
async def _(event: MessageEvent):
|
|
46
|
+
return event.get_user_id()
|
|
47
|
+
|
|
48
|
+
@method.kwarg("group_id")
|
|
49
|
+
async def _(event: MessageEvent):
|
|
50
|
+
return getattr(event, "group_id", getattr(event, "guild_id", None))
|
|
51
|
+
|
|
52
|
+
return method
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from io import BytesIO
|
|
2
|
+
from collections.abc import Callable, Coroutine, AsyncGenerator
|
|
3
|
+
from clovers.core.adapter import AdapterMethod
|
|
4
|
+
from clovers.core.plugin import Result
|
|
5
|
+
from nonebot.matcher import Matcher
|
|
6
|
+
from nonebot.permission import SUPERUSER
|
|
7
|
+
from nonebot.adapters.onebot.v11 import (
|
|
8
|
+
Bot,
|
|
9
|
+
MessageEvent,
|
|
10
|
+
GroupMessageEvent,
|
|
11
|
+
Message,
|
|
12
|
+
MessageSegment,
|
|
13
|
+
GROUP_ADMIN,
|
|
14
|
+
GROUP_OWNER,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def initializer(main: type[Matcher]) -> AdapterMethod:
|
|
19
|
+
method = AdapterMethod()
|
|
20
|
+
|
|
21
|
+
@method.send("text")
|
|
22
|
+
async def _(message: str, send: Callable[..., Coroutine] = main.send):
|
|
23
|
+
"""发送纯文本"""
|
|
24
|
+
await send(message)
|
|
25
|
+
|
|
26
|
+
@method.send("image")
|
|
27
|
+
async def _(message: BytesIO, send: Callable[..., Coroutine] = main.send):
|
|
28
|
+
"""发送图片"""
|
|
29
|
+
await send(MessageSegment.image(message))
|
|
30
|
+
|
|
31
|
+
@method.send("list")
|
|
32
|
+
async def _(message: list[Result], send: Callable[..., Coroutine] = main.send):
|
|
33
|
+
"""发送图片文本混合信息"""
|
|
34
|
+
msg = Message()
|
|
35
|
+
for seg in message:
|
|
36
|
+
if seg.send_method == "text":
|
|
37
|
+
msg += seg.data
|
|
38
|
+
elif seg.send_method == "image":
|
|
39
|
+
msg += MessageSegment.image(seg.data)
|
|
40
|
+
await send(msg)
|
|
41
|
+
|
|
42
|
+
@method.send("segmented")
|
|
43
|
+
async def _(message: AsyncGenerator[Result, None], send: Callable[..., Coroutine] = main.send):
|
|
44
|
+
"""发送分段信息"""
|
|
45
|
+
async for seg in message:
|
|
46
|
+
await method.send_dict[seg.send_method](seg.data, send)
|
|
47
|
+
|
|
48
|
+
@method.kwarg("user_id")
|
|
49
|
+
async def _(event: MessageEvent):
|
|
50
|
+
return event.get_user_id()
|
|
51
|
+
|
|
52
|
+
@method.kwarg("group_id")
|
|
53
|
+
async def _(event: MessageEvent):
|
|
54
|
+
group_id = getattr(event, "group_id", None)
|
|
55
|
+
return str(group_id) if group_id else None
|
|
56
|
+
|
|
57
|
+
@method.kwarg("to_me")
|
|
58
|
+
async def _(event: MessageEvent):
|
|
59
|
+
return event.to_me
|
|
60
|
+
|
|
61
|
+
@method.kwarg("nickname")
|
|
62
|
+
async def _(event: MessageEvent):
|
|
63
|
+
return event.sender.card or event.sender.nickname
|
|
64
|
+
|
|
65
|
+
@method.kwarg("avatar")
|
|
66
|
+
async def _(event: MessageEvent) -> str:
|
|
67
|
+
return f"https://q1.qlogo.cn/g?b=qq&nk={event.user_id}&s=640"
|
|
68
|
+
|
|
69
|
+
@method.kwarg("group_avatar")
|
|
70
|
+
async def _(event: MessageEvent) -> str:
|
|
71
|
+
if isinstance(event, GroupMessageEvent):
|
|
72
|
+
return f"https://p.qlogo.cn/gh/{event.group_id}/{event.group_id}/640"
|
|
73
|
+
return ""
|
|
74
|
+
|
|
75
|
+
@method.kwarg("image_list")
|
|
76
|
+
async def _(event: MessageEvent):
|
|
77
|
+
url = [msg.data["url"] for msg in event.message if msg.type == "image"]
|
|
78
|
+
if event.reply:
|
|
79
|
+
url += [msg.data["url"] for msg in event.reply.message if msg.type == "image"]
|
|
80
|
+
return url
|
|
81
|
+
|
|
82
|
+
@method.kwarg("permission")
|
|
83
|
+
async def _(bot: Bot, event: MessageEvent) -> int:
|
|
84
|
+
if await SUPERUSER(bot, event):
|
|
85
|
+
return 3
|
|
86
|
+
if await GROUP_OWNER(bot, event):
|
|
87
|
+
return 2
|
|
88
|
+
if await GROUP_ADMIN(bot, event):
|
|
89
|
+
return 1
|
|
90
|
+
return 0
|
|
91
|
+
|
|
92
|
+
@method.kwarg("at")
|
|
93
|
+
async def _(event: MessageEvent) -> list[str]:
|
|
94
|
+
return [str(msg.data["qq"]) for msg in event.message if msg.type == "at"]
|
|
95
|
+
|
|
96
|
+
@method.kwarg("send_group_message")
|
|
97
|
+
async def _(bot: Bot) -> Callable[[int, Result], Coroutine]:
|
|
98
|
+
async def send_group_message(group_id: int, result: Result):
|
|
99
|
+
send = lambda message: bot.send_group_msg(group_id=group_id, message=message)
|
|
100
|
+
await method.send_dict[result.send_method](result.data, send)
|
|
101
|
+
|
|
102
|
+
return send_group_message
|
|
103
|
+
|
|
104
|
+
return method
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 KarisAya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: nonebot-plugin-clovers
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: KarisAya
|
|
6
|
+
Author-email: 1048827424@qq.com
|
|
7
|
+
Requires-Python: >=3.12,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Dist: clovers (>=0.1.1,<0.2.0)
|
|
10
|
+
Requires-Dist: nonebot-adapter-onebot (>=2.4.3,<3.0.0)
|
|
11
|
+
Requires-Dist: nonebot-adapter-qq (>=1.4.3,<2.0.0)
|
|
12
|
+
Requires-Dist: nonebot2 (>=2.2.1,<3.0.0)
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://v2.nonebot.dev/"><img src="https://v2.nonebot.dev/logo.png" width="200" height="200" alt="nonebot"></a>
|
|
17
|
+
</p>
|
|
18
|
+
<div align="center">
|
|
19
|
+
|
|
20
|
+
# nonebot_plugin_clovers
|
|
21
|
+
|
|
22
|
+
<img src="https://img.shields.io/badge/python-3.12+-blue.svg" alt="python">
|
|
23
|
+
<a href="./LICENSE">
|
|
24
|
+
<img src="https://img.shields.io/github/license/KarisAya/nonebot_plugin_clovers.svg" alt="license">
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://pypi.python.org/pypi/nonebot_plugin_clovers">
|
|
27
|
+
<img src="https://img.shields.io/pypi/v/nonebot_plugin_clovers.svg" alt="pypi">
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://pypi.python.org/pypi/nonebot_plugin_clovers">
|
|
30
|
+
<img src="https://img.shields.io/pypi/dm/nonebot_plugin_clovers" alt="pypi download">
|
|
31
|
+
</a>
|
|
32
|
+
</div>
|
|
33
|
+
|
|
34
|
+
## 💿 安装
|
|
35
|
+
|
|
36
|
+
<details open>
|
|
37
|
+
<summary>推荐使用 nb-cli 安装</summary>
|
|
38
|
+
在 nonebot2 项目的根目录下打开命令行, 输入以下指令即可安装
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
nb plugin install nonebot_plugin_clovers
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
</details>
|
|
45
|
+
|
|
46
|
+
<details>
|
|
47
|
+
<summary>使用包管理器安装</summary>
|
|
48
|
+
<details>
|
|
49
|
+
<summary>pip</summary>
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install nonebot_plugin_clovers
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
</details>
|
|
56
|
+
|
|
57
|
+
<details>
|
|
58
|
+
<summary>poetry</summary>
|
|
59
|
+
```bash
|
|
60
|
+
poetry add nonebot_plugin_clovers
|
|
61
|
+
```
|
|
62
|
+
</details>
|
|
63
|
+
|
|
64
|
+
打开 nonebot2 项目根目录下的 `pyproject.toml` 文件, 在 `[tool.nonebot]` 部分的 `plugins` 项里追加写入
|
|
65
|
+
|
|
66
|
+
```toml
|
|
67
|
+
[tool.nonebot]
|
|
68
|
+
plugins = [
|
|
69
|
+
# ...
|
|
70
|
+
"nonebot_plugin_clovers"
|
|
71
|
+
]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
</details>
|
|
75
|
+
|
|
76
|
+
## ⚙️ 配置
|
|
77
|
+
|
|
78
|
+
`clovers_config_file` clovers 框架配置文件路径
|
|
79
|
+
|
|
80
|
+
```properties
|
|
81
|
+
"clovers_config_file" = "clovers.toml"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
插件位置
|
|
85
|
+
|
|
86
|
+
## 🎉 使用
|
|
87
|
+
|
|
88
|
+
详见[clovers](https://github.com/KarisAya/clovers) 自定义的聊天平台异步机器人指令-响应插件框架
|
|
89
|
+
|
|
90
|
+
在你定义的 clovers 框架配置文件文件中添加下面的配置
|
|
91
|
+
|
|
92
|
+
```toml
|
|
93
|
+
[nonebot_plugin_clovers]
|
|
94
|
+
plugins_path = "./clovers_library"
|
|
95
|
+
plugins_list = []
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`plugins_path` 加载本地插件位置
|
|
99
|
+
`plugins_list` 加载插件列表
|
|
100
|
+
|
|
101
|
+
已完成的[适配器方法](https://github.com/KarisAya/nonebot_plugin_clovers/tree/master/nonebot_plugin_clovers/adapters)
|
|
102
|
+
|
|
103
|
+
## 📞 联系
|
|
104
|
+
|
|
105
|
+
如有建议,bug 反馈等可以加群
|
|
106
|
+
|
|
107
|
+
机器人 bug 研究中心(闲聊群) 744751179
|
|
108
|
+
|
|
109
|
+
永恒之城(测试群) 724024810
|
|
110
|
+
|
|
111
|
+

|
|
112
|
+
|
|
113
|
+
## 💡 鸣谢
|
|
114
|
+
|
|
115
|
+
- [nonebot2](https://github.com/nonebot/nonebot2) 跨平台 Python 异步聊天机器人框架
|
|
116
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
nonebot_plugin_clovers/__init__.py,sha256=u4t7-30df2nvxZSlTaY39iBAMSY0I-whuwQMbWhk_Ac,1946
|
|
2
|
+
nonebot_plugin_clovers/adapters/main.py,sha256=uiWlVlHZFMpTA37wO_cugGSo-D4jP_d2d-N8Qz3Dyv8,842
|
|
3
|
+
nonebot_plugin_clovers/adapters/qq.py,sha256=0GTo-OY0oZvu6YTR2eMKfO9j6RgGPurmR54Pwo_CuNA,1730
|
|
4
|
+
nonebot_plugin_clovers/adapters/v11.py,sha256=7drMfVxlTva1lTHhceOWrcQu34EUVn-Kf8ePAeQqpW0,3609
|
|
5
|
+
nonebot_plugin_clovers-0.1.0.dist-info/LICENSE,sha256=v2QDhv3Xo1gkhRe1DXym6RZe_E3gSOKFi3FkKbVCVWw,1086
|
|
6
|
+
nonebot_plugin_clovers-0.1.0.dist-info/METADATA,sha256=GRXioCPFc1ybSnzUQUSuVAGnXpJvmC7wzjmq9UhPEhU,2844
|
|
7
|
+
nonebot_plugin_clovers-0.1.0.dist-info/WHEEL,sha256=d2fvjOD7sXsVzChCqf0Ty0JbHKBaLYwDbGQDwQTnJ50,88
|
|
8
|
+
nonebot_plugin_clovers-0.1.0.dist-info/RECORD,,
|