alemonjs 1.1.18 → 1.1.20

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.
Files changed (43) hide show
  1. package/README.md +79 -12
  2. package/lib/core/apps.js +105 -23
  3. package/lib/core/cache.js +1 -1
  4. package/lib/core/call.js +44 -0
  5. package/lib/core/dealmsg.js +66 -0
  6. package/lib/core/ip.js +7 -0
  7. package/lib/core/typings.js +1 -0
  8. package/lib/define/main.js +7 -3
  9. package/lib/discord/alemon/bot.js +1 -1
  10. package/lib/discord/sdk/wss.js +1 -1
  11. package/lib/kook/alemon/bot.js +1 -1
  12. package/lib/kook/alemon/message/DIRECT_MESSAGE.js +2 -2
  13. package/lib/kook/alemon/message/PUBLIC_GUILD_MESSAGES.js +3 -3
  14. package/lib/kook/sdk/ws.js +2 -2
  15. package/lib/ntqq/alemon/conversation.js +1 -1
  16. package/lib/ntqq/alemon/group/bot.js +1 -1
  17. package/lib/ntqq/sdk/api/group.js +2 -2
  18. package/lib/ntqq/sdk/wss.js +7 -7
  19. package/lib/one/alemon/bot.js +1 -1
  20. package/lib/one/alemon/conversation.js +1 -1
  21. package/lib/qq/alemon/bot.js +1 -1
  22. package/lib/qq/alemon/conversation.js +1 -1
  23. package/lib/qq/alemon/message/GUILD_MESSAGES.js +1 -1
  24. package/lib/qq/alemon/message/PUBLIC_GUILD_MESSAGES.js +1 -1
  25. package/lib/qq/sdk/typings.js +1 -1
  26. package/lib/qq/sdk/wss.js +4 -4
  27. package/lib/villa/alemon/message/MESSAGES.js +2 -2
  28. package/lib/villa/sdk/wss.js +43 -22
  29. package/package.json +2 -1
  30. package/types/core/apps.d.ts +67 -21
  31. package/types/core/cache.d.ts +1 -1
  32. package/types/core/call.d.ts +20 -0
  33. package/types/core/ip.d.ts +5 -0
  34. package/types/discord/sdk/config.d.ts +1 -2
  35. package/types/kook/sdk/config.d.ts +1 -2
  36. package/types/ntqq/sdk/config.d.ts +3 -3
  37. package/types/ntqq/sdk/wss.d.ts +2 -2
  38. package/types/qq/sdk/config.d.ts +10 -3
  39. package/types/qq/sdk/typings.d.ts +2 -9
  40. package/types/villa/sdk/config.d.ts +7 -1
  41. package/types/villa/sdk/types.d.ts +0 -10
  42. package/types/villa/sdk/wss.d.ts +1 -1
  43. package/types/villa/villa.d.ts +2 -2
package/README.md CHANGED
@@ -1,23 +1,90 @@
1
- ☞[English document](./README_English.md)
1
+ ## 阿柠檬跨平台开发框架
2
2
 
3
- ## 阿柠檬跨平台开发框架机器人
3
+ alemonjs 是消息正则匹配的跨平台开发框架,支持 QQ 群、QQ 频道、KOOK、Discord、米游社大别野等平台,只需要一套代码即可快速构建机器人。
4
4
 
5
- > 需要配置机器人才可启动
5
+ 我们提供了热开发和打包编译工具,除此,也能自行混淆压缩来提高机器人响应速度
6
6
 
