feat(plugin): 新增极简插件开发模式
新增 SimplePlugin 基类,提供面向新手的极简插件开发方式 添加相关示例代码和文档说明
This commit is contained in:
38
plugins/class_style_example.py
Normal file
38
plugins/class_style_example.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from core.plugin import Plugin, command, on_message
|
||||
from models.events.message import MessageEvent
|
||||
from core.permission import Permission
|
||||
|
||||
# 插件元信息
|
||||
__plugin_meta__ = {
|
||||
"name": "类风格插件示例",
|
||||
"description": "演示如何使用类风格编写插件",
|
||||
"usage": "/hello - 打招呼\n/echo <msg> - 复读消息",
|
||||
}
|
||||
|
||||
class MyPlugin(Plugin):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
# 可以在这里初始化一些状态
|
||||
self.count = 0
|
||||
|
||||
@command("hello")
|
||||
async def hello(self, event: MessageEvent, args: list[str]):
|
||||
self.count += 1
|
||||
await self.reply(event, f"Hello from class-based plugin! (Called {self.count} times)")
|
||||
|
||||
@command("echo", permission=Permission.USER)
|
||||
async def echo(self, event: MessageEvent, args: list[str]):
|
||||
if args:
|
||||
await self.reply(event, " ".join(args))
|
||||
else:
|
||||
await self.reply(event, "请输入要复读的内容。")
|
||||
|
||||
@on_message()
|
||||
async def handle_message(self, event: MessageEvent):
|
||||
# 这是一个通用的消息处理器,会处理所有消息
|
||||
# 注意:这可能会与命令冲突,通常需要过滤
|
||||
if "特定关键词" in event.raw_message:
|
||||
await self.reply(event, "检测到特定关键词!")
|
||||
|
||||
# 实例化插件以注册
|
||||
plugin = MyPlugin()
|
||||
41
plugins/simple_style_example.py
Normal file
41
plugins/simple_style_example.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from core.plugin import SimplePlugin
|
||||
from models.events.message import MessageEvent
|
||||
|
||||
# 插件元信息
|
||||
__plugin_meta__ = {
|
||||
"name": "极简插件示例",
|
||||
"description": "演示面向新手的极简插件写法",
|
||||
"usage": "/ping - 测试\n/add <a> <b> - 加法\n/greet <name> - 问候",
|
||||
}
|
||||
|
||||
class MySimplePlugin(SimplePlugin):
|
||||
|
||||
async def ping(self, event: MessageEvent):
|
||||
"""
|
||||
发送 /ping 即可调用
|
||||
"""
|
||||
return "Pong! (来自极简插件)"
|
||||
|
||||
async def greet(self, event: MessageEvent, name: str):
|
||||
"""
|
||||
发送 /greet Neo 即可调用
|
||||
"""
|
||||
return f"你好, {name}!"
|
||||
|
||||
async def add(self, event: MessageEvent, a: int, b: int):
|
||||
"""
|
||||
发送 /add 10 20 即可调用
|
||||
自动处理类型转换
|
||||
"""
|
||||
return f"{a} + {b} = {a + b}"
|
||||
|
||||
async def echo_all(self, event: MessageEvent, msg: str):
|
||||
"""
|
||||
只有一个参数时,会自动捕获所有剩余文本
|
||||
发送 /echo_all 这是一个 测试 消息
|
||||
msg 将会是 "这是一个 测试 消息"
|
||||
"""
|
||||
return f"复读: {msg}"
|
||||
|
||||
# 实例化插件以生效
|
||||
plugin = MySimplePlugin()
|
||||
Reference in New Issue
Block a user