# 编写 mm Plugin 插件脚本

## 规则（Rules）

- index.js 位于 `app/{模块名}/plugin/{插件名}/index.js`
- 所有方法写在 `module.exports = { ... }` 对象内
- 生命周期方法：`_init`（初始化）、`install`（安装）、`uninstall`（卸载）、`start`（启动）、`stop`（停止）
- 主逻辑方法：`main`（主程序）、`cmd`（指令）、`chat`（聊天驱动）、`api`（API接口）

## 方法（Methods）

### 步骤1：编写基础骨架

```javascript
module.exports = {
    async _init(option) {
        var msg = null;
        return msg;
    },

    async install(option) {
        var msg = null;
        return msg;
    },

    async uninstall(option) {
        var msg = null;
        return msg;
    },

    async start(option) {
        var msg = null;
        return msg;
    },

    async stop(option) {
        var msg = null;
        return msg;
    },

    async main(param1, param2) {
        var ret = null;
        return ret;
    },

    cmd(content) {
        var ret = "";
        return ret;
    }
};
```

### 步骤2：添加初始化逻辑

_init 中注册本插件的 API：

```javascript
async _init(option) {
    var msg = null;
    var api = $.admin.api('插件名', '插件标题');
    await api.call('update', 'app/');
    return msg;
},
```

### 步骤3：添加安装/卸载逻辑

install 中创建默认配置，uninstall 中清理数据。

## 技巧（Tips）

- **chat 方法**用于机器人聊天驱动，接收 `from_user, to_user, content, group, type, msg_type, db` 参数
- **api 方法**用于其他插件调用本插件，接收 `ctx, db` 参数
- **配置读取**：通过 `this.options` 或 `$.admin.getOptions('app.plugin_name')` 读取在 plugin.json 中定义的 options
- **生命周期**：插件生命周期与 App 不同，方法名不带 `_` 前缀（除 `_init` 外）
