hotcat-bot-qq 0.1.10 → 0.1.11

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.
package/README.md CHANGED
@@ -25,6 +25,7 @@ import { Message } from './message'
25
25
  const bot = new BotClient({
26
26
  baseUrl: 'ws://localhost:8082/onebot/v11/ws/',
27
27
  accessToken: 'your-access-token',
28
+ owner: 123456789, // 必填:bot 主人(管理员)QQ 号
28
29
  })
29
30
  await bot.start()
30
31
 
@@ -5,6 +5,8 @@ import { BotScheduler } from './botScheduler';
5
5
  export interface BotConfig {
6
6
  baseUrl: string;
7
7
  accessToken?: string;
8
+ /** Bot 主人(管理员)QQ 号,必填,未配置将拒绝启动 */
9
+ owner: number;
8
10
  reconnection?: {
9
11
  enable?: boolean;
10
12
  attempts?: number;
@@ -19,6 +21,7 @@ export interface BotConfig {
19
21
  * const bot = new BotClient({
20
22
  * baseUrl: 'ws://localhost:8082/onebot/v11/ws/',
21
23
  * accessToken: 'yourAccessToken',
24
+ * owner: 123456789,
22
25
  * })
23
26
  * await bot.start()
24
27
  */
@@ -29,7 +32,14 @@ export declare class BotClient {
29
32
  plugin: import('./botPlugin').BotPluginSystem;
30
33
  nickname: string | null;
31
34
  id: number | null;
35
+ /** Bot 主人(管理员)QQ 号,来自配置项 `owner` */
36
+ owner: number;
32
37
  constructor(config: BotConfig);
38
+ /**
39
+ * 判断指定 QQ 号是否为 bot 主人
40
+ * @param qq - 待判断的 QQ 号
41
+ */
42
+ isOwner(qq: number | string): boolean;
33
43
  /**
34
44
  * 连接 napcat 并初始化事件监听,启动成功后自动填充 {@link nickname} 和 {@link id}。
35
45
  */
package/dist/botClient.js CHANGED
@@ -12,6 +12,7 @@ import { fileURLToPath } from 'url';
12
12
  * const bot = new BotClient({
13
13
  * baseUrl: 'ws://localhost:8082/onebot/v11/ws/',
14
14
  * accessToken: 'yourAccessToken',
15
+ * owner: 123456789,
15
16
  * })
16
17
  * await bot.start()
17
18
  */
@@ -53,13 +54,39 @@ export class BotClient {
53
54
  writable: true,
54
55
  value: void 0
55
56
  });
56
- this.api = new BotApi(config);
57
+ /** Bot 主人(管理员)QQ 号,来自配置项 `owner` */
58
+ Object.defineProperty(this, "owner", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: void 0
63
+ });
64
+ const owner = Number(config?.owner);
65
+ if (!Number.isInteger(owner) || owner <= 0) {
66
+ throw new Error('未配置 bot 主人 QQ 号:请在 BotClient 配置中填写 owner 字段,例如 owner: 123456789,未配置前无法启动');
67
+ }
68
+ const baseUrl = typeof config?.baseUrl === 'string' ? config.baseUrl.trim() : '';
69
+ if (!baseUrl) {
70
+ throw new Error('未配置 NapCat WebSocket 地址:请在 BotClient 配置中填写 baseUrl 字段,例如 baseUrl: "ws://localhost:8082/onebot/v11/ws/",未配置前无法启动');
71
+ }
72
+ if (!/^wss?:\/\/\S+$/i.test(baseUrl)) {
73
+ throw new Error('baseUrl 格式错误:必须以 ws:// 或 wss:// 开头,例如 ws://localhost:8082/onebot/v11/ws/');
74
+ }
75
+ this.owner = owner;
76
+ this.api = new BotApi({ ...config, baseUrl });
57
77
  this.api.client = this;
58
78
  this.event = new BotEvent(this.api, this);
59
79
  this.scheduler = new BotScheduler();
60
80
  this.nickname = null;
61
81
  this.id = null;
62
82
  }
83
+ /**
84
+ * 判断指定 QQ 号是否为 bot 主人
85
+ * @param qq - 待判断的 QQ 号
86
+ */
87
+ isOwner(qq) {
88
+ return Number(qq) === this.owner;
89
+ }
63
90
  /**
64
91
  * 连接 napcat 并初始化事件监听,启动成功后自动填充 {@link nickname} 和 {@link id}。
65
92
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hotcat-bot-qq",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "基于 node-napcat-ts 的 TypeScript QQ 机器人框架",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -52,8 +52,8 @@
52
52
  ],
53
53
  "scripts": {
54
54
  "build": "tsc -p tsconfig.build.json && node scripts/fix-dist-extensions.mjs",
55
- "build:plugin": "tsc --outDir plugins/hotCatPlugin --rootDir plugins/hotCatPlugin --module ESNext --target ES2020 --moduleResolution bundler --declaration --skipLibCheck plugins/hotCatPlugin/index.ts",
56
- "prepublishOnly": "bun run build && bun run build:plugin",
55
+ "build:plugin": "bun run build && tsc --outDir plugins/hotCatPlugin --rootDir plugins/hotCatPlugin --module ESNext --target ES2020 --moduleResolution bundler --declaration --skipLibCheck plugins/hotCatPlugin/index.ts",
56
+ "prepublishOnly": "bun run build:plugin",
57
57
  "docs:dev": "vitepress dev docs",
58
58
  "docs:build": "vitepress build docs",
59
59
  "docs:preview": "vitepress preview docs"
@@ -12,6 +12,9 @@ export declare class HotCatPlugin extends PluginBase {
12
12
  static create(api: BotApi, bot: BotClient): HotCatPlugin;
13
13
  load(): Promise<void>;
14
14
  unload(): Promise<void>;
15
+ private isInfoCommand;
16
+ private buildInfo;
15
17
  private onGroupMsg;
18
+ private onPrivateMsg;
16
19
  private formatUptime;
17
20
  }
@@ -7,20 +7,15 @@ export class HotCatPlugin extends PluginBase {
7
7
  this.startTime = 0;
8
8
  this.onGroupMsg = async (_bot, event) => {
9
9
  const msg = event.raw_message.trim();
10
- if (msg === '#hotCat' || msg === '#hotcat') {
11
- const uptime = this.formatUptime(Date.now() - this.startTime);
12
- const platform = os.platform();
13
- const arch = os.arch();
14
- await this.api.sendGroupMessage(event.group_id, Message.reply(event.message_id), Message.text(`HotCat 信息\n` +
15
- `版本:${this.meta.version}\n` +
16
- `平台:${platform} ${arch}\n` +
17
- `运行时间:${uptime}\n` +
18
- `Github: https://github.com/CHCAT1320/hotCat-bot-qq\n` +
19
- `开发文档: https://hotcat-bot-qq.docs.chcat1320.top\n` +
20
- `交流群: 638144340\n` +
21
- `管理员QQ: 1095216448\n` +
22
- `基于 MIT 许可发布 版权所有 © 2026 CHCAT1320`));
23
- }
10
+ if (!this.isInfoCommand(msg))
11
+ return;
12
+ await this.api.sendGroupMessage(event.group_id, Message.reply(event.message_id), Message.text(this.buildInfo()));
13
+ };
14
+ this.onPrivateMsg = async (_bot, event) => {
15
+ const msg = event.raw_message.trim();
16
+ if (!this.isInfoCommand(msg))
17
+ return;
18
+ await this.api.sendPrivateMessage(event.user_id, Message.text(this.buildInfo()));
24
19
  };
25
20
  }
26
21
  static create(api, bot) {
@@ -29,9 +24,28 @@ export class HotCatPlugin extends PluginBase {
29
24
  async load() {
30
25
  this.startTime = Date.now();
31
26
  this.bot.event.message.onGroupMessage(this.onGroupMsg);
27
+ this.bot.event.message.onPrivateMessage(this.onPrivateMsg);
32
28
  }
33
29
  async unload() {
34
30
  this.bot.event.message.offGroupMessage(this.onGroupMsg);
31
+ this.bot.event.message.offPrivateMessage(this.onPrivateMsg);
32
+ }
33
+ isInfoCommand(msg) {
34
+ return msg === '#hotCat' || msg === '#hotcat';
35
+ }
36
+ buildInfo() {
37
+ const uptime = this.formatUptime(Date.now() - this.startTime);
38
+ const platform = os.platform();
39
+ const arch = os.arch();
40
+ return `HotCat 信息\n` +
41
+ `版本:${this.meta.version}\n` +
42
+ `平台:${platform} ${arch}\n` +
43
+ `运行时间:${uptime}\n` +
44
+ `管理员QQ: ${this.bot.owner}\n` +
45
+ `Github: https://github.com/CHCAT1320/hotCat-bot-qq\n` +
46
+ `开发文档: https://hotcat-bot-qq.docs.chcat1320.top\n` +
47
+ `交流群: 638144340\n` +
48
+ `基于 MIT 许可发布 版权所有 © 2026 CHCAT1320`;
35
49
  }
36
50
  formatUptime(ms) {
37
51
  const s = Math.floor(ms / 1000);
@@ -50,7 +64,7 @@ export class HotCatPlugin extends PluginBase {
50
64
  }
51
65
  HotCatPlugin.meta = {
52
66
  name: 'hotCatPlugin',
53
- version: '0.1.10',
54
- description: 'HotCat 默认插件(#hotCat 指令查询 bot 状态)',
67
+ version: '0.1.11',
68
+ description: 'HotCat 默认插件(群聊/私聊 #hotCat 指令查询 bot 状态)',
55
69
  author: 'CHCAT1320',
56
70
  };
@@ -7,8 +7,8 @@ import * as os from 'os'
7
7
  export class HotCatPlugin extends PluginBase {
8
8
  static meta = {
9
9
  name: 'hotCatPlugin',
10
- version: '0.1.10',
11
- description: 'HotCat 默认插件(#hotCat 指令查询 bot 状态)',
10
+ version: '0.1.11',
11
+ description: 'HotCat 默认插件(群聊/私聊 #hotCat 指令查询 bot 状态)',
12
12
  author: 'CHCAT1320',
13
13
  }
14
14
 
@@ -21,35 +21,51 @@ export class HotCatPlugin extends PluginBase {
21
21
  async load() {
22
22
  this.startTime = Date.now()
23
23
  this.bot.event.message.onGroupMessage(this.onGroupMsg)
24
+ this.bot.event.message.onPrivateMessage(this.onPrivateMsg)
24
25
  }
25
26
 
26
27
  async unload() {
27
28
  this.bot.event.message.offGroupMessage(this.onGroupMsg)
29
+ this.bot.event.message.offPrivateMessage(this.onPrivateMsg)
30
+ }
31
+
32
+ private isInfoCommand(msg: string): boolean {
33
+ return msg === '#hotCat' || msg === '#hotcat'
34
+ }
35
+
36
+ private buildInfo(): string {
37
+ const uptime = this.formatUptime(Date.now() - this.startTime)
38
+ const platform = os.platform()
39
+ const arch = os.arch()
40
+
41
+ return `HotCat 信息\n` +
42
+ `版本:${this.meta.version}\n` +
43
+ `平台:${platform} ${arch}\n` +
44
+ `运行时间:${uptime}\n` +
45
+ `管理员QQ: ${this.bot.owner}\n` +
46
+ `Github: https://github.com/CHCAT1320/hotCat-bot-qq\n` +
47
+ `开发文档: https://hotcat-bot-qq.docs.chcat1320.top\n` +
48
+ `交流群: 638144340\n` +
49
+ `基于 MIT 许可发布 版权所有 © 2026 CHCAT1320`
28
50
  }
29
51
 
30
52
  private onGroupMsg = async (_bot: BotClient, event: any) => {
31
53
  const msg = event.raw_message.trim()
54
+ if (!this.isInfoCommand(msg)) return
55
+
56
+ await this.api.sendGroupMessage(event.group_id,
57
+ Message.reply(event.message_id),
58
+ Message.text(this.buildInfo())
59
+ )
60
+ }
32
61
 
33
- if (msg === '#hotCat' || msg === '#hotcat') {
34
- const uptime = this.formatUptime(Date.now() - this.startTime)
35
- const platform = os.platform()
36
- const arch = os.arch()
62
+ private onPrivateMsg = async (_bot: BotClient, event: any) => {
63
+ const msg = event.raw_message.trim()
64
+ if (!this.isInfoCommand(msg)) return
37
65
 
38
- await this.api.sendGroupMessage(event.group_id,
39
- Message.reply(event.message_id),
40
- Message.text(
41
- `HotCat 信息\n` +
42
- `版本:${this.meta.version}\n` +
43
- `平台:${platform} ${arch}\n` +
44
- `运行时间:${uptime}\n` +
45
- `Github: https://github.com/CHCAT1320/hotCat-bot-qq\n` +
46
- `开发文档: https://hotcat-bot-qq.docs.chcat1320.top\n` +
47
- `交流群: 638144340\n` +
48
- `管理员QQ: 1095216448\n` +
49
- `基于 MIT 许可发布 版权所有 © 2026 CHCAT1320`
50
- )
51
- )
52
- }
66
+ await this.api.sendPrivateMessage(event.user_id,
67
+ Message.text(this.buildInfo())
68
+ )
53
69
  }
54
70
 
55
71
  private formatUptime(ms: number): string {