Skip to content

Latest commit

 

History

History
256 lines (189 loc) · 8.06 KB

开发指引.md

File metadata and controls

256 lines (189 loc) · 8.06 KB

开发指引

目录结构

Adachi-BOT
├── app.js                      # 主程序
├── config
│   ├── command.yml             # 用户插件配置
│   ├── command_master.yml      # 管理员插件配置
│   └── setting.yml             # 基础配置
├── data
│   └── db                      # 数据库文件
├── resources                   # 资源目录
├── resources_custom
└── src
    ├── plugins                 # 插件
    ├── utils                   # 公共库
    └── views                   # HTML

插件开发

代码示例

下面的 Patch 演示了如何添加一个插件。

From afa00db289a688129c04e882465382fcdf03c49a Mon Sep 17 00:00:00 2001
From: Arondight <[email protected]>
Date: Mon, 25 Oct 2021 23:36:26 +0800
Subject: [PATCH] Hello World!

---
 config_defaults/command.yml      | 16 ++++++++++++++++
 src/plugins/hello_world/index.js | 13 +++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 src/plugins/hello_world/index.js

diff --git a/config_defaults/command.yml b/config_defaults/command.yml
index 14bef65..261286b 100644
--- a/config_defaults/command.yml
+++ b/config_defaults/command.yml
@@ -195,6 +195,22 @@ gacha:
       entrance:
         - ^取消定轨
 
+hello_world:
+  enable: true
+  weights: 10099
+  regex:
+    - ^hello\sworld$
+  functions:
+    hello_world:
+      type: command
+      show: true
+      weights: 9999
+      name: hello world
+      usage:
+      description: 向你致以诚挚的问候
+      entrance:
+        - hello world
+
 tools:
   enable: true
   weights: 99
diff --git a/src/plugins/hello_world/index.js b/src/plugins/hello_world/index.js
new file mode 100644
index 0000000..b7a16bd
--- /dev/null
+++ b/src/plugins/hello_world/index.js
@@ -0,0 +1,13 @@
+async function Plugin(Message, bot) {
+  const msg = Message.raw_message;        // 聊天消息
+  const userID = Message.user_id;         // 聊天发起人的 QQ 号
+  const groupID = Message.group_id;       // 群消息的群号
+  const type = Message.type;              // group 或者 private
+  const name = Message.sender.nickname;   // 聊天发起人的 QQ 昵称
+  const sendID = "group" === type ? groupID : userID;
+  const message = `Welcome to world, ${name} (${userID}) !`;
+
+  await bot.sendMessage(sendID, message, type, userID);
+}
+
+export { Plugin as run };
-- 
2.27.0

应用该 Patch 后,启动机器人,发送 QQ 聊天信息 hello world 则会得到回复 Welcome to world, <nickname> (<id>) !

  1. ../config_defaults/command.yml 中添加命令入口。
  2. ../src/plugins/ 目录下创建插件目录并编写代码。
  3. 如有需要,使用 ../src/utils/config.js 中的 hasEntrance 验证插件的各个功能入口。

全局变量

有以下几个全局变量包含了配置文件中的数据,可以在插件中直接使用。使用这些全局变量前确保仔细阅读../src/utils/config.js 中的注释,清楚地了解你要用的数据结构。

变量 数据
global.alias alias.yml
global.all command.ymlcommand_master.yml 的部分内容
global.artifacts artifacts.yml
global.command command.yml
global.config setting.ymlgreeting.yml
global.eggs pool_eggs.yml
global.master command_master.yml
global.rootdir 项目所在的目录

如果你使用了这些全局变量,确保文件头用类似以下的注释注明用到的全局变量,以避免 npm run check 将全局变量视为未声明的变量。

/* global rootdir, config */
/* eslint no-undef: "error" */

可能的 oicq 数据结构改变

当消息为一条命令时,插件接收的 oicq 消息的数据结构可能改变。原本的 CommonMessageEventData 中的 message 字段是一个可能包含这些类型的数组,但是为了统一 messageraw_message 字段,在 ../src/utils/load.js 中剔除了 TextElem 之外的所有类型并只保留一个 TextElem 。除此之外,将 raw_messageGroupMessageEventData 中仅存的 TextElem 进行了统一。最后 GroupMessageEventData 中新增了字段 atMe: boolean 来表示这条消息是否 @ 了机器人。

  1. 插件接收的 raw_messagemessage[0].data.text 依次去除了 @ 机器人的 CQ 码、命令前缀 config.prefixes 和行首空格。
  2. 建议只使用 raw_message

数据库

../src/utils/database.js 中使用 lodash 封装了 lowdb

API

导入

import db from "../../utils/database.js";

初始化

初始化名称为 name 的数据库。如果数据库已存在,加载其数据;如果数据库不存在,创建空数据库。

default 默认设置为 { user: [] }

db.init(name: string, default?: object);

../src/utils/init.js 中使用,以便启动时初始化。

键的存在性

在数据库 name 中判断 path 是否存在。

db.has(name: string, ...path);

对应 lodash.hasIn

值的存在性

在数据库 name 中的 key 对应的 Array 中,检测所有的索引 index 在是否包含值 value

db.includes(name: string, key: string, index: string, value: any);

对应 lodash.includes

获取数据

index 返回值
undefined key 对应的 Array
object key 对应的 Array 中,获取包含索引 index 的对象
db.get(name: string, key: string, index?: object);

对应 lodash.getlodash.find

写入数据

在数据库 name 中的 key 对应的 Array 中,插入一条数据 data 。此方法自动调用 db.write

db.push(name: string, key: string, data: object);

对应 lodash.push

更新数据

在数据库 name 中的 key 对应的 Array 中,将包含索引 index 的对象更新为 data 。此方法自动调用 db.write

只更新 data 中包含的 pair ,不会修改其他数据。

db.update(name: string, key: string, index: object, data: object);

对应 lodash.assign

设置数据

把数据库 name 中的 key 对应的数据设置为 data 。此方法自动调用 db.write

db.set(name: string, key: string, data: any);

对应 lodash.set

删除数据

在数据库 name 中的 key 对应的 Array 中,将包含索引 index 的对象删除 。此方法自动调用 db.write

db.remove(name: string, key: string, index: object);

对应 lodash.reject

写入磁盘

把数据库 name 的数据写入磁盘,一般无须手动调用

db.write(name: string);

示例

下面的代码演示了如何使用这些数据库 API 。

import db from "../../utils/database.js";

async function func () {
  /* some code */
  await db.init("info");
  await db.has("info", "user", 0, "uid");
  await db.includes("info", "user", uid);
  await db.get("info", "user", { uid });
  await db.get("info", "user");
  await db.push("time", "user", { uid, time: 0 });
  await db.update("music", "source", { ID: id }, { ...data, Source: source });
  await db.set("gacha", "data", [indefinite, character, weapon]);
  await db.remove("cookie", "uid", { cookie });
  await db.write("info");  // 几乎在任何情况下你都不需要手动调用 db.write
  /* some code */
}

注意这些 API 只提供异步版本,你在无法在一个非异步的环境中使用这些 API 。