7
- 开发文档 OPEN[https://alemonjs.com](https://alemonjs.com)
7
+ [开发文档 https://alemonjs.com](https://alemonjs.com)
8
8
 
9
- 类型/接口 OPEN[docs](https://ningmengchongshui.github.io/alemon/)
9
+ ### 快速开始
10
10
 
11
- 开发模板 GITEE[create-alemonjs](https://gitee.com/ningmengchongshui/alemon/tree/cli/bin)
11
+ 可直接执行脚手架 并快速启动程序
12
12
 
13
- 开发模板 GITHUB[create-alemonjs](https://github.com/ningmengchongshui/alemon/tree/cli/bin)
13
+ ```sh
14
+ npm init alemonjs@latest -y
15
+ cd alemonb
16
+ npm install
17
+ npm run dev
18
+ ```
14
19
 
15
- 共享平台 OPEN[sharing platform](https://gitee.com/ningmengchongshui/alemon/tree/web/docs/.vitepress/components/data)
20
+ 连接平台需要正确配置登录
16
21
 
17
- ## 开源协议
22
+ `a.login.config.ts`
18
23
 
19
- GNU GPL 是使用最广泛的自由软件许可证,并有强烈的版权要求
24
+ ```ts
25
+ import { LoginMap } from 'alemonjs'
26
+ export const login: LoginMap = {
27
+ // 配置名
28
+ test: {
29
+ // 平台名 qq discord ntqq qq villa
30
+ qq: {
31
+ // ... 配置信息
32
+ // ...详细请看
33
+ // alemonjs.com
34
+ }
35
+ }
36
+ }
37
+ ```
20
38
 
21
- 分发衍生作品时,作品的源代码必须在同一许可证下可用
39
+ > npm run [脚本名] [配置名] [平台名]
22
40
 
23
- GNUGPL 有多种变体,每个变体都有不同的要求
41
+ 启动时带上匹配规则机器人正确启动
42
+
43
+ ```sh
44
+ npm run dev test qq
45
+ ```
46
+
47
+ ### 开发示例
48
+
49
+ 启动开发模式后会以`apps`作为应用目录
50
+
51
+ ```ts
52
+ // apps/word.ts
53
+ import { APlugin, AMessage } from 'alemonjs'
54
+ // 导出应用 PluginName
55
+ export class PluginName extends APlugin {
56
+ constructor() {
57
+ super({
58
+ dsc: '插件描述', // 描述,可有可无
59
+ rule: [
60
+ {
61
+ reg: /你好/, // 响应消息 你好
62
+ fnc: 'post' // 执行函数 post
63
+ }
64
+ ]
65
+ })
66
+ }
67
+ // 定义函数 post
68
+ async post(e: AMessage) {
69
+ // 事件.回复("你好呀")
70
+ e.reply('你好呀')
71
+ // 直接返回 null | undefined | false
72
+ // 不再进行后续的正则匹配
73
+ return
74
+ }
75
+ }
76
+ ```
77
+
78
+ 每当文件有修改或进行保存时就会自动刷新应用
79
+
80
+ 当确认该应用可用于生成环境时,可进行打包操作
81
+
82
+ ```sh
83
+ npm run build
84
+ ```
85
+
86
+ > 注意,请确保内容修改都是在 npm run dev 下进行
87
+
88
+ 打包完成后,会生成`dist/main.js`和`package.json`文件
89
+
90
+ 该程序可放置于`plugins/your plugin name`文件夹下
package/lib/core/apps.js CHANGED
@@ -2,8 +2,9 @@ import { dirname, basename } from 'path';
2
2
  import { fileURLToPath } from 'url';
3
3
  import { setAppMessage, setAppArg, setAppCharacter, setAppEvent, setAppPriority } from './cache.js';
4
4
  import { setApp } from './app.js';
5
+ import { setAllCall } from './call.js';
5
6
  /**
6
- * 引用路径
7
+ * 应用路径
7
8
  * @param url
8
9
  * @returns
9
10
  */
@@ -34,17 +35,19 @@ export function getAppName(url) {
34
35
  /**
35
36
  * 创建应用对象
36
37
  * @param url import.meta.url
37
- * @returns AppName目录名
38
+ @deprecated createApp()
39
+ * @returns
38
40
  */
39
41
  export function createApps(url) {
40
- return createApp(getAppName(url));
42
+ return createApp(url);
41
43
  }
42
44
  /**
43
45
  * 创建应用对象
44
- * @param AppName 目录名 getAppName(import.meta.url)
45
- * @returns 应用app对象
46
+ * @param url import.meta.url
47
+ * @returns
46
48
  */
47
- export function createApp(AppName) {
49
+ export function createApp(url) {
50
+ const AppName = getAppName(url);
48
51
  /**
49
52
  * 应用集
50
53
  */
@@ -56,8 +59,11 @@ export function createApp(AppName) {
56
59
  /**
57
60
  * 设置默认指令规则
58
61
  */
59
- setAppCharacter(AppName, '/');
60
- return {
62
+ setAppCharacter(AppName);
63
+ /**
64
+ * 应用
65
+ */
66
+ const app = {
61
67
  /**
62
68
  * 设置正则最低优先级
63
69
  * 当设置了500,而所有优先级为5000时
@@ -66,30 +72,28 @@ export function createApp(AppName) {
66
72
  * @param val
67
73
  * @returns
68
74
  */
69
- setPriority: (val) => {
75
+ priority: (val) => {
70
76
  try {
71
77
  setAppPriority(AppName, val);
72
- return true;
73
78
  }
74
79
  catch (err) {
75
80
  console.error('APP setEvent', err);
76
- return false;
77
81
  }
82
+ return app;
78
83
  },
79
84
  /**
80
85
  * 设置正则默认消息
81
86
  * @param val
82
87
  * @returns
83
88
  */
84
- setEvent: (val) => {
89
+ event: (val) => {
85
90
  try {
86
91
  setAppEvent(AppName, val);
87
- return true;
88
92
  }
89
93
  catch (err) {
90
94
  console.error('APP setEvent', err);
91
- return false;
92
95
  }
96
+ return app;
93
97
  },
94
98
  /**
95
99
  * 设置指令规则
@@ -97,12 +101,11 @@ export function createApp(AppName) {
97
101
  setCharacter: (val) => {
98
102
  try {
99
103
  setAppCharacter(AppName, val);
100
- return true;
101
104
  }
102
105
  catch (err) {
103
106
  console.error('APP setCharacter', err);
104
- return false;
105
107
  }
108
+ return app;
106
109
  },
107
110
  /**
108
111
  * 设置扩展参
@@ -110,31 +113,45 @@ export function createApp(AppName) {
110
113
  setArg: (fnc) => {
111
114
  try {
112
115
  setAppArg(AppName, fnc);
113
- return true;
114
116
  }
115
117
  catch (err) {
116
118
  console.error('APP setArg', err);
117
- return false;
118
119
  }
120
+ return app;
121
+ },
122
+ /**
123
+ * 重定义事件
124
+ * @param fnc 回调函数
125
+ * @returns 是否成功定义
126
+ */
127
+ reSetEvent: (fnc) => {
128
+ try {
129
+ setAppMessage(AppName, fnc);
130
+ }
131
+ catch (err) {
132
+ console.error('APP setMessage', err);
133
+ }
134
+ return app;
119
135
  },
120
136
  /**
121
- * 重定义消息
137
+ * 重定义事件
122
138
  * @param fnc 回调函数
139
+ * @deprecated 废弃,请使用reSetEvent()
123
140
  * @returns 是否成功定义
124
141
  */
125
142
  setMessage: (fnc) => {
126
143
  try {
127
144
  setAppMessage(AppName, fnc);
128
- return true;
129
145
  }
130
146
  catch (err) {
131
147
  console.error('APP setMessage', err);
132
- return false;
133
148
  }
149
+ return app;
134
150
  },
135
151
  /**
136
152
  * 创建应用
137
153
  * @param app 应用对象
154
+ * @deprecated 废弃,请使用use()
138
155
  */
139
156
  component: (urlObject = {}) => {
140
157
  try {
@@ -173,15 +190,78 @@ export function createApp(AppName) {
173
190
  }
174
191
  }
175
192
  }
176
- return true;
177
193
  }
178
194
  catch (err) {
179
195
  console.error('APP component', err);
180
- return false;
181
196
  }
197
+ return app;
198
+ },
199
+ /**
200
+ * 创建应用
201
+ * @param app 应用对象
202
+ */
203
+ use: (urlObject = {}) => {
204
+ try {
205
+ for (const item in urlObject) {
206
+ /**
207
+ * 如果该导出是class
208
+ */
209
+ if (urlObject[item].prototype) {
210
+ if (!Object.prototype.hasOwnProperty.call(apps, item)) {
211
+ /**
212
+ * 不重名
213
+ */
214
+ apps[item] = urlObject[item];
215
+ continue;
216
+ }
217
+ const T = true;
218
+ while (T) {
219
+ const keyName = `${item}$${acount}`;
220
+ if (!Object.prototype.hasOwnProperty.call(apps, keyName)) {
221
+ /**
222
+ * 不重名
223
+ */
224
+ apps[keyName] = urlObject[item];
225
+ /**
226
+ * 重置为0
227
+ */
228
+ acount = 0;
229
+ break;
230
+ }
231
+ else {
232
+ /**
233
+ * 加1
234
+ */
235
+ acount++;
236
+ }
237
+ }
238
+ }
239
+ }
240
+ }
241
+ catch (err) {
242
+ console.error('APP use', err);
243
+ }
244
+ return app;
245
+ },
246
+ /**
247
+ * 比正则系统更优先的回调系统
248
+ * 且无视所有event设置
249
+ * @param event 事件
250
+ * @param call 回调
251
+ * @param priority 优先级
252
+ */
253
+ on: (event, call, priority = 9000) => {
254
+ try {
255
+ setAllCall(event, call, priority);
256
+ }
257
+ catch (err) {
258
+ console.error('APP on', err);
259
+ }
260
+ return app;
182
261
  },
183
262
  /**
184
263
  * 挂起应用
264
+ * @returns
185
265
  */
186
266
  mount: () => {
187
267
  try {
@@ -190,6 +270,8 @@ export function createApp(AppName) {
190
270
  catch (err) {
191
271
  console.error('APP mount', err);
192
272
  }
273
+ return app;
193
274
  }
194
275
  };
276
+ return app;
195
277
  }
package/lib/core/cache.js CHANGED
@@ -62,7 +62,7 @@ export function getAppCharacter(key) {
62
62
  * @param key 插件名
63
63
  * @param val 字符
64
64
  */
65
- export function setAppCharacter(key, val) {
65
+ export function setAppCharacter(key, val = '/') {
66
66
  CAT[key] = val;
67
67
  }
68
68
  /**
@@ -0,0 +1,44 @@
1
+ import lodash from 'lodash';
2
+ const CALL = {
3
+ AUDIO_FREQUENCY: [],
4
+ AUDIO_MICROPHONE: [],
5
+ CHANNEL: [],
6
+ FORUMS_POST: [],
7
+ FORUMS_REPLY: [],
8
+ FORUMS_THREAD: [],
9
+ GUILD: [],
10
+ GUILD_BOT: [],
11
+ GUILD_MEMBERS: [],
12
+ GUILD_MESSAGE_REACTIONS: [],
13
+ INTERACTION: [],
14
+ MESSAGES: [],
15
+ message: []
16
+ };
17
+ /**
18
+ * 排序回调
19
+ */
20
+ export function orderByAppCall() {
21
+ // 排序
22
+ for (const val in CALL) {
23
+ CALL[val] = lodash.orderBy(CALL[val], ['priority'], ['asc']);
24
+ }
25
+ }
26
+ /**
27
+ * 得到回调
28
+ * @returns
29
+ */
30
+ export function getAppCall(event) {
31
+ return CALL[event];
32
+ }
33
+ /**
34
+ * 设置回调
35
+ * @param event
36
+ * @param call
37
+ * @param priority
38
+ */
39
+ export function setAllCall(event, call, priority = 9000) {
40
+ CALL[event].push({
41
+ call,
42
+ priority
43
+ });
44
+ }
@@ -7,6 +7,7 @@ import { getApp, delApp, getAppKey } from './app.js';
7
7
  import { EventEnum } from './typings.js';
8
8
  import { conversationHandlers, getConversationState } from './dialogue.js';
9
9
  import { getAppProCoinfg } from './configs.js';
10
+ import { getAppCall, orderByAppCall } from './call.js';
10
11
  const Command = {};
11
12
  const CommandNotMessage = {};
12
13
  const CommandApp = {};
@@ -244,6 +245,18 @@ function dataInit() {
244
245
  * 插件初始化
245
246
  */
246
247
  export async function appsInit() {
248
+ /**
249
+ * *********
250
+ * 回调系统
251
+ * ********
252
+ */
253
+ // 排序
254
+ orderByAppCall();
255
+ /**
256
+ * ************
257
+ * 正则系统
258
+ * ************
259
+ */
247
260
  // 清空当前的apps
248
261
  dataInit();
249
262
  // 得到所有插件名
@@ -309,6 +322,43 @@ export async function InstructionMatching(e) {
309
322
  await handler(e, state);
310
323
  return true;
311
324
  }
325
+ let t = false;
326
+ /**
327
+ * 回调系统
328
+ */
329
+ for (const app of getAppCall('MESSAGES')) {
330
+ if (t === true)
331
+ break;
332
+ try {
333
+ // app.call
334
+ const back = await app.call(e);
335
+ if (back === true) {
336
+ t = back;
337
+ }
338
+ }
339
+ catch (err) {
340
+ console.error(`[${e.event}]`, `[${app.call}]`, `[${false}]\n`, `[${err}]`);
341
+ continue;
342
+ }
343
+ }
344
+ /**
345
+ * 回调系统
346
+ */
347
+ for (const app of getAppCall('message')) {
348
+ if (t === true)
349
+ break;
350
+ try {
351
+ // app.call
352
+ const back = await app.call(e);
353
+ if (back === true) {
354
+ t = back;
355
+ }
356
+ }
357
+ catch (err) {
358
+ console.error(`[${e.event}]`, `[${app.call}]`, `[${false}]\n`, `[${err}]`);
359
+ continue;
360
+ }
361
+ }
312
362
  const APPCACHE = {};
313
363
  const ARGCACHE = {};
314
364
  /**
@@ -414,6 +464,22 @@ export async function typeMessage(e) {
414
464
  console.info('e', e);
415
465
  if (!CommandNotMessage[e.event])
416
466
  return true;
467
+ /**
468
+ * 回调系统
469
+ */
470
+ const event = getAppCall(e.event);
471
+ for (const app of event) {
472
+ try {
473
+ // app.call
474
+ const back = await app.call(e);
475
+ if (back === true)
476
+ break;
477
+ }
478
+ catch (err) {
479
+ console.error(`[${e.event}]`, `[${app.call}]`, `[${false}]\n`, `[${err}]`);
480
+ continue;
481
+ }
482
+ }
417
483
  const APPCACHE = {};
418
484
  const ARGCACHE = {};
419
485
  for (const item in CommandApp) {
package/lib/core/ip.js CHANGED
@@ -1,5 +1,12 @@
1
1
  import { publicIp } from 'public-ip';
2
2
  let myIp;
3
+ /**
4
+ * 得到本机IP地址
5
+ * @returns
6
+ */
7
+ export async function setPublicIP(val) {
8
+ myIp = val;
9
+ }
3
10
  /**
4
11
  * 得到本机IP地址
5
12
  * @returns
@@ -67,6 +67,7 @@ export const EventEnum = [
67
67
  'MESSAGES',
68
68
  /**
69
69
  * 消息
70
+ * @deprecated MESSAGES
70
71
  */
71
72
  'message',
72
73
  /**
@@ -3,7 +3,7 @@ import { rebotMap } from './map.js';
3
3
  import { nodeScripts } from './child_process.js';
4
4
  import { command } from './command.js';
5
5
  import { AppNameError } from '../log/index.js';
6
- import { setLanchConfig, loadInit, appsInit, setAppProCoinfg, startChrom, getPublicIP } from '../core/index.js';
6
+ import { setLanchConfig, loadInit, appsInit, setAppProCoinfg, startChrom, getPublicIP, setPublicIP } from '../core/index.js';
7
7
  import { getPupPath, setBotConfigByKey, getBotConfigByKey } from '../config/index.js';
8
8
  import { createWeb } from '../koa/index.js';
9
9
  import { autoClearFiles } from '../koa/file.js';
@@ -222,8 +222,12 @@ export async function defineAlemonConfig(Options) {
222
222
  // 定时清除
223
223
  autoClearFiles();
224
224
  }
225
- // 缓存ip
226
- getPublicIP();
225
+ if (Options?.server?.ip) {
226
+ setPublicIP(Options?.server?.ip);
227
+ }
228
+ else {
229
+ getPublicIP();
230
+ }
227
231
  }
228
232
  /**
229
233
  * ***********
@@ -1,7 +1,7 @@
1
1
  let bot = {
2
2
  id: '',
3
3
  name: '',
4
- avatar: 'string'
4
+ avatar: ''
5
5
  };
6
6
  export function setBotMsgByDISOCRD(val) {
7
7
  bot = val;
@@ -10,7 +10,7 @@ import { getBotConfig } from './config.js';
10
10
  export async function createClient(conversation, shard = [0, 1]) {
11
11
  const { url } = await gateway();
12
12
  if (!url) {
13
- console.error('TOKEN 错误');
13
+ console.error('[getway] token err');
14
14
  return;
15
15
  }
16
16
  const wsConn = new WebSocket(`${url}?v=10&encoding=json`);
@@ -1,7 +1,7 @@
1
1
  let bot = {
2
2
  id: '',
3
3
  name: '',
4
- avatar: 'string'
4
+ avatar: ''
5
5
  };
6
6
  export function setBotMsgByKOOK(val) {
7
7
  bot = val;
@@ -44,8 +44,8 @@ export const DIRECT_MESSAGE = async (event) => {
44
44
  at: false,
45
45
  at_users: [],
46
46
  at_user: undefined,
47
- msg: event.content,
48
- msg_txt: event.content,
47
+ msg: event?.extra?.kmarkdown?.raw_content ?? event.content,
48
+ msg_txt: event?.extra?.kmarkdown?.raw_content ?? event.content,
49
49
  msg_id: event.msg_id,
50
50
  open_id: open_id,
51
51
  //
@@ -19,7 +19,7 @@ export const PUBLIC_GUILD_MESSAGES_KOOK = async (event) => {
19
19
  console.info('event', event);
20
20
  let at = false;
21
21
  const at_users = [];
22
- let msg = event.content;
22
+ let msg = event?.extra?.kmarkdown?.raw_content ?? event.content;
23
23
  /**
24
24
  * 艾特类型所得到的
25
25
  * 包括机器人在内
@@ -30,7 +30,7 @@ export const PUBLIC_GUILD_MESSAGES_KOOK = async (event) => {
30
30
  at_users.push({
31
31
  id: item.role_id,
32
32
  name: item.name,
33
- avatar: 'string',
33
+ avatar: '',
34
34
  bot: true
35
35
  });
36
36
  msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
@@ -90,7 +90,7 @@ export const PUBLIC_GUILD_MESSAGES_KOOK = async (event) => {
90
90
  at_users,
91
91
  at_user,
92
92
  msg,
93
- msg_txt: event.content,
93
+ msg_txt: event?.extra?.kmarkdown?.raw_content ?? event.content,
94
94
  msg_id: event.msg_id,
95
95
  open_id: data?.code ?? '', // 私聊标记 空的 需要创建私聊 每次请求都自动创建
96
96
  //
@@ -24,11 +24,11 @@ export async function getGatewayUrl(token, url = 'https://www.kookapp.cn/api/v3/
24
24
  return response.data.data.url;
25
25
  }
26
26
  else {
27
- console.error('http err', response.data.message);
27
+ console.error('[getway] http err', response.data.message);
28
28
  }
29
29
  }
30
30
  catch (error) {
31
- console.error('token err', error.message);
31
+ console.error('[getway] token err', error.message);
32
32
  }
33
33
  }
34
34
  /**
@@ -12,7 +12,7 @@ export function conversation(t, d) {
12
12
  setBotMsgByNtqq({
13
13
  id: d.user.id,
14
14
  name: d.user.name,
15
- avatar: 'string'
15
+ avatar: ''
16
16
  });
17
17
  }
18
18
  else if (t == 'GROUP_AT_MESSAGE_CREATE') {
@@ -1,7 +1,7 @@
1
1
  let bot = {
2
2
  id: '',
3
3
  name: '',
4
- avatar: 'string'
4
+ avatar: ''
5
5
  };
6
6
  export function setBotMsgByNtqq(val) {
7
7
  bot = val;
@@ -35,11 +35,11 @@ export async function gateway() {
35
35
  return url;
36
36
  }
37
37
  else {
38
- console.error('http err:', null);
38
+ console.error('[getway] http err:', null);
39
39
  }
40
40
  })
41
41
  .catch(error => {
42
- console.error('token err:', error.message);
42
+ console.error('[getway] token err:', error.message);
43
43
  });
44
44
  }
45
45
  /**