alemonjs 2.0.0-rc.2 → 2.0.0-rc.4

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.
@@ -0,0 +1,6 @@
1
+ import { ConfigType } from '../typing/config.js';
2
+
3
+ type BotType = (_: ConfigType) => typeof global.alemonjs;
4
+ declare const defineBot: (callback: BotType) => BotType;
5
+
6
+ export { defineBot };
@@ -0,0 +1,3 @@
1
+ const defineBot = (callback) => callback;
2
+
3
+ export { defineBot };
@@ -0,0 +1,11 @@
1
+ import { ConfigType } from '../typing/config.js';
2
+
3
+ type callbackObjType = {
4
+ onCreated?: () => void;
5
+ onMounted?: () => void;
6
+ unMounted?: () => void;
7
+ };
8
+ type callbackType = (config: ConfigType) => callbackObjType;
9
+ declare const defineChildren: (_: callbackType) => void;
10
+
11
+ export { defineChildren };
@@ -0,0 +1,3 @@
1
+ const defineChildren = (_) => { };
2
+
3
+ export { defineChildren };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 递归获取所有文件名以 res 开头的文件
3
+ * @param dir 目录路径
4
+ * @returns 文件路径数组
5
+ */
6
+ declare const getAppsFiles: (dir: string) => string[];
7
+
8
+ export { getAppsFiles };
@@ -1,28 +1,18 @@
1
- import { join, dirname } from 'node:path';
2
- import fs from 'node:fs';
1
+ import fs from 'fs';
2
+ import { join } from 'path';
3
3
 
4
- const _dir = './src/apps';
5
- /**
6
- *
7
- */
8
- const values = [];
9
- /**
10
- *
11
- * @returns
12
- */
13
- const getFilesValues = () => values;
14
4
  /**
15
5
  * 递归获取所有文件名以 res 开头的文件
16
6
  * @param dir 目录路径
17
7
  * @returns 文件路径数组
18
8
  */
