alemonjs 2.1.0-alpha.28 → 2.1.0-alpha.29

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 (2) hide show
  1. package/lib/cbp/testone.js +47 -36
  2. package/package.json +1 -1
@@ -38,43 +38,27 @@ const connectionTestOne = (ws, _request) => {
38
38
  if (!existsSync(testonePath)) {
39
39
  mkdirSync(testonePath, { recursive: true });
40
40
  }
41
- const fileWatchers = new Map();
42
- // 通用的文件监听函数
43
- const watchFile = (filePath, _type, handler) => {
44
- try {
45
- // 如果文件存在,直接监听
46
- if (existsSync(filePath)) {
47
- const watcher = watch(filePath, { persistent: true }, (eventType, filename) => {
48
- if (eventType === 'change' && filename) {
49
- handler();
50
- }
51
- });
52
- fileWatchers.set(filePath, watcher);
53
- }
54
- }
55
- catch (error) {
56
- logger.error(`监听文件失败: ${filePath}`, error);
57
- }
58
- };
59
41
  // 监听整个目录,捕获新文件创建
60
42
  const dirWatcher = watch(testonePath, { persistent: true }, (eventType, filename) => {
61
43
  if (!filename)
62
44
  return;
63
- const filePath = join(testonePath, filename);
64
45
  // 如果是新创建的文件,开始监听它
65
- if (eventType === 'rename' && existsSync(filePath) && !fileWatchers.has(filePath)) {
46
+ if (eventType === 'change') {
66
47
  if (filename === 'commands.json') {
67
- watchFile(filePath, 'commands', onCommands);
68
48
  onCommands(); // 立即触发一次
69
49
  }
70
50
  else if (filename === 'users.json') {
71
- watchFile(filePath, 'users', onUsers);
72
51
  onUsers();
73
52
  }
74
53
  else if (filename === 'channels.json') {
75
- watchFile(filePath, 'channels', onChannels);
76
54
  onChannels();
77
55
  }
56
+ else if (filename === 'user.json') {
57
+ onUser();
58
+ }
59
+ else if (filename === 'bot.json') {
60
+ onBot();
61
+ }
78
62
  }
79
63
  });
80
64
  const commandsPath = join(testonePath, 'commands.json');
@@ -125,12 +109,38 @@ const connectionTestOne = (ws, _request) => {
125
109
  logger.error('读取 channels.json 失败:', error);
126
110
  });
127
111
  }, 1000);
128
- // 初始化时监听已存在的文件
129
- watchFile(commandsPath, 'commands', onCommands);
130
- watchFile(usersPath, 'users', onUsers);
131
- watchFile(channelsPath, 'channels', onChannels);
132
112
  const userPath = join(testonePath, 'user.json');
113
+ const onUser = _.debounce(() => {
114
+ if (!existsSync(userPath))
115
+ return;
116
+ readFile(userPath, 'utf-8')
117
+ .then(data => {
118
+ const user = JSON.parse(data);
119
+ global.testoneClient?.send(flattedJSON.stringify({
120
+ type: 'user',
121
+ payload: user
122
+ }));
123
+ })
124
+ .catch(error => {
125
+ logger.error('读取 user.json 失败:', error);
126
+ });
127
+ }, 1000);
133
128
  const botPath = join(testonePath, 'bot.json');
129
+ const onBot = _.debounce(() => {
130
+ if (!existsSync(botPath))
131
+ return;
132
+ readFile(botPath, 'utf-8')
133
+ .then(data => {
134
+ const bot = JSON.parse(data);
135
+ global.testoneClient?.send(flattedJSON.stringify({
136
+ type: 'bot',
137
+ payload: bot
138
+ }));
139
+ })
140
+ .catch(error => {
141
+ logger.error('读取 bot.json 失败:', error);
142
+ });
143
+ }, 1000);
134
144
  const privateMessagePath = join(testonePath, '.cache', 'private.message.json');
135
145
  const publicMessagePath = join(testonePath, '.cache', 'public.message.json');
136
146
  const cacheDir = dirname(privateMessagePath);
@@ -176,7 +186,7 @@ const connectionTestOne = (ws, _request) => {
176
186
  const messages = JSON.parse(readFileSync(messagePath, 'utf-8'));
177
187
  const updatedMessages = messages.filter((msg) => msg.CreateAt !== CreateAt);
178
188
  console.log('updatedMessages', updatedMessages);
179
- writeFile(messagePath, JSON.stringify(updatedMessages, null, 2), (error) => {
189
+ writeFile(messagePath, JSON.stringify(updatedMessages, null, 2), error => {
180
190
  if (error) {
181
191
  logger.error(`写入 ${type} 消息失败:`, error);
182
192
  }
@@ -189,11 +199,9 @@ const connectionTestOne = (ws, _request) => {
189
199
  };
190
200
  const onSaveMessage = (type, message) => {
191
201
  const messagePath = type === 'private' ? privateMessagePath : publicMessagePath;
192
- const messages = existsSync(messagePath)
193
- ? JSON.parse(readFileSync(messagePath, 'utf-8'))
194
- : [];
202
+ const messages = existsSync(messagePath) ? JSON.parse(readFileSync(messagePath, 'utf-8')) : [];
195
203
  messages.push(message);
196
- writeFile(messagePath, JSON.stringify(messages, null, 2), (error) => {
204
+ writeFile(messagePath, JSON.stringify(messages, null, 2), error => {
197
205
  if (error) {
198
206
  logger.error(`写入 ${type} 消息失败:`, error);
199
207
  }
@@ -256,7 +264,7 @@ const connectionTestOne = (ws, _request) => {
256
264
  initData();
257
265
  }
258
266
  else if (parsedMessage.type === 'commands') {
259
- onChannels();
267
+ onCommands();
260
268
  }
261
269
  else if (parsedMessage.type === 'users') {
262
270
  onUsers();
@@ -264,6 +272,12 @@ const connectionTestOne = (ws, _request) => {
264
272
  else if (parsedMessage.type === 'channels') {
265
273
  onChannels();
266
274
  }
275
+ else if (parsedMessage.type === 'user') {
276
+ onUser();
277
+ }
278
+ else if (parsedMessage.type === 'bot') {
279
+ onBot();
280
+ }
267
281
  else if (parsedMessage.type === 'private.message.delete') {
268
282
  const CreateAt = parsedMessage.payload.CreateAt;
269
283
  onDeleteMessage('private', CreateAt);
@@ -286,9 +300,6 @@ const connectionTestOne = (ws, _request) => {
286
300
  // 处理关闭事件
287
301
  global.testoneClient.on('close', () => {
288
302
  logger.info('WebSocket connection closed');
289
- // 清理所有文件监听器
290
- fileWatchers.forEach(watcher => watcher.close());
291
- fileWatchers.clear();
292
303
  dirWatcher.close();
293
304
  });
294
305
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.1.0-alpha.28",
3
+ "version": "2.1.0-alpha.29",
4
4
  "description": "bot script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",