alemonjs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +340 -0
- package/README.md +54 -0
- package/lib/alemon/app.js +38 -0
- package/lib/alemon/apps.js +121 -0
- package/lib/alemon/buffer.js +38 -0
- package/lib/alemon/dealmsg.js +423 -0
- package/lib/alemon/dialogue.js +61 -0
- package/lib/alemon/index.js +40 -0
- package/lib/alemon/message.js +27 -0
- package/lib/alemon/plugin.js +44 -0
- package/lib/alemon/puppeteer.js +226 -0
- package/lib/alemon/qrcode.js +66 -0
- package/lib/alemon/screenshot.js +126 -0
- package/lib/alemon/typings.js +96 -0
- package/lib/config.js +25 -0
- package/lib/discord/alemon/api.js +44 -0
- package/lib/discord/alemon/bot.js +11 -0
- package/lib/discord/alemon/conversation.js +7 -0
- package/lib/discord/alemon/message/PUBLIC_GUILD_MESSAGES.js +185 -0
- package/lib/discord/alemon/segment.js +62 -0
- package/lib/discord/index.js +50 -0
- package/lib/discord/login.js +99 -0
- package/lib/index.js +12 -0
- package/lib/kook/alemon/bot.js +11 -0
- package/lib/kook/alemon/conversation.js +56 -0
- package/lib/kook/alemon/message/PUBLIC_GUILD_MESSAGES.js +195 -0
- package/lib/kook/alemon/segment.js +90 -0
- package/lib/kook/index.js +39 -0
- package/lib/kook/login.js +53 -0
- package/lib/login.js +112 -0
- package/lib/main.js +106 -0
- package/lib/ntqq/alemon/bot.js +11 -0
- package/lib/ntqq/alemon/conversation.js +18 -0
- package/lib/ntqq/alemon/message/C2C_MESSAGE_CREATE.js +142 -0
- package/lib/ntqq/alemon/message/GROUP_AT_MESSAGE_CREATE.js +218 -0
- package/lib/ntqq/alemon/segment.js +29 -0
- package/lib/ntqq/alemon/types.js +1 -0
- package/lib/ntqq/index.js +77 -0
- package/lib/ntqq/login.js +74 -0
- package/lib/ntqq/sdk/api.js +158 -0
- package/lib/ntqq/sdk/config.js +46 -0
- package/lib/ntqq/sdk/index.js +5 -0
- package/lib/ntqq/sdk/intents.js +69 -0
- package/lib/ntqq/sdk/types.js +1 -0
- package/lib/ntqq/sdk/typings.js +9 -0
- package/lib/ntqq/sdk/web/client.js +89 -0
- package/lib/ntqq/sdk/web/img.js +76 -0
- package/lib/ntqq/sdk/web/index.js +4 -0
- package/lib/ntqq/sdk/web/ip.js +24 -0
- package/lib/ntqq/sdk/wss.js +178 -0
- package/lib/qq/alemon/apidoc.js +216 -0
- package/lib/qq/alemon/bot.js +11 -0
- package/lib/qq/alemon/console.js +13 -0
- package/lib/qq/alemon/conversation.js +139 -0
- package/lib/qq/alemon/doc.js +93 -0
- package/lib/qq/alemon/e.js +41 -0
- package/lib/qq/alemon/message/AUDIO_ACTION.js +57 -0
- package/lib/qq/alemon/message/DIRECT_MESSAGE.js +202 -0
- package/lib/qq/alemon/message/FORUMS_EVENT.js +66 -0
- package/lib/qq/alemon/message/GUILDS.js +65 -0
- package/lib/qq/alemon/message/GUILD_MEMBERS.js +153 -0
- package/lib/qq/alemon/message/GUILD_MESSAGES.js +50 -0
- package/lib/qq/alemon/message/GUILD_MESSAGE_REACTIONS.js +37 -0
- package/lib/qq/alemon/message/INTERACTION.js +36 -0
- package/lib/qq/alemon/message/MESSAGE.js +303 -0
- package/lib/qq/alemon/message/MESSAGE_AUDIT.js +41 -0
- package/lib/qq/alemon/message/OPEN_FORUMS_EVENT.js +74 -0
- package/lib/qq/alemon/message/PUBLIC_GUILD_MESSAGES.js +52 -0
- package/lib/qq/alemon/permissions.js +43 -0
- package/lib/qq/alemon/privatechat.js +78 -0
- package/lib/qq/alemon/segment.js +25 -0
- package/lib/qq/alemon/types.js +25 -0
- package/lib/qq/index.js +44 -0
- package/lib/qq/login.js +151 -0
- package/lib/types.js +5 -0
- package/lib/villa/alemon/conversation.js +48 -0
- package/lib/villa/alemon/message/GUILDS.js +124 -0
- package/lib/villa/alemon/message/GUILD_MEMBERS.js +128 -0
- package/lib/villa/alemon/message/GUILD_MESSAGE_REACTIONS.js +127 -0
- package/lib/villa/alemon/message/MESSAGES.js +334 -0
- package/lib/villa/alemon/message/MESSAGE_AUDIT.js +128 -0
- package/lib/villa/alemon/message/e.js +41 -0
- package/lib/villa/alemon/segment.js +34 -0
- package/lib/villa/index.js +46 -0
- package/lib/villa/login.js +62 -0
- package/package.json +63 -0
- package/types/alemon/app.d.ts +22 -0
- package/types/alemon/apps.d.ts +41 -0
- package/types/alemon/buffer.d.ts +13 -0
- package/types/alemon/dealmsg.d.ts +41 -0
- package/types/alemon/dialogue.d.ts +55 -0
- package/types/alemon/index.d.ts +40 -0
- package/types/alemon/message.d.ts +17 -0
- package/types/alemon/plugin.d.ts +69 -0
- package/types/alemon/puppeteer.d.ts +53 -0
- package/types/alemon/qrcode.d.ts +20 -0
- package/types/alemon/screenshot.d.ts +23 -0
- package/types/alemon/typings.d.ts +525 -0
- package/types/config.d.ts +12 -0
- package/types/discord/alemon/api.d.ts +11 -0
- package/types/discord/alemon/bot.d.ts +6 -0
- package/types/discord/alemon/conversation.d.ts +6 -0
- package/types/discord/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +7 -0
- package/types/discord/alemon/segment.d.ts +46 -0
- package/types/discord/index.d.ts +1 -0
- package/types/discord/login.d.ts +7 -0
- package/types/index.d.ts +6 -0
- package/types/kook/alemon/bot.d.ts +8 -0
- package/types/kook/alemon/conversation.d.ts +7 -0
- package/types/kook/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +6 -0
- package/types/kook/alemon/segment.d.ts +66 -0
- package/types/kook/index.d.ts +5 -0
- package/types/kook/login.d.ts +7 -0
- package/types/login.d.ts +18 -0
- package/types/main.d.ts +15 -0
- package/types/ntqq/alemon/bot.d.ts +8 -0
- package/types/ntqq/alemon/conversation.d.ts +8 -0
- package/types/ntqq/alemon/message/C2C_MESSAGE_CREATE.d.ts +2 -0
- package/types/ntqq/alemon/message/GROUP_AT_MESSAGE_CREATE.d.ts +8 -0
- package/types/ntqq/alemon/segment.d.ts +20 -0
- package/types/ntqq/alemon/types.d.ts +11 -0
- package/types/ntqq/index.d.ts +1 -0
- package/types/ntqq/login.d.ts +6 -0
- package/types/ntqq/sdk/api.d.ts +44 -0
- package/types/ntqq/sdk/config.d.ts +23 -0
- package/types/ntqq/sdk/index.d.ts +5 -0
- package/types/ntqq/sdk/intents.d.ts +2 -0
- package/types/ntqq/sdk/types.d.ts +11 -0
- package/types/ntqq/sdk/typings.d.ts +17 -0
- package/types/ntqq/sdk/web/client.d.ts +8 -0
- package/types/ntqq/sdk/web/img.d.ts +20 -0
- package/types/ntqq/sdk/web/index.d.ts +4 -0
- package/types/ntqq/sdk/web/ip.d.ts +5 -0
- package/types/ntqq/sdk/wss.d.ts +35 -0
- package/types/qq/alemon/apidoc.d.ts +154 -0
- package/types/qq/alemon/bot.d.ts +8 -0
- package/types/qq/alemon/console.d.ts +1 -0
- package/types/qq/alemon/conversation.d.ts +5 -0
- package/types/qq/alemon/doc.d.ts +93 -0
- package/types/qq/alemon/e.d.ts +38 -0
- package/types/qq/alemon/message/AUDIO_ACTION.d.ts +15 -0
- package/types/qq/alemon/message/DIRECT_MESSAGE.d.ts +16 -0
- package/types/qq/alemon/message/FORUMS_EVENT.d.ts +25 -0
- package/types/qq/alemon/message/GUILDS.d.ts +19 -0
- package/types/qq/alemon/message/GUILD_MEMBERS.d.ts +11 -0
- package/types/qq/alemon/message/GUILD_MESSAGES.d.ts +12 -0
- package/types/qq/alemon/message/GUILD_MESSAGE_REACTIONS.d.ts +9 -0
- package/types/qq/alemon/message/INTERACTION.d.ts +8 -0
- package/types/qq/alemon/message/MESSAGE.d.ts +16 -0
- package/types/qq/alemon/message/MESSAGE_AUDIT.d.ts +9 -0
- package/types/qq/alemon/message/OPEN_FORUMS_EVENT.d.ts +28 -0
- package/types/qq/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +12 -0
- package/types/qq/alemon/permissions.d.ts +29 -0
- package/types/qq/alemon/privatechat.d.ts +14 -0
- package/types/qq/alemon/segment.d.ts +19 -0
- package/types/qq/alemon/types.d.ts +82 -0
- package/types/qq/index.d.ts +6 -0
- package/types/qq/login.d.ts +6 -0
- package/types/types.d.ts +85 -0
- package/types/villa/alemon/conversation.d.ts +7 -0
- package/types/villa/alemon/message/GUILDS.d.ts +10 -0
- package/types/villa/alemon/message/GUILD_MEMBERS.d.ts +14 -0
- package/types/villa/alemon/message/GUILD_MESSAGE_REACTIONS.d.ts +7 -0
- package/types/villa/alemon/message/MESSAGES.d.ts +7 -0
- package/types/villa/alemon/message/MESSAGE_AUDIT.d.ts +10 -0
- package/types/villa/alemon/message/e.d.ts +37 -0
- package/types/villa/alemon/segment.d.ts +26 -0
- package/types/villa/index.d.ts +1 -0
- package/types/villa/login.d.ts +7 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { writeFile } from 'fs/promises';
|
|
3
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync } from 'fs';
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
import { getMessage } from './message.js';
|
|
6
|
+
import { getApp, delApp, getAppKey } from './app.js';
|
|
7
|
+
import { EventEnum } from './typings.js';
|
|
8
|
+
import { conversationHandlers, getConversationState } from './dialogue.js';
|
|
9
|
+
/**
|
|
10
|
+
* 指令合集
|
|
11
|
+
*/
|
|
12
|
+
const Command = {};
|
|
13
|
+
/**
|
|
14
|
+
* 指令合集
|
|
15
|
+
*/
|
|
16
|
+
const CommandNotR = {};
|
|
17
|
+
/**
|
|
18
|
+
* plugins插件集合
|
|
19
|
+
*/
|
|
20
|
+
let PluginsArr = [];
|
|
21
|
+
/**
|
|
22
|
+
* 指令json
|
|
23
|
+
*/
|
|
24
|
+
const plugins = {};
|
|
25
|
+
/**
|
|
26
|
+
* 默认执行地址
|
|
27
|
+
*/
|
|
28
|
+
const route = '/help';
|
|
29
|
+
/**
|
|
30
|
+
* 执行文件
|
|
31
|
+
*/
|
|
32
|
+
let addressMenu = join(process.cwd(), route);
|
|
33
|
+
/**
|
|
34
|
+
* 大正则
|
|
35
|
+
*/
|
|
36
|
+
let mergedRegex;
|
|
37
|
+
/**
|
|
38
|
+
* 设置指令json地址
|
|
39
|
+
* @param rt '/help'
|
|
40
|
+
*/
|
|
41
|
+
export function setAppsHelp(rt = route) {
|
|
42
|
+
addressMenu = join(process.cwd(), rt);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 得到机器人帮助
|
|
46
|
+
* @param AppName
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
export function getPluginHelp(AppName) {
|
|
50
|
+
const basePath = join(addressMenu, `${AppName}.json`);
|
|
51
|
+
return JSON.parse(readFileSync(basePath, 'utf8'));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 创建机器人帮助
|
|
55
|
+
*/
|
|
56
|
+
function createPluginHelp() {
|
|
57
|
+
// 不存在
|
|
58
|
+
if (!existsSync(addressMenu))
|
|
59
|
+
mkdirSync(addressMenu, { recursive: true });
|
|
60
|
+
// 创建help
|
|
61
|
+
for (const item in plugins) {
|
|
62
|
+
const basePath = join(addressMenu, `${item}.json`);
|
|
63
|
+
const jsonData = JSON.stringify(plugins[item], null, 2);
|
|
64
|
+
// 异步创建避免阻塞
|
|
65
|
+
writeFile(basePath, jsonData, 'utf-8');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 应用挂载
|
|
70
|
+
* @param AppsObj
|
|
71
|
+
* @param appname
|
|
72
|
+
* @param belong
|
|
73
|
+
*/
|
|
74
|
+
async function synthesis(AppsObj, appname, belong) {
|
|
75
|
+
// 没有记载
|
|
76
|
+
if (!plugins[appname]) {
|
|
77
|
+
plugins[appname] = [];
|
|
78
|
+
}
|
|
79
|
+
for (const item in AppsObj) {
|
|
80
|
+
const keys = new AppsObj[item]();
|
|
81
|
+
// 控制类型
|
|
82
|
+
const eventType = keys['eventType'] ?? 'CREATE';
|
|
83
|
+
/**
|
|
84
|
+
* 不合法
|
|
85
|
+
*/
|
|
86
|
+
if (!keys['rule'] ||
|
|
87
|
+
!Array.isArray(keys['rule']) ||
|
|
88
|
+
keys['rule'].length == 0) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 指令不存在
|
|
93
|
+
*/
|
|
94
|
+
for await (const key of keys['rule']) {
|
|
95
|
+
if (!key['fnc'] ||
|
|
96
|
+
!key['reg'] ||
|
|
97
|
+
typeof keys[key['fnc']] !== 'function') {
|
|
98
|
+
/**
|
|
99
|
+
* 函数指定不存在,正则不存在
|
|
100
|
+
* 得到的不是函数
|
|
101
|
+
*/
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
// 先看指令优先级,没有就看类优先级,再没有则默认优先级
|
|
105
|
+
const priority = key['priority'] ?? keys['priority'] ?? 9000;
|
|
106
|
+
// 得到函数名
|
|
107
|
+
const fncName = key['fnc'];
|
|
108
|
+
// 得到函数
|
|
109
|
+
const fnc = keys[fncName];
|
|
110
|
+
const doc = key['doc'] ?? '';
|
|
111
|
+
const dsc = key['dsc'] ?? '';
|
|
112
|
+
// 如果类型正确
|
|
113
|
+
if (typeof key['reg'] === 'string' || key['reg'] instanceof RegExp) {
|
|
114
|
+
// 存在正则就必须是MESSAGES
|
|
115
|
+
const event = 'MESSAGES';
|
|
116
|
+
// 得到解析
|
|
117
|
+
const reg = key['reg'];
|
|
118
|
+
// 推送
|
|
119
|
+
plugins[appname].push({
|
|
120
|
+
event: event,
|
|
121
|
+
eventType: eventType,
|
|
122
|
+
reg: String(reg),
|
|
123
|
+
dsc,
|
|
124
|
+
doc,
|
|
125
|
+
priority
|
|
126
|
+
});
|
|
127
|
+
// 保存
|
|
128
|
+
Command[event].push({
|
|
129
|
+
belong,
|
|
130
|
+
event: event,
|
|
131
|
+
eventType: eventType,
|
|
132
|
+
reg: new RegExp(reg),
|
|
133
|
+
priority,
|
|
134
|
+
fncName,
|
|
135
|
+
fnc,
|
|
136
|
+
AppName: appname
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// 控制消息 -- 类型必须要存在的
|
|
141
|
+
const event = keys['event'];
|
|
142
|
+
// 推送
|
|
143
|
+
plugins[appname].push({
|
|
144
|
+
event: event,
|
|
145
|
+
eventType: eventType,
|
|
146
|
+
dsc,
|
|
147
|
+
doc,
|
|
148
|
+
priority
|
|
149
|
+
});
|
|
150
|
+
// 保存
|
|
151
|
+
CommandNotR[event].push({
|
|
152
|
+
belong,
|
|
153
|
+
event: event,
|
|
154
|
+
eventType: eventType,
|
|
155
|
+
priority,
|
|
156
|
+
fncName,
|
|
157
|
+
fnc,
|
|
158
|
+
AppName: appname
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 加载应用插件
|
|
167
|
+
* @param dir
|
|
168
|
+
*/
|
|
169
|
+
async function loadPlugins(dir) {
|
|
170
|
+
if (!existsSync(dir))
|
|
171
|
+
mkdirSync(dir, { recursive: true });
|
|
172
|
+
const flies = readdirSync(dir);
|
|
173
|
+
/**
|
|
174
|
+
* 识别并执行插件
|
|
175
|
+
*/
|
|
176
|
+
for await (const appname of flies) {
|
|
177
|
+
if (existsSync(`${dir}/${appname}/index.ts`)) {
|
|
178
|
+
/**
|
|
179
|
+
* 优先考虑ts
|
|
180
|
+
*/
|
|
181
|
+
await import(`file://${dir}/${appname}/index.ts`).catch(err => {
|
|
182
|
+
console.error(`file://${dir}/${appname}/index.ts`);
|
|
183
|
+
console.error(err);
|
|
184
|
+
process.exit();
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
else if (existsSync(`${dir}/${appname}/index.js`)) {
|
|
188
|
+
/**
|
|
189
|
+
* 允许js写法
|
|
190
|
+
*/
|
|
191
|
+
await import(`file://${dir}/${appname}/index.js`).catch(error => {
|
|
192
|
+
console.error(`file://${dir}/${appname}/index.js`);
|
|
193
|
+
console.error(error);
|
|
194
|
+
process.exit();
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 初始化应用
|
|
202
|
+
*/
|
|
203
|
+
function dataInit() {
|
|
204
|
+
PluginsArr = [];
|
|
205
|
+
for (const item of EventEnum) {
|
|
206
|
+
if (isNaN(Number(item))) {
|
|
207
|
+
Command[item] = [];
|
|
208
|
+
CommandNotR[item] = [];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 应用初始化
|
|
215
|
+
* @returns
|
|
216
|
+
*/
|
|
217
|
+
export async function appsInit() {
|
|
218
|
+
/**
|
|
219
|
+
* 清空当前的apps
|
|
220
|
+
*/
|
|
221
|
+
dataInit();
|
|
222
|
+
/**
|
|
223
|
+
* 得到所有插件名
|
|
224
|
+
*/
|
|
225
|
+
const APPARR = getAppKey();
|
|
226
|
+
/**
|
|
227
|
+
* 导出所有插件名
|
|
228
|
+
*/
|
|
229
|
+
for await (const item of APPARR) {
|
|
230
|
+
/**
|
|
231
|
+
* 获取插件集
|
|
232
|
+
*/
|
|
233
|
+
const apps = getApp(item);
|
|
234
|
+
/**
|
|
235
|
+
* 分析插件集
|
|
236
|
+
*/
|
|
237
|
+
await synthesis(apps, item, 'plugins');
|
|
238
|
+
/**
|
|
239
|
+
* 记录该插件
|
|
240
|
+
*/
|
|
241
|
+
PluginsArr.push(item);
|
|
242
|
+
/**
|
|
243
|
+
* 删除指集
|
|
244
|
+
*/
|
|
245
|
+
delApp(item);
|
|
246
|
+
}
|
|
247
|
+
/***
|
|
248
|
+
* 排序之后把所有正则变成一条正则
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* 排序
|
|
252
|
+
*/
|
|
253
|
+
for (const val in Command) {
|
|
254
|
+
Command[val] = lodash.orderBy(Command[val], ['priority'], ['asc']);
|
|
255
|
+
}
|
|
256
|
+
const mergedRegexArr = [];
|
|
257
|
+
for (const val in Command) {
|
|
258
|
+
for (const data of Command[val]) {
|
|
259
|
+
if (data.reg !== undefined && data.eventType !== undefined) {
|
|
260
|
+
mergedRegexArr.push(data.reg);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// 机器人整体指令正则
|
|
265
|
+
mergedRegex = new RegExp(mergedRegexArr.map(regex => regex.source).join('|'));
|
|
266
|
+
/**
|
|
267
|
+
* 排序
|
|
268
|
+
*/
|
|
269
|
+
for (const val in CommandNotR) {
|
|
270
|
+
CommandNotR[val] = lodash.orderBy(CommandNotR[val], ['priority'], ['asc']);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 生成指令json
|
|
274
|
+
*/
|
|
275
|
+
createPluginHelp();
|
|
276
|
+
/**
|
|
277
|
+
* 打印
|
|
278
|
+
*/
|
|
279
|
+
console.info(`[LOAD] apps*${PluginsArr.length} `);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
export function getMergedRegex() {
|
|
283
|
+
return mergedRegex;
|
|
284
|
+
}
|
|
285
|
+
let appDir = '/application';
|
|
286
|
+
export function getAppDir() {
|
|
287
|
+
return appDir;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* 初始化应用 mount = ture 则直接应用
|
|
291
|
+
* @param param0 { mount = false, address = '/application' }
|
|
292
|
+
* @returns
|
|
293
|
+
*/
|
|
294
|
+
export async function loadInit(val = { mount: false, address: '/application' }) {
|
|
295
|
+
const { mount, address } = val;
|
|
296
|
+
appDir = address ?? '/application';
|
|
297
|
+
/**
|
|
298
|
+
* 加载插件
|
|
299
|
+
*/
|
|
300
|
+
await loadPlugins(join(process.cwd(), address ?? '/application'));
|
|
301
|
+
/**
|
|
302
|
+
* 取消集成
|
|
303
|
+
*/
|
|
304
|
+
if (mount)
|
|
305
|
+
return;
|
|
306
|
+
/**
|
|
307
|
+
* 开始集成
|
|
308
|
+
*/
|
|
309
|
+
await appsInit();
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* 指令匹配
|
|
314
|
+
* @param e
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
export async function InstructionMatching(e) {
|
|
318
|
+
/**
|
|
319
|
+
* 获取对话状态
|
|
320
|
+
*/
|
|
321
|
+
const state = await getConversationState(e.user_id);
|
|
322
|
+
/**
|
|
323
|
+
* 获取对话处理函数
|
|
324
|
+
*/
|
|
325
|
+
const handler = conversationHandlers.get(e.user_id);
|
|
326
|
+
if (handler && state) {
|
|
327
|
+
/**
|
|
328
|
+
* 如果用户处于对话状态
|
|
329
|
+
* 则调用对话处理函数
|
|
330
|
+
*/
|
|
331
|
+
await handler(e, state);
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* 撤回事件 匹配不到事件 或者 大正则不匹配
|
|
336
|
+
*/
|
|
337
|
+
if (e.isRecall || !Command[e.event] || !mergedRegex.test(e.msg))
|
|
338
|
+
return true;
|
|
339
|
+
/**
|
|
340
|
+
* 循环所有指令 用 awwat确保指令顺序
|
|
341
|
+
*/
|
|
342
|
+
for await (const data of Command[e.event]) {
|
|
343
|
+
if (e.eventType != data.eventType ||
|
|
344
|
+
data.reg === undefined ||
|
|
345
|
+
!data.reg.test(e.msg)) {
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const AppFnc = getMessage(data.AppName);
|
|
349
|
+
try {
|
|
350
|
+
if (typeof AppFnc == 'function')
|
|
351
|
+
e = AppFnc(e);
|
|
352
|
+
const res = await data
|
|
353
|
+
.fnc(e)
|
|
354
|
+
.then((res) => {
|
|
355
|
+
console.info(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${true}]`);
|
|
356
|
+
return res;
|
|
357
|
+
})
|
|
358
|
+
.catch((err) => {
|
|
359
|
+
console.error(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${err}]`);
|
|
360
|
+
console.error(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${false}]`);
|
|
361
|
+
return false;
|
|
362
|
+
});
|
|
363
|
+
// 不是false都直接中断匹配
|
|
364
|
+
if (res !== false) {
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
catch (err) {
|
|
369
|
+
logErr(err, data);
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 不匹配指令的方法
|
|
377
|
+
* 只用匹配类型函数
|
|
378
|
+
* @param e
|
|
379
|
+
* @returns
|
|
380
|
+
*/
|
|
381
|
+
export async function typeMessage(e) {
|
|
382
|
+
if (!CommandNotR[e.event])
|
|
383
|
+
return true;
|
|
384
|
+
// 循环查找
|
|
385
|
+
for (const data of CommandNotR[e.event]) {
|
|
386
|
+
if (e.eventType != data.eventType)
|
|
387
|
+
continue;
|
|
388
|
+
try {
|
|
389
|
+
const AppFnc = getMessage(data.AppName);
|
|
390
|
+
if (typeof AppFnc == 'function')
|
|
391
|
+
e = AppFnc(e);
|
|
392
|
+
const res = await data
|
|
393
|
+
.fnc(e)
|
|
394
|
+
.then((res) => {
|
|
395
|
+
console.info(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${true}]`);
|
|
396
|
+
return res;
|
|
397
|
+
})
|
|
398
|
+
.catch((err) => {
|
|
399
|
+
console.error(err);
|
|
400
|
+
console.error(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${false}]`);
|
|
401
|
+
return false;
|
|
402
|
+
});
|
|
403
|
+
if (res) {
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
logErr(err, data);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* 错误信息反馈
|
|
416
|
+
* @param err
|
|
417
|
+
* @param data
|
|
418
|
+
*/
|
|
419
|
+
function logErr(err, data) {
|
|
420
|
+
console.error(err);
|
|
421
|
+
console.error(`\n[${data.event}][${data.belong}][${data.AppName}][${data.fncName}][${false}]`);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
*/
|
|
4
|
+
const Sockes = {};
|
|
5
|
+
/**
|
|
6
|
+
* 得到对话
|
|
7
|
+
* @param key
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
const getAsync = async (key) => {
|
|
11
|
+
return Sockes[key];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* 设置对话
|
|
15
|
+
* @param key
|
|
16
|
+
* @param val
|
|
17
|
+
*/
|
|
18
|
+
const setAsync = async (key, val) => {
|
|
19
|
+
Sockes[key] = val;
|
|
20
|
+
return;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 删除对话
|
|
24
|
+
* @param key
|
|
25
|
+
*/
|
|
26
|
+
const delAsync = async (key) => {
|
|
27
|
+
delete Sockes[key];
|
|
28
|
+
return;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* 注册对话处理器
|
|
32
|
+
*/
|
|
33
|
+
export const conversationHandlers = new Map();
|
|
34
|
+
/**
|
|
35
|
+
* 获取对话状态
|
|
36
|
+
* @param userId 根据id获取
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
export const getConversationState = async (userId) => {
|
|
40
|
+
const state = await getAsync(`conversation-state:${userId}`);
|
|
41
|
+
return state ? JSON.parse(state) : null;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* 保存对话状态
|
|
45
|
+
* @param userId
|
|
46
|
+
* @param state
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
export const setConversationState = async (userId, state) => {
|
|
50
|
+
await setAsync(`conversation-state:${userId}`, JSON.stringify(state));
|
|
51
|
+
return;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* 删除对话状态
|
|
55
|
+
* @param userId
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
export const deleteConversationState = async (userId) => {
|
|
59
|
+
await delAsync(`conversation-state:${userId}`);
|
|
60
|
+
return;
|
|
61
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件
|
|
3
|
+
*/
|
|
4
|
+
export * from './plugin.js';
|
|
5
|
+
/**
|
|
6
|
+
* 应用
|
|
7
|
+
*/
|
|
8
|
+
export * from './apps.js';
|
|
9
|
+
/**
|
|
10
|
+
* 消息
|
|
11
|
+
*/
|
|
12
|
+
export * from './message.js';
|
|
13
|
+
/**
|
|
14
|
+
* 对话机
|
|
15
|
+
*/
|
|
16
|
+
export * from './dialogue.js';
|
|
17
|
+
/**
|
|
18
|
+
* 接口
|
|
19
|
+
*/
|
|
20
|
+
export * from './qrcode.js';
|
|
21
|
+
/**
|
|
22
|
+
* 模块加载
|
|
23
|
+
*/
|
|
24
|
+
export * from './dealmsg.js';
|
|
25
|
+
/**
|
|
26
|
+
* 浏览器
|
|
27
|
+
*/
|
|
28
|
+
export * from './puppeteer.js';
|
|
29
|
+
/**
|
|
30
|
+
* 截图
|
|
31
|
+
*/
|
|
32
|
+
export * from './screenshot.js';
|
|
33
|
+
/**
|
|
34
|
+
* 类型
|
|
35
|
+
*/
|
|
36
|
+
export * from './typings.js';
|
|
37
|
+
/**
|
|
38
|
+
* 二进制
|
|
39
|
+
*/
|
|
40
|
+
export * from './buffer.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// 消息控制
|
|
2
|
+
const MSG = {};
|
|
3
|
+
/**
|
|
4
|
+
* 得到消息
|
|
5
|
+
* @param key 插件名
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export function getMessage(key) {
|
|
9
|
+
return MSG[key];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 设置消息
|
|
13
|
+
* @param key 插件名
|
|
14
|
+
* @param fnc 方法
|
|
15
|
+
*/
|
|
16
|
+
export function setMessage(key, fnc) {
|
|
17
|
+
MSG[key] = fnc;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 删除消息
|
|
22
|
+
* @param key 插件名
|
|
23
|
+
*/
|
|
24
|
+
export function delMessage(key) {
|
|
25
|
+
delete MSG[key];
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export class plugin {
|
|
2
|
+
/**
|
|
3
|
+
* 模块名
|
|
4
|
+
*/
|
|
5
|
+
name;
|
|
6
|
+
/**
|
|
7
|
+
* 模块说明
|
|
8
|
+
*/
|
|
9
|
+
dsc;
|
|
10
|
+
/**
|
|
11
|
+
* 事件枚举
|
|
12
|
+
*/
|
|
13
|
+
event;
|
|
14
|
+
/**
|
|
15
|
+
* 事件类型
|
|
16
|
+
*/
|
|
17
|
+
eventType;
|
|
18
|
+
/**
|
|
19
|
+
* 匹配优先级
|
|
20
|
+
*/
|
|
21
|
+
priority;
|
|
22
|
+
/**
|
|
23
|
+
* 匹配集
|
|
24
|
+
*/
|
|
25
|
+
rule;
|
|
26
|
+
/**
|
|
27
|
+
* @param name 类名标记
|
|
28
|
+
* @param event 事件类型
|
|
29
|
+
* @param eventType 消息类型
|
|
30
|
+
* @param priority 优先级 数字越小优先级越高
|
|
31
|
+
* @param rule.reg 命令正则 RegExp | string
|
|
32
|
+
* @param rule.fnc 命令函数 function
|
|
33
|
+
* @param rule.dsc 指令示范 sdc
|
|
34
|
+
* @param rule.doc 指令文档 doc
|
|
35
|
+
* @param rule.priority 优先级 数字越小优先级越高
|
|
36
|
+
*/
|
|
37
|
+
constructor({ name = 'your-name', event = 'MESSAGES', eventType = 'CREATE', priority = 9000, rule = [] }) {
|
|
38
|
+
this.name = name;
|
|
39
|
+
this.event = event;
|
|
40
|
+
this.eventType = eventType;
|
|
41
|
+
this.priority = priority;
|
|
42
|
+
this.rule = rule;
|
|
43
|
+
}
|
|
44
|
+
}
|