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>) !
。
- 在
../config_defaults/command.yml
中添加命令入口。- 在
../src/plugins/
目录下创建插件目录并编写代码。- 如有需要,使用
../src/utils/config.js
中的hasEntrance
验证插件的各个功能入口。
有以下几个全局变量包含了配置文件中的数据,可以在插件中直接使用。使用这些全局变量前确保仔细阅读了 ../src/utils/config.js
中的注释,清楚地了解你要用的数据结构。
变量 | 数据 |
---|---|
global.alias |
alias.yml |
global.all |
command.yml 和 command_master.yml 的部分内容 |
global.artifacts |
artifacts.yml |
global.command |
command.yml |
global.config |
setting.yml 、greeting.yml |
global.eggs |
pool_eggs.yml |
global.master |
command_master.yml |
global.rootdir |
项目所在的目录 |
如果你使用了这些全局变量,确保文件头用类似以下的注释注明用到的全局变量,以避免 npm run check
将全局变量视为未声明的变量。
/* global rootdir, config */
/* eslint no-undef: "error" */
当消息为一条命令时,插件接收的 oicq 消息的数据结构可能改变。原本的 CommonMessageEventData 中的 message
字段是一个可能包含这些类型的数组,但是为了统一 message
和 raw_message
字段,在 ../src/utils/load.js
中剔除了 TextElem 之外的所有类型并只保留一个 TextElem
。除此之外,将 raw_message
和 GroupMessageEventData 中仅存的 TextElem
进行了统一。最后 GroupMessageEventData
中新增了字段 atMe: boolean
来表示这条消息是否 @
了机器人。
- 插件接收的
raw_message
和message[0].data.text
依次去除了@
机器人的 CQ 码、命令前缀config.prefixes
和行首空格。- 建议只使用
raw_message
。
在 ../src/utils/database.js
中使用 lodash 封装了 lowdb 。
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
对应的 Arrayobject
key
对应的 Array 中,获取包含索引index
的对象
db.get(name: string, key: string, index?: object);
对应
lodash.get
和lodash.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 。