hotcat-bot-qq 0.1.5 → 0.1.6

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.
@@ -13,7 +13,7 @@ export type NoticeEvent = Record<string, any> & {
13
13
  sub_type?: string;
14
14
  };
15
15
  /** BotConsole 日志分类 */
16
- export type BotConsoleType = 'system' | 'groupMessage' | 'privateMessage' | 'error' | 'sendGroupMessage' | 'sendPrivateMessage' | 'groupMessageSelf' | 'privateMessageSelf' | 'requestFriend' | 'requestGroupAdd' | 'requestGroupInvite' | 'notice';
16
+ export type BotConsoleType = 'system' | 'groupMessage' | 'privateMessage' | 'error' | 'sendGroupMessage' | 'sendPrivateMessage' | 'groupMessageSelf' | 'privateMessageSelf' | 'requestFriend' | 'requestGroupAdd' | 'requestGroupInvite' | 'notice' | 'plugin';
17
17
  /** ANSI 颜色/样式码 */
18
18
  export declare const colors: {
19
19
  reset: string;
@@ -66,7 +66,13 @@ export declare const formatMessage: (m: Message) => string;
66
66
  */
67
67
  export declare class BotConsole {
68
68
  type: BotConsoleType;
69
- content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent;
70
- constructor(type: BotConsoleType, content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent);
69
+ content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent | {
70
+ name: string;
71
+ msg: string;
72
+ };
73
+ constructor(type: BotConsoleType, content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent | {
74
+ name: string;
75
+ msg: string;
76
+ });
71
77
  log(): void;
72
78
  }
@@ -293,5 +293,9 @@ export class BotConsole {
293
293
  ('name_new' in event ? `${colors.green}${sanitize(event.name_new)}${colors.reset}` : '') +
294
294
  ('file' in event ? `文件:${event.file.name}` : ''));
295
295
  }
296
+ else if (this.type === 'plugin') {
297
+ const { name, msg } = this.content;
298
+ console.log(`${colors.magenta}[plugin:${name}]:${colors.reset} ${msg}`);
299
+ }
296
300
  }
297
301
  }