19
- const getFiles = (dir) => {
9
+ const getAppsFiles = (dir) => {
20
10
  let results = [];
21
11
  const list = fs.readdirSync(dir, { withFileTypes: true });
22
12
  list.forEach(item => {
23
13
  const fullPath = join(dir, item.name);
24
14
  if (item.isDirectory()) {
25
- results = results.concat(getFiles(fullPath));
15
+ results = results.concat(getAppsFiles(fullPath));
26
16
  }
27
17
  else if (item.isFile() && item.name.startsWith('res')) {
28
18
  if (item.name.endsWith('.ts') ||
@@ -35,30 +25,5 @@ const getFiles = (dir) => {
35
25
  });
36
26
  return results;
37
27
  };
38
- /**
39
- * 加载文件
40
- */
41
- const loadFiles = async () => {
42
- const dir = join(process.cwd(), _dir);
43
- const files = getFiles(dir);
44
- // 读取config ,根据config对目录进行分类
45
- for (const item of files) {
46
- // 暂时不使用 config.js
47
- // const dir = dirname(item)
48
- let v = {};
49
- // try {
50
- // const obj = await import(`file://${dir}/config.js`)
51
- // v = obj?.default
52
- // } catch (e) {
53
- // // console.error(e)
54
- // }
55
- // 保存目录地址和文件地址
56
- values.push({
57
- ...v,
58
- dir: dirname(item),
59
- path: item
60
- });
61
- }
62
- };
63
28
 
64
- export { getFilesValues, loadFiles };
29
+ export { getAppsFiles };
@@ -1,9 +1,26 @@
1
1
  import { AEvents } from '../typing/typing.js';
2
2
 
3
+ type DbKey = {
4
+ dir: string;
5
+ value?: {
6
+ reg: RegExp;
7
+ event: string;
8
+ priority: number;
9
+ } | null;
10
+ path: string;
11
+ };
12
+ declare global {
13
+ var AppsFiles: DbKey[];
14
+ }
15
+ /**
16
+ *
17
+ * @param val
18
+ */
19
+ declare const pushAppsFiles: (val: DbKey) => void;
3
20
  /**
4
21
  * 加载文件
5
22
  */
6
- declare const loadFiles: () => Promise<void>;
23
+ declare const loadFiles: () => void;
7
24
  /**
8
25
  * 消息处理器
9
26
  * @param value
@@ -12,4 +29,4 @@ declare const loadFiles: () => Promise<void>;
12
29
  */
13
30
  declare const OnProcessor: <T extends keyof AEvents>(value: AEvents[T], event: T) => void;
14
31
 
15
- export { OnProcessor, loadFiles };
32
+ export { OnProcessor, loadFiles, pushAppsFiles };
@@ -3,8 +3,9 @@ import fs from 'node:fs';
3
3
  import { useParse } from '../hook/use-api.js';
4
4
 
5
5
  const _dir = './src/apps';
6
- //
7
- const files = [];
6
+ if (!global.AppsFiles) {
7
+ global.AppsFiles = [];
8
+ }
8
9
  //
9
10
  const values = {
10
11
  'message.create': []
@@ -14,13 +15,13 @@ const values = {
14
15
  * @param dir 目录路径
15
16
  * @returns 文件路径数组
16
17
  */
17
- const getFiles = (dir) => {
18
+ const getAppsFiles = (dir) => {
18
19
  let results = [];
19
20
  const list = fs.readdirSync(dir, { withFileTypes: true });
20
21
  list.forEach(item => {
21
22
  const fullPath = join(dir, item.name);
22
23
  if (item.isDirectory()) {
23
- results = results.concat(getFiles(fullPath));
24
+ results = results.concat(getAppsFiles(fullPath));
24
25
  }
25
26
  else if (item.isFile() && item.name.startsWith('res')) {
26
27
  if (item.name.endsWith('.ts') ||
@@ -33,26 +34,23 @@ const getFiles = (dir) => {
33
34
  });
34
35
  return results;
35
36
  };
37
+ /**
38
+ *
39
+ * @param val
40
+ */
41
+ const pushAppsFiles = (val) => {
42
+ global.AppsFiles.push(val);
43
+ };
36
44
  /**
37
45
  * 加载文件
38
46
  */
39
- const loadFiles = async () => {
47
+ const loadFiles = () => {
40
48
  const dir = join(process.cwd(), _dir);
41
- const Filesx = getFiles(dir);
49
+ const Filesx = getAppsFiles(dir);
42
50
  // 读取config ,根据config对目录进行分类
43
51
  for (const item of Filesx) {
44
- // 暂时不使用 config.js
45
- // const dir = dirname(item)
46
- let v = {};
47
- // try {
48
- // const obj = await import(`file://${dir}/config.js`)
49
- // v = obj?.default
50
- // } catch (e) {
51
- // // console.error(e)
52
- // }
53
52
  // 保存目录地址和文件地址
54
- files.push({
55
- ...v,
53
+ global.AppsFiles.push({
56
54
  dir: dirname(item),
57
55
  path: item
58
56
  });
@@ -70,7 +68,7 @@ const onMessageCreate = async (e) => {
70
68
  global.storeoberver['message.create'] = [];
71
69
  }
72
70
  // copy
73
- const messageFiles = [...files];
71
+ const messageFiles = [...global.AppsFiles];
74
72
  const messageCreate = [...values['message.create']];
75
73
  let i = 0;
76
74
  let j = 0;
@@ -97,19 +95,34 @@ const onMessageCreate = async (e) => {
97
95
  next();
98
96
  return;
99
97
  }
98
+ //
99
+ n++;
100
100
  // 发现订阅
101
- const item = global.storeoberver['message.create'][n];
101
+ const item = global.storeoberver['message.create'][n - 1];
102
+ if (!item) {
103
+ // 继续 next
104
+ nextOberver();
105
+ return;
106
+ }
102
107
  for (const key in item.event) {
103
108
  // 只要发现不符合的,就继续
104
109
  if (item.event[key] !== e[key]) {
105
110
  // 不符合。继续 next。
106
- n++;
107
111
  nextOberver();
108
112
  return;
109
113
  }
110
114
  }
111
- // 符合。调用
112
- item.callback(e, { next });
115
+ // 设置为undefined
116
+ global.storeoberver['message.create'][n - 1] = undefined;
117
+ // 放回来
118
+ const Continue = () => {
119
+ global.storeoberver['message.create'][n - 1] = item;
120
+ // 直接结束才对
121
+ };
122
+ // 没有调用下一步。应该删除当前的 n ?
123
+ // 有没有可能。按key来分。
124
+ item.callback(e, { next: Continue });
125
+ //
113
126
  };
114
127
  const calli = async () => {
115
128
  // 调用完了
@@ -144,8 +157,8 @@ const onMessageCreate = async (e) => {
144
157
  // 推送, 确保下次直接流向 message.create ,不再从头开始
145
158
  if (!values['message.create'].find(v => v.path === file.path)) {
146
159
  // update files and values
147
- const index = files.findIndex(v => v.path === file.path);
148
- files.splice(index, 1);
160
+ const index = global.AppsFiles.findIndex(v => v.path === file.path);
161
+ global.AppsFiles.splice(index, 1);
149
162
  values['message.create'].push(v);
150
163
  }
151
164
  const msg = useParse(e.Megs, 'Text') ?? '';
@@ -211,4 +224,4 @@ const OnProcessor = (value, event) => {
211
224
  return;
212
225
  };
213
226
 
214
- export { OnProcessor, loadFiles };
227
+ export { OnProcessor, loadFiles, pushAppsFiles };
package/lib/config.d.ts CHANGED
@@ -1,19 +1,11 @@
1
+ import { ConfigType } from './typing/config.js';
2
+
1
3
  declare const argv: string[];
2
4
  /**
3
5
  * @param key 参数
4
6
  * @returns 参数值
5
7
  */
6
8
  declare const getArgvValue: (key: string) => string;
7
- type ConfigType = {
8
- login: string | undefined;
9
- app_id: string | undefined;
10
- token: string | undefined;
11
- intent: string | undefined;
12
- secret: string | undefined;
13
- shard: number[] | undefined;
14
- intents: string[] | undefined;
15
- master_id: string[] | undefined;
16
- };
17
9
  /**
18
10
  * 配置类
19
11
  */
@@ -34,4 +26,4 @@ declare class Config {
34
26
  get value(): any;
35
27
  }
36
28
 
37
- export { Config, type ConfigType, argv, getArgvValue };
29
+ export { Config, argv, getArgvValue };
@@ -68,8 +68,24 @@ const useOberver = (event, option) => {
68
68
  global.storeoberver = {};
69
69
  if (!global.storeoberver[option])
70
70
  global.storeoberver[option] = [];
71
- // 如果不存在。则创建
72
- global.storeoberver[option].push({ event: v, callback });
71
+ let i = 0;
72
+ const next = () => {
73
+ if (i >= global.storeoberver[option].length) {
74
+ // 如果不存在。则创建
75
+ global.storeoberver[option][i] = { event: v, callback };
76
+ return;
77
+ }
78
+ i++;
79
+ // 是空的。占据位置。
80
+ if (!global.storeoberver[option][i]) {
81
+ global.storeoberver[option][i] = { event: v, callback };
82
+ }
83
+ else {
84
+ // 不是空的。继续
85
+ next();
86
+ }
87
+ };
88
+ next();
73
89
  return;
74
90
  };
75
91
  };
package/lib/index.d.ts CHANGED
@@ -1,8 +1,12 @@
1
- export { Config, ConfigType, argv, getArgvValue } from './config.js';
1
+ export { Config, argv, getArgvValue } from './config.js';
2
2
  export { useOberver, useParse, useSend } from './hook/use-api.js';
3
3
  export { OnObserver, OnResponse, ResponseConfig } from './app/event-utlis.js';
4
4
  export { AEventByMessageCreate, AEventByMessageDelete, AEventByMessageUpdate, AEvents } from './typing/typing.js';
5
- export { OnProcessor, loadFiles } from './app/event-processor.js';
5
+ export { ConfigType } from './typing/config.js';
6
+ export { OnProcessor, loadFiles, pushAppsFiles } from './app/event-processor.js';
7
+ export { getAppsFiles } from './app/event-files.js';
8
+ export { defineBot } from './app/event-bot.js';
9
+ export { defineChildren } from './app/event-chidren.js';
6
10
  export { Ark, At, BtBox, Button, Embed, Emoji, Files, Image, Link, Text, Video, Voice } from './hook/message-format.js';
7
11
 
8
12
  type options = {
package/lib/index.js CHANGED
@@ -1,10 +1,33 @@
1
1
  import { argv, getArgvValue, Config } from './config.js';
2
- import { loadFiles } from './app/event-processor.js';
2
+ import { loadFiles, pushAppsFiles } from './app/event-processor.js';
3
3
  export { OnProcessor } from './app/event-processor.js';
4
+ import { getAppsFiles } from './app/event-files.js';
5
+ import { join, dirname } from 'path';
6
+ import { readFileSync } from 'fs';
4
7
  export { useOberver, useParse, useSend } from './hook/use-api.js';
5
8
  export { OnObserver, OnResponse, ResponseConfig } from './app/event-utlis.js';
9
+ export { defineBot } from './app/event-bot.js';
10
+ export { defineChildren } from './app/event-chidren.js';
6
11
  export { Ark, At, BtBox, Button, Embed, Emoji, Files, Image, Link, Text, Video, Voice } from './hook/message-format.js';
7
12
 
13
+ /**
14
+ *
15
+ * @param app
16
+ */
17
+ const loadChildrenFiles = app => {
18
+ const packageJson = JSON.parse(readFileSync(`node_modules/${app}/package.json`, 'utf-8'));
19
+ const mainPath = join(`node_modules/${app}`, packageJson.main);
20
+ const mainDir = dirname(mainPath);
21
+ const appsDir = join(mainDir, 'apps');
22
+ const files = getAppsFiles(appsDir);
23
+ for (const file of files) {
24
+ const dir = join(process.cwd(), file);
25
+ pushAppsFiles({
26
+ dir: dirname(dir),
27
+ path: dir
28
+ });
29
+ }
30
+ };
8
31
  /**
9
32
  * 创建机器人
10
33
  * @returns
@@ -16,12 +39,24 @@ async function createBot() {
16
39
  if (!cfg.values?.login) {
17
40
  throw new Error('login is required');
18
41
  }
19
- await loadFiles();
42
+ // local
43
+ loadFiles();
44
+ // module
45
+ if (cfg?.value?.apps) {
46
+ if (Array.isArray(cfg?.value?.apps)) {
47
+ for (const app of cfg?.value?.apps) {
48
+ // const m = await import(app)
49
+ // const c = m?.default()
50
+ loadChildrenFiles(app);
51
+ }
52
+ }
53
+ }
20
54
  // prefix
21
55
  const prefix = getArgvValue('--prefix') ?? '@alemonjs/';
22
56
  if (!skip) {
23
- const { login } = await import(`${prefix}${cfg.values.login}`);
24
- login(cfg.values);
57
+ const bot = await import(`${prefix}${cfg.values.login}`);
58
+ // 挂在全局
59
+ global.alemonjs = bot?.default(cfg.values);
25
60
  return;
26
61
  }
27
62
  await import(`${prefix}${cfg.values.login}`);
@@ -34,4 +69,4 @@ function defineConfig(options = {}) {
34
69
  return options;
35
70
  }
36
71
 
37
- export { Config, argv, createBot, defineConfig, getArgvValue, loadFiles };
72
+ export { Config, argv, createBot, defineConfig, getAppsFiles, getArgvValue, loadFiles, pushAppsFiles };
@@ -0,0 +1,12 @@
1
+ type ConfigType = {
2
+ login: string | undefined;
3
+ app_id: string | undefined;
4
+ token: string | undefined;
5
+ intent: string | undefined;
6
+ secret: string | undefined;
7
+ shard: number[] | undefined;
8
+ intents: string[] | undefined;
9
+ master_id: string[] | undefined;
10
+ };
11
+
12
+ export type { ConfigType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.0-rc.4",
4
4
  "description": "啊柠檬脚本",
5
5
  "author": "ningmengchongshui",
6
6
  "license": "MIT",