Files
AstrBot/examples/hello_plugin
whatevertogo 84184c4c0a feat(errors): Enhance AstrBotError with detailed documentation and examples
feat(events): Expand MessageEvent with reply capabilities and detailed docstrings

fix(loader): Ensure plugin path is correctly managed in sys.path

feat(star): Improve Star class documentation and lifecycle method descriptions

feat(testing): Add plugin metadata handling in MockContext and enhance PluginHarness

feat(hello): Refactor HelloPlugin to utilize new reply methods and structured capabilities

test(decorators): Add tests for input/output model support in provide_capability

test(events): Implement tests for reply_image and reply_chain methods in MessageEvent

test(http): Validate API registration with capability handler references and error handling

test(tests): Enhance tests for plugin harness and directory handling in dev commands
2026-03-14 23:37:09 +08:00
..

Hello Plugin

这是给 AstrBot SDK 插件作者准备的最小示例。

目录结构

hello_plugin/
├── plugin.yaml
├── requirements.txt
├── main.py
└── tests
    └── test_plugin.py

能学到什么

  • 如何定义一个 Star 插件
  • 如何注册命令 handler
  • 如何使用 MessageEvent.reply()
  • 如何用 PluginHarness.from_plugin_dir() 走真实 dispatch 链
  • 如何从 Context 里读取当前插件元数据
  • 如何用 MockContext / MockMessageEvent 写插件测试

运行

在仓库根目录执行:

cd examples/hello_plugin
astrbot-sdk validate
astrbot-sdk dev --local --event-text hello
astrbot-sdk dev --local --watch --event-text hello

测试

python -m pytest examples/hello_plugin/tests/test_plugin.py -v

代码说明

  • hello: 最小命令,收到 hello 时回复 Hello, World!
  • about: 读取 ctx.metadata.get_current_plugin(),演示 capability 客户端的基础用法
  • tests/test_plugin.py: 展示 direct handler test
  • tests/test_dispatch.py: 展示 PluginHarness.from_plugin_dir() dispatch test