@@ -49,6 +49,7 @@ export declare class BotPluginSystem {
49
49
  list(): string[];
50
50
  /** 返回所有已加载的插件名列表 */
51
51
  loaded(): string[];
52
+ private findIndex;
52
53
  /**
53
54
  * 扫描目录,自动注册并加载所有插件
54
55
  * @param dir - 插件目录路径
package/dist/botPlugin.js CHANGED
@@ -70,7 +70,7 @@ export class BotPluginSystem {
70
70
  throw new Error(`插件 "${name}" 已注册`);
71
71
  }
72
72
  this.registry.set(name, { ctor, meta });
73
- new BotConsole('system', `插件 "${meta.name}" 已注册`).log();
73
+ new BotConsole('plugin', { name: meta.name, msg: '已注册' }).log();
74
74
  }
75
75
  /**
76
76
  * 加载插件
@@ -87,7 +87,7 @@ export class BotPluginSystem {
87
87
  const instance = record.ctor.create(this.api, this.client, record.meta);
88
88
  await instance.load();
89
89
  this.instances.set(name, instance);
90
- new BotConsole('system', `插件 "${record.meta.name}" 已加载`).log();
90
+ new BotConsole('plugin', { name: record.meta.name, msg: '已加载' }).log();
91
91
  }
92
92
  /**
93
93
  * 卸载插件
@@ -100,7 +100,7 @@ export class BotPluginSystem {
100
100
  }
101
101
  await instance.unload();
102
102
  this.instances.delete(name);
103
- new BotConsole('system', `插件 "${name}" 已卸载`).log();
103
+ new BotConsole('plugin', { name: this.registry.get(name)?.meta.name || name, msg: '已卸载' }).log();
104
104
  }
105
105
  /**
106
106
  * 热重载:先卸载再加载
@@ -122,6 +122,15 @@ export class BotPluginSystem {
122
122
  loaded() {
123
123
  return Array.from(this.instances.keys());
124
124
  }
125
+ findIndex(dir, name) {
126
+ const js = path.join(dir, name, 'index.js');
127
+ if (fs.existsSync(js))
128
+ return js;
129
+ const ts = path.join(dir, name, 'index.ts');
130
+ if (fs.existsSync(ts))
131
+ return ts;
132
+ return null;
133
+ }
125
134
  /**
126
135
  * 扫描目录,自动注册并加载所有插件
127
136
  * @param dir - 插件目录路径
@@ -137,8 +146,8 @@ export class BotPluginSystem {
137
146
  if (!entry.isDirectory())
138
147
  continue;
139
148
  const name = entry.name;
140
- const indexPath = path.join(dir, name, 'index.ts');
141
- if (!fs.existsSync(indexPath))
149
+ const indexPath = this.findIndex(dir, name);
150
+ if (!indexPath)
142
151
  continue;
143
152
  try {
144
153
  const mod = await import(path.resolve(indexPath));
@@ -179,7 +188,7 @@ export class BotPluginSystem {
179
188
  return;
180
189
  }
181
190
  this.watched = new Set(this.list());
182
- new BotConsole('system', `开始监听插件目录: ${this.watchDir}`).log();
191
+ new BotConsole('plugin', { name: 'watch', msg: `开始监听 ${this.watchDir}` }).log();
183
192
  this.watcher = setInterval(async () => {
184
193
  if (!fs.existsSync(this.watchDir))
185
194
  return;
@@ -189,8 +198,8 @@ export class BotPluginSystem {
189
198
  if (!entry.isDirectory())
190
199
  continue;
191
200
  const name = entry.name;
192
- const indexPath = path.join(this.watchDir, name, 'index.ts');
193
- if (!fs.existsSync(indexPath))
201
+ const indexPath = this.findIndex(this.watchDir, name);
202
+ if (!indexPath)
194
203
  continue;
195
204
  current.add(name);
196
205
  if (!this.watched.has(name)) {
@@ -199,12 +208,13 @@ export class BotPluginSystem {
199
208
  }
200
209
  for (const name of Array.from(this.watched)) {
201
210
  if (!current.has(name) && this.registry.has(name)) {
211
+ const displayName = this.registry.get(name)?.meta.name || name;
202
212
  try {
203
213
  if (this.instances.has(name)) {
204
214
  await this.unload(name);
205
215
  }
206
216
  this.registry.delete(name);
207
- new BotConsole('system', `插件 "${name}" 目录已删除,已移除`).log();
217
+ new BotConsole('plugin', { name: displayName, msg: '目录已删除,已移除' }).log();
208
218
  }
209
219
  catch { }
210
220
  }
@@ -218,11 +228,13 @@ export class BotPluginSystem {
218
228
  clearInterval(this.watcher);
219
229
  this.watcher = null;
220
230
  this.watchDir = null;
221
- new BotConsole('system', '已停止插件目录监听').log();
231
+ new BotConsole('plugin', { name: 'watch', msg: '已停止' }).log();
222
232
  }
223
233
  }
224
234
  async scanOne(dir, name) {
225
- const indexPath = path.join(dir, name, 'index.ts');
235
+ const indexPath = this.findIndex(dir, name);
236
+ if (!indexPath)
237
+ return;
226
238
  try {
227
239
  const mod = await import(indexPath);
228
240
  const ctor = mod[name.charAt(0).toUpperCase() + name.slice(1) + 'Plugin'] || mod.default || Object.values(mod).find((v) => typeof v === 'function' && v.create);
@@ -234,7 +246,7 @@ export class BotPluginSystem {
234
246
  };
235
247
  this.register(name, ctor, meta);
236
248
  await this.load(name);
237
- new BotConsole('system', `检测到新插件 "${name}",已自动加载`).log();
249
+ new BotConsole('plugin', { name: meta.name, msg: '已自动加载' }).log();
238
250
  }
239
251
  catch (e) {
240
252
  new BotConsole('error', `自动加载插件 "${name}" 失败: ${e.message || e}`).log();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hotcat-bot-qq",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "基于 node-napcat-ts 的 TypeScript QQ 机器人框架",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -7,7 +7,7 @@ import * as os from 'os'
7
7
  export class HotCatPlugin extends PluginBase {
8
8
  static meta = {
9
9
  name: 'hotCatPlugin',
10
- version: '0.1.5',
10
+ version: '0.1.6',
11
11
  description: 'HotCat 默认插件(#hotCat 指令查询 bot 状态)',
12
12
  author: 'CHCAT1320',
13
13
  }