Skip to content

Commit

Permalink
Update 插件开发.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter authored May 25, 2024
1 parent 6e4e563 commit 25ac6fb
Showing 1 changed file with 25 additions and 63 deletions.
88 changes: 25 additions & 63 deletions docs/开发/插件开发.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@

:::

为了方便管理,本项目的插件一律输出为 Git 仓库,**推荐在 https://github.com/Soulter/helloworld 这个模板仓库之上进行开发。**
# 如何开发

## 使用模板开发

为了方便管理,本项目的插件一律输出为 Git 仓库。

首先打开 [helloworld](https://github.com/Soulter/helloworld),这个是一个插件模板,接下来在这个模板上二次开发。

接下来点击右上角的 Use this template,然后点击 Create new repository。
![image](https://github.com/Soulter/AstrBot-docs/assets/37870767/7891edd8-3968-4cb8-aae3-b29b92aa1770)

然后在 Repository name 处输入你的插件名字,不要中文。建议以 `astrbot_plugin_` 开头,如 `astrbot_plugin_yuanshen`
![image](https://github.com/Soulter/AstrBot-docs/assets/37870767/560fa66f-740a-456c-a6dc-3766a79d3e9f)

创建好后,打开电脑上的 Visual Studio Code,然后将创建好的仓库 clone 下来。有关怎么 clone,请自行参考网上。

:::caution
强烈建议把模板仓库 fork 之后 clone 到机器人文件夹下的 addons/plugins/ 目录下,然后用 Pycharm/VSCode 等工具打开可获更棒的编程体验(自动补全等)。建议所有开发者都这样做,不然很难了解到一些数据结构。
:::

接下来打开 main.py,你就可以在这里面创建属于你的插件了!!!


# 插件格式

推荐在 https://github.com/Soulter/helloworld 这个模板仓库之上进行开发
> 这里我再叨叨一下插件的格式规范
- 仓库名:任意
- 必须含有 `main.py` 文件,且 `main.py` 中需要含有一个特定的类(下称“主类”)
Expand All @@ -20,11 +41,10 @@
> info函数里的插件名是您的插件的**唯一识别名**
# 开发后的插件如何使用?
- 首先需要将仓库上传到 Github。
- 然后使用指令 `plugin i 仓库地址` 进行导入。如 `plugin i https://github.com/Soulter/helloworld`
- 首先需要将仓库上传到 Github 或者 Gitee。有关怎么上传,请自行去网上搜索
- 上传好后使用指令 `plugin i 仓库地址` 进行导入。如 `plugin i https://github.com/Soulter/helloworld`
- 开发好的插件可以通过 issue 提交至此项目,我们会将其放至相应位置供用户安装。


# AstrBot API
目前,开放的 API 都放在了 `util.plugin_dev.api.v1` 包下,建议开发者前往查阅其内部的注释来更好地了解插件开发。

Expand All @@ -39,64 +59,6 @@ util.plugin_dev.api.v1
|- types.py # 插件类型。在补充 info 函数的时候可以用到
```


:::caution
把模板仓库 fork 之后 clone 到机器人文件夹下的 addons/plugins/ 目录下,然后用 Pycharm/VSCode 等工具打开可获更棒的编程体验(自动补全等)。建议所有开发者都这样做,不然很难了解到一些数据结构
:::


# 例子:helloworld插件

本插件当接收到消息为helloworld,则会返回`Hello World!!`

```python
from nakuru.entities.components import *
flag_not_support = False
try:
from util.plugin_dev.api.v1.config import *
from util.plugin_dev.api.v1.bot import *
from util.plugin_dev.api.v1.register import *
from util.plugin_dev.api.v1.message import *
except ImportError:
flag_not_support = True
print("导入接口失败。请升级到 AstrBot 最新版本。")


'''
注意改插件名噢!格式:XXXPlugin 或 Main
'''
class HelloWorldPlugin:
"""
初始化函数, 可以选择直接pass
"""
def __init__(self) -> None:
pass

"""
机器人程序在收到一条消息之后会调用此函数。
"""
def run(self, ame: AstrMessageEvent):
if ame.message_str == "helloworld":
return CommandResult(
hit=True,
success=True,
message_chain=[Plain("Hello World!!")],
command_name="helloworld"
)

"""
插件元信息。
"""
def info(self):
return PluginMetadata(
plugin_name="helloworld", # 插件名
plugin_type=PluginType.COMMON, # 插件类型
author="Soulter", # 插件作者
desc="测试插件", # 插件简介
version="v1.3", # 插件版本
)
```

# 数据结构定义

:::caution
Expand Down

0 comments on commit 25ac6fb

Please sign in to comment.