alemonjs 2.1.0-alpha.16 → 2.1.0-alpha.18

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.
@@ -194,7 +194,7 @@ const useMessage = (event) => {
194
194
  // return [createResult(ResultCode.Warn, '暂未支持', message_id)]
195
195
  // }
196
196
  const message = {
197
- send,
197
+ send
198
198
  // withdraw,
199
199
  // forward,
200
200
  // reply,
@@ -1,7 +1,7 @@
1
1
  import '../global.js';
2
2
  import { DataButton, ButtonRow, DataButtonGroup, DataButtonTemplate } from '../typing/message/button.js';
3
3
  import { DataArkListTip, DataArkListContent, DataArkList, DataArkListItem, DataArkCard, DataArkBigCard } from '../typing/message/ark.js';
4
- import { DataMarkDown, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle, DataMarkdownSubtitle, DataMarkdownBold, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownStrikethrough, DataMarkdownLink, DataMarkdownImage, DataMarkdownList, DataMarkdownListItem, DataMarkdownBlockquote, DataMarkdownDivider, DataMarkdownNewline } from '../typing/message/markdown.js';
4
+ import { DataMarkDown, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle, DataMarkdownSubtitle, DataMarkdownBold, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownStrikethrough, DataMarkdownLink, DataMarkdownImage, DataMarkdownList, DataMarkdownListItem, DataMarkdownBlockquote, DataMarkdownDivider, DataMarkdownNewline, DataMarkdownCode } from '../typing/message/markdown.js';
5
5
  import { DataImage, DataImageURL, DataImageFile } from '../typing/message/image.js';
6
6
  import { DataText } from '../typing/message/text.js';
7
7
  import { DataMention } from '../typing/message/mention.js';
@@ -119,6 +119,7 @@ declare const MD: {
119
119
  blockquote(text: string): DataMarkdownBlockquote;
120
120
  divider(): DataMarkdownDivider;
121
121
  newline(value?: boolean): DataMarkdownNewline;
122
+ code(value: DataMarkdownCode["value"], options?: DataMarkdownCode["options"]): DataMarkdownCode;
122
123
  };
123
124
 
124
125
  export { Ark, BT, Image, ImageFile, ImageURL, Link, MD, Mention, Text };
@@ -355,5 +355,12 @@ MD.newline = (value = false) => {
355
355
  value: value
356
356
  };
357
357
  };
358
+ MD.code = (value, options) => {
359
+ return {
360
+ type: 'MD.code',
361
+ value: value,
362
+ options: options
363
+ };
364
+ };
358
365
 
359
366
  export { Ark, BT, Image, ImageFile, ImageURL, Link, MD, Mention, Text };
@@ -30,8 +30,7 @@ declare const useUserHashKey: (event: {
30
30
  declare const createEventName: (url: string, appKey: string) => string;
31
31
  /**
32
32
  * 将字符串转为数字
33
- * @param str
34
- * @returns
33
+ * @deprecated 已废弃
35
34
  */
36
35
  declare const stringToNumber: (str: string, size?: number) => number;
37
36
  /**
package/lib/core/utils.js CHANGED
@@ -48,8 +48,7 @@ const createEventName = (url, appKey) => {
48
48
  };
49
49
  /**
50
50
  * 将字符串转为数字
51
- * @param str
52
- * @returns
51
+ * @deprecated 已废弃
53
52
  */
54
53
  const stringToNumber = (str, size = 33) => {
55
54
  let hash = 5381;
@@ -7,6 +7,12 @@ const processor_repeated_clear_size = 37;
7
7
  const file_suffix_middleware = /^mw(\.|\..*\.)(js|ts|jsx|tsx)$/;
8
8
  // 相应文件后缀正则
9
9
  const file_suffix_response = /^res(\.|\..*\.)(js|ts|jsx|tsx)$/;
10
+ // 通用框架前缀正则
11
+ const file_prefix_common = /^(@alemonjs\/|alemonjs-)/;
12
+ // 默认端口
13
+ const default_port = 17117;
14
+ // 默认平台通用前缀
15
+ const default_platform_common_prefix = '@alemonjs/';
10
16
  /**
11
17
  * 结果反馈码
12
18
  */
@@ -29,4 +35,4 @@ const EventMessageText = [
29
35
  'private.interaction.create'
30
36
  ];
31
37
 
32
- export { EventMessageText, Fail, FailAuth, FailInternal, FailParams, Ok, Warn, file_suffix_middleware, file_suffix_response, processor_repeated_clear_size, processor_repeated_clear_time_max, processor_repeated_clear_time_min, processor_repeated_event_time, processor_repeated_user_time };
38
+ export { EventMessageText, Fail, FailAuth, FailInternal, FailParams, Ok, Warn, default_platform_common_prefix, default_port, file_prefix_common, file_suffix_middleware, file_suffix_response, processor_repeated_clear_size, processor_repeated_clear_time_max, processor_repeated_clear_time_min, processor_repeated_event_time, processor_repeated_user_time };
package/lib/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export { PrivateEventMessageCreate, PrivateEventMessageDelete, PrivateEventMessa
12
12
  export { PrivateEventRequestFriendAdd, PrivateEventRequestGuildAdd } from './typing/event/request/index.js';
13
13
  export { ActionsEventEnum } from './typing/event/actions.js';
14
14
  export { Current, CurrentResult, CurrentResultValue, DefineChildrenCallback, DefineChildrenFunc, DefineChildrenValue, DefinePlatformCallback, DefinePlatformFunc, DefinePlatformValue, OnDataFormatFunc, OnMiddlewareFunc, OnMiddlewareReversalFunc, OnMiddlewareValue, OnResponseFunc, OnResponseReversalFunc, OnResponseValue, OnSelectsFunc } from './typing/event/index.js';
15
- export { EventKeys, Events, EventsEnum, EventsKeyEnum, EventsMessageCreate, EventsMessageCreateEnum, EventsMessageCreateKeys } from './typing/event/map.js';
15
+ export { EventKeys, Events, EventsEnum, EventsMessageCreate, EventsMessageCreateEnum, EventsMessageCreateKeys } from './typing/event/map.js';
16
16
  export { LoggerUtils } from './typing/logger/index.js';
17
17
  export { ClientAPI, ClientAPIMessageResult } from './typing/client/index.js';
18
18
  export { DataEnums, MessageDataFormat } from './typing/message/index.js';
@@ -41,4 +41,4 @@ export { DataMention } from './typing/message/mention.js';
41
41
  export { DataImage, DataImageFile, DataImageURL } from './typing/message/image.js';
42
42
  export { ButtonRow, DataButton, DataButtonGroup, DataButtonTemplate } from './typing/message/button.js';
43
43
  export { DataArkBigCard, DataArkCard, DataArkList, DataArkListContent, DataArkListItem, DataArkListTip } from './typing/message/ark.js';
44
- export { DataMarkDown, DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle } from './typing/message/markdown.js';
44
+ export { DataMarkDown, DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownCode, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle } from './typing/message/markdown.js';
package/lib/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export { ActionsEventEnum } from './typing/event/actions.js';
2
- export { EventsKeyEnum } from './typing/event/map.js';
3
2
  export { core, logger } from './global.js';
4
3
  export { ConfigCore, getConfig, getConfigValue } from './core/config.js';
5
4
  export { defineChildren } from './app/define-chidren.js';
package/lib/main.js CHANGED
@@ -16,8 +16,8 @@ import { cbpClient } from './cbp/connect.js';
16
16
  import { join } from 'path';
17
17
  import { existsSync } from 'fs';
18
18
  import { cbpServer } from './cbp/index.js';
19
+ import { default_port, file_prefix_common, default_platform_common_prefix } from './core/variable.js';
19
20
 
20
- const defaultPort = 17117;
21
21
  const loadState = () => {
22
22
  const value = getConfigValue() ?? {};
23
23
  const state = value?.core?.state ?? [];
@@ -72,7 +72,7 @@ const start = async (options = {}) => {
72
72
  }
73
73
  else {
74
74
  // 创建 cbp 服务器
75
- const port = options?.port || cfg.argv?.port || cfg.value?.port || defaultPort;
75
+ const port = options?.port || cfg.argv?.port || cfg.value?.port || default_port;
76
76
  // 设置环境变量
77
77
  process.env.port = port;
78
78
  cbpServer(port, async () => {
@@ -89,7 +89,7 @@ const start = async (options = {}) => {
89
89
  }
90
90
  // 如果存在
91
91
  if (platform) {
92
- const reg = /^(@alemonjs\/|alemonjs-)/;
92
+ const reg = file_prefix_common;
93
93
  if (reg.test(platform)) {
94
94
  process.env.platform = platform;
95
95
  // 剪切
@@ -103,7 +103,7 @@ const start = async (options = {}) => {
103
103
  }
104
104
  else {
105
105
  // 如果没有指定平台,则使用登录名作为平台
106
- process.env.platform = `@alemonjs/${login}`;
106
+ process.env.platform = `${default_platform_common_prefix}${login}`;
107
107
  process.env.login = login;
108
108
  }
109
109
  // 设置了 login。强制指定
@@ -32,7 +32,5 @@ type Events = {
32
32
  } & EventsMessageCreate;
33
33
  type EventKeys = keyof Events;
34
34
  type EventsEnum = Events[EventKeys];
35
- declare const EventsKeyEnum: EventKeys[];
36
35
 
37
- export { EventsKeyEnum };
38
36
  export type { EventKeys, Events, EventsEnum, EventsMessageCreate, EventsMessageCreateEnum, EventsMessageCreateKeys };
@@ -3,7 +3,7 @@ export { ButtonRow, DataButton } from './button.js';
3
3
  import { DataArkList, DataArkCard, DataArkBigCard } from './ark.js';
4
4
  export { DataArkListContent, DataArkListItem, DataArkListTip } from './ark.js';
5
5
  import { DataMarkDown, DataMarkdownTemplate } from './markdown.js';
6
- export { DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownText, DataMarkdownTitle } from './markdown.js';
6
+ export { DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownCode, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownText, DataMarkdownTitle } from './markdown.js';
7
7
  import { DataImage, DataImageURL, DataImageFile } from './image.js';
8
8
  import { DataText } from './text.js';
9
9
  import { DataMention } from './mention.js';
@@ -75,10 +75,17 @@ type DataMarkdownNewline = {
75
75
  type: 'MD.newline';
76
76
  value: boolean;
77
77
  };
78
- type DataMarkDownBalue = DataMarkdownText | DataMarkdownTitle | DataMarkdownSubtitle | DataMarkdownBold | DataMarkdownItalic | DataMarkdownItalicStar | DataMarkdownStrikethrough | DataMarkdownLink | DataMarkdownImage | DataMarkdownList | DataMarkdownBlockquote | DataMarkdownDivider | DataMarkdownNewline;
78
+ type DataMarkdownCode = {
79
+ type: 'MD.code';
80
+ value: string;
81
+ options?: {
82
+ language?: string;
83
+ };
84
+ };
85
+ type DataMarkDownBalue = DataMarkdownText | DataMarkdownTitle | DataMarkdownSubtitle | DataMarkdownBold | DataMarkdownItalic | DataMarkdownItalicStar | DataMarkdownStrikethrough | DataMarkdownLink | DataMarkdownImage | DataMarkdownList | DataMarkdownBlockquote | DataMarkdownDivider | DataMarkdownNewline | DataMarkdownCode;
79
86
  type DataMarkDown = {
80
87
  type: 'Markdown';
81
88
  value: DataMarkDownBalue[];
82
89
  };
83
90
 
84
- export type { DataMarkDown, DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle };
91
+ export type { DataMarkDown, DataMarkdownBlockquote, DataMarkdownBold, DataMarkdownCode, DataMarkdownDivider, DataMarkdownImage, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownLink, DataMarkdownList, DataMarkdownListItem, DataMarkdownNewline, DataMarkdownStrikethrough, DataMarkdownSubtitle, DataMarkdownTemplate, DataMarkdownText, DataMarkdownTitle };
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PathLike } from 'fs';
2
- import { Readable } from 'stream';
2
+ import { Readable } from 'node:stream';
3
3
  import { Options } from 'public-ip';
4
4
 
5
5
  /**
@@ -8,19 +8,13 @@ import { Options } from 'public-ip';
8
8
  * @returns
9
9
  */
10
10
  declare const getBufferByURL: (url: string) => Promise<Buffer>;
11
- /**
12
- * 通过路径获取Buffer
13
- * @param val
14
- * @returns
15
- */
16
- declare const getBufferByPath: (val: string) => Buffer | undefined;
17
11
  /**
18
12
  * 生成二维码
19
13
  * @param text
20
- * @param localpath
14
+ * @param targetPath
21
15
  * @returns
22
16
  */
23
- declare const createQRCode: (text: string, localpath?: string) => Promise<Buffer | false>;
17
+ declare const createQRCode: (text: string, targetPath?: string) => Promise<Buffer | false>;
24
18
  declare class Counter {
25
19
  #private;
26
20
  /**
@@ -69,7 +63,9 @@ declare function createPicFrom(options: {
69
63
  * @param options
70
64
  * @returns
71
65
  */
72
- declare const getPublicIP: (options?: Options) => Promise<string>;
66
+ declare const getPublicIP: (options?: Options & {
67
+ force?: boolean;
68
+ }) => Promise<string>;
73
69
  /**
74
70
  * 正则表达式工具类
75
71
  */
@@ -81,4 +77,4 @@ declare class Regular extends RegExp {
81
77
  and(...regs: RegExp[]): Regular;
82
78
  }
83
79
 
84
- export { Counter, Regular, createPicFrom, createQRCode, getBufferByPath, getBufferByURL, getPublicIP };
80
+ export { Counter, Regular, createPicFrom, createQRCode, getBufferByURL, getPublicIP };
package/lib/utils.js CHANGED
@@ -1,8 +1,8 @@
1
- import { existsSync, readFileSync, writeFile, createReadStream } from 'fs';
1
+ import { writeFile, existsSync, createReadStream } from 'fs';
2
2
  import axios from 'axios';
3
- import { join, basename } from 'path';
4
3
  import { toDataURL } from 'qrcode';
5
- import { isReadable, Readable } from 'stream';
4
+ import { isReadable, Readable } from 'node:stream';
5
+ import { basename } from 'path';
6
6
  import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
7
7
  import { publicIp } from 'public-ip';
8
8
 
@@ -18,27 +18,13 @@ const getBufferByURL = async (url) => {
18
18
  })
19
19
  .then(res => Buffer.from(res.data, 'binary'));
20
20
  };
21
- /**
22
- * 通过路径获取Buffer
23
- * @param val
24
- * @returns
25
- */
26
- const getBufferByPath = (val) => {
27
- const add = join(process.cwd(), val);
28
- // 绝对路径
29
- if (existsSync(add))
30
- return Buffer.from(readFileSync(add));
31
- // 相对路径
32
- if (existsSync(val))
33
- return Buffer.from(readFileSync(val));
34
- };
35
21
  /**
36
22
  * 生成二维码
37
23
  * @param text
38
- * @param localpath
24
+ * @param targetPath
39
25
  * @returns
40
26
  */
41
- const createQRCode = async (text, localpath) => {
27
+ const createQRCode = async (text, targetPath) => {
42
28
  try {
43
29
  const qrDataURL = await new Promise((resolve, reject) => {
44
30
  toDataURL(text, {
@@ -55,11 +41,11 @@ const createQRCode = async (text, localpath) => {
55
41
  });
56
42
  });
57
43
  const bufferData = Buffer.from(qrDataURL.split(',')[1], 'base64');
58
- if (localpath != undefined) {
59
- writeFile(localpath, bufferData, (err) => {
44
+ if (targetPath) {
45
+ writeFile(targetPath, bufferData, (err) => {
60
46
  if (err)
61
47
  throw err;
62
- console.info('buffer set', localpath);
48
+ console.info(targetPath);
63
49
  });
64
50
  }
65
51
  return bufferData;
@@ -182,11 +168,12 @@ async function createPicFrom(options) {
182
168
  * @returns
183
169
  */
184
170
  const getPublicIP = async (options = {}) => {
185
- if (global.publicip)
171
+ const { force, ...config } = options;
172
+ if (global.publicip && !force)
186
173
  return global.publicip;
187
174
  return await publicIp({
188
175
  onlyHttps: true,
189
- ...options
176
+ ...config
190
177
  }).then(ip => {
191
178
  global.publicip = ip;
192
179
  return global.publicip;
@@ -213,4 +200,4 @@ class Regular extends RegExp {
213
200
  }
214
201
  }
215
202
 
216
- export { Counter, Regular, createPicFrom, createQRCode, getBufferByPath, getBufferByURL, getPublicIP };
203
+ export { Counter, Regular, createPicFrom, createQRCode, getBufferByURL, getPublicIP };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.1.0-alpha.16",
3
+ "version": "2.1.0-alpha.18",
4
4
  "description": "bot script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -34,7 +34,12 @@
34
34
  "koa-static": "^5.0.0",
35
35
  "@koa/cors": "^5.0.0",
36
36
  "ws": "^8.18.0",
37
- "flatted": "^3.3.3"
37
+ "flatted": "^3.3.3",
38
+ "uuid": "11.1.0",
39
+ "public-ip": "^7.0.1",
40
+ "axios": "^1.10.0",
41
+ "file-type": "21.0.0",
42
+ "qrcode": "^1.5.4"
38
43
  },
39
44
  "devDependencies": {
40
45
  "@types/uuid": "^10.0.0",
@@ -1,77 +0,0 @@
1
- import { Dirent } from 'fs';
2
- import { ResultCode } from '../core/code.js';
3
-
4
- /**
5
- * 将字符串转为定长字符串
6
- * @param str 输入字符串
7
- * @param options 可选项
8
- * @returns 固定长度的哈希值
9
- */
10
- declare const createHash: (str: string, options?: {
11
- length?: number;
12
- algorithm?: string;
13
- }) => string;
14
- /**
15
- * 使用用户的哈希键
16
- * @param e
17
- * @returns
18
- */
19
- declare const useUserHashKey: (event: {
20
- UserId: string;
21
- Platform: string;
22
- }) => string;
23
- /**
24
- * 创建app名称
25
- * @param url
26
- * @param app 模块名
27
- * @param select 选择事件类型,默认 res
28
- * @returns
29
- */
30
- declare const createEventName: (url: string, appKey: string) => string;
31
- /**
32
- * 将字符串转为数字
33
- * @param str
34
- * @returns
35
- */
36
- declare const stringToNumber: (str: string, size?: number) => number;
37
- /**
38
- * 递归获取所有文件
39
- * @param dir
40
- * @param condition
41
- * @returns
42
- */
43
- declare const getRecursiveDirFiles: (dir: string, condition?: (func: Dirent) => boolean) => {
44
- path: string;
45
- name: string;
46
- }[];
47
- /**
48
- * 解析错误并提示缺失的模块
49
- * @param e
50
- * @returns
51
- */
52
- declare const showErrorModule: (e: Error) => void;
53
- declare const getInputExportPath: (input?: string) => any;
54
- /**
55
- * 属于直接调用的函数
56
- * 参数错误直接抛出错误
57
- * 如果函数内部有错误,则使用 Result 进行返回
58
- */
59
- /**
60
- * 异步方法。且需要读取返回值的进行判断的,
61
- * 都要以 Result 作为返回值
62
- */
63
- type Result = {
64
- code: ResultCode;
65
- message: string | Object;
66
- data: any;
67
- };
68
- /**
69
- * 创建结果
70
- * @param code
71
- * @param message
72
- * @returns
73
- */
74
- declare const createResult: (code: ResultCode, message: string | Object, data?: any) => Result;
75
-
76
- export { createEventName, createHash, createResult, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
77
- export type { Result };
package/lib/app/utils.js DELETED
@@ -1,162 +0,0 @@
1
- import crypto from 'crypto';
2
- import fs, { existsSync, readdirSync } from 'fs';
3
- import path, { join } from 'path';
4
- import { createRequire } from 'module';
5
- import { ResultCode } from '../core/code.js';
6
- import { file_suffix_response } from '../core/variable.js';
7
-
8
- const require = createRequire(import.meta.url);
9
- /**
10
- * 将字符串转为定长字符串
11
- * @param str 输入字符串
12
- * @param options 可选项
13
- * @returns 固定长度的哈希值
14
- */
15
- const createHash = (str, options = {}) => {
16
- const { length = 11, algorithm = 'sha256' } = options;
17
- // 使用 crypto 生成哈希
18
- const hash = crypto.createHash(algorithm).update(str).digest('hex');
19
- // 截取指定长度
20
- return hash.slice(0, length);
21
- };
22
- /**
23
- * 使用用户的哈希键
24
- * @param e
25
- * @returns
26
- */
27
- const useUserHashKey = (event) => {
28
- return createHash(`${event.Platform}:${event.UserId}`);
29
- };
30
- /**
31
- * 创建app名称
32
- * @param url
33
- * @param app 模块名
34
- * @param select 选择事件类型,默认 res
35
- * @returns
36
- */
37
- const createEventName = (url, appKey) => {
38
- let uri = url;
39
- if (process.platform === 'win32') {
40
- uri = uri.replace(/\\/g, '/');
41
- }
42
- // 去掉空字符串
43
- const names = uri.split('/').filter(item => item !== '');
44
- // 去掉最后一个文件名
45
- names.pop();
46
- const name = `${appKey}:${names.join(':')}`;
47
- return name;
48
- };
49
- /**
50
- * 将字符串转为数字
51
- * @param str
52
- * @returns
53
- */
54
- const stringToNumber = (str, size = 33) => {
55
- let hash = 5381;
56
- let i = str.length;
57
- while (i) {
58
- hash = (hash * size) ^ str.charCodeAt(--i);
59
- }
60
- /*JavaScript对32位签名执行逐位操作(如上面的XOR)
61
- *整数。由于我们希望结果始终为正,因此转换
62
- *通过执行无符号位移,将带符号的int转换为无符号*/
63
- return hash >>> 0;
64
- };
65
- /**
66
- * 递归获取所有文件
67
- * @param dir
68
- * @param condition
69
- * @returns
70
- */
71
- const getRecursiveDirFiles = (dir, condition = item => file_suffix_response.test(item.name)) => {
72
- //
73
- let results = [];
74
- if (!existsSync(dir))
75
- return results;
76
- const list = readdirSync(dir, { withFileTypes: true });
77
- list.forEach(item => {
78
- const fullPath = join(dir, item.name);
79
- if (item.isDirectory()) {
80
- results = results.concat(getRecursiveDirFiles(fullPath, condition));
81
- }
82
- else if (item.isFile() && condition(item)) {
83
- results.push({
84
- path: fullPath,
85
- name: item.name
86
- });
87
- }
88
- });
89
- return results;
90
- };
91
- /**
92
- * 解析错误并提示缺失的模块
93
- * @param e
94
- * @returns
95
- */
96
- const showErrorModule = (e) => {
97
- if (!e)
98
- return;
99
- const moduleNotFoundRegex = /Cannot find (module|package)/;
100
- // 处理模块未找到的错误
101
- if (moduleNotFoundRegex.test(e?.message)) {
102
- const match = e.stack?.match(/'(.+?)'/);
103
- if (match) {
104
- const pack = match[1];
105
- logger.error({
106
- code: ResultCode.FailInternal,
107
- message: `缺少模块或依赖 ${pack},请安装`,
108
- data: null
109
- });
110
- return;
111
- }
112
- }
113
- // 处理其他错误
114
- logger.error({
115
- code: ResultCode.FailInternal,
116
- message: e?.message,
117
- data: e
118
- });
119
- };
120
- const createExports = (packageJson) => {
121
- if (packageJson?.exports) {
122
- if (typeof packageJson.exports === 'string') {
123
- return packageJson.exports;
124
- }
125
- else if (typeof packageJson.exports === 'object') {
126
- return packageJson.exports['.'] || packageJson.exports['./index.js'];
127
- }
128
- }
129
- };
130
- const getInputExportPath = (input) => {
131
- const packageJsonPath = path.join(input ?? process.cwd(), 'package.json');
132
- if (fs.existsSync(packageJsonPath)) {
133
- const packageJson = require(packageJsonPath);
134
- const main = packageJson?.main || createExports(packageJson);
135
- if (main) {
136
- return main;
137
- }
138
- }
139
- };
140
- /**
141
- * 创建结果
142
- * @param code
143
- * @param message
144
- * @returns
145
- */
146
- const createResult = (code, message, data) => {
147
- // 如果不是 2000。则logger
148
- if (code !== ResultCode.Ok) {
149
- logger.error({
150
- code,
151
- message,
152
- data: data
153
- });
154
- }
155
- return {
156
- code,
157
- message,
158
- data: data
159
- };
160
- };
161
-
162
- export { createEventName, createHash, createResult, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
@@ -1,3 +0,0 @@
1
- declare const cbpServer: (port: number, listeningListener?: () => void) => void
2
-
3
- export { cbpServer }
@@ -1,20 +0,0 @@
1
- const EventsKeyEnum = [
2
- 'message.create',
3
- 'message.update',
4
- 'message.delete',
5
- 'message.reaction.add',
6
- 'message.reaction.remove',
7
- 'private.message.create',
8
- 'private.message.update',
9
- 'private.message.delete',
10
- 'private.friend.add',
11
- 'private.guild.add',
12
- 'channal.create',
13
- 'channal.delete',
14
- 'guild.join',
15
- 'guild.exit',
16
- 'member.add',
17
- 'member.remove'
18
- ];
19
-
20
- export { EventsKeyEnum };