hotcat-bot-qq 0.0.1

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,187 @@
1
+ const cqEscape = (v) => v.replace(/&/g, '&').replace(/\[/g, '[').replace(/\]/g, ']').replace(/,/g, ',');
2
+ const toBase64 = (file) => Buffer.isBuffer(file) ? `base64://${file.toString('base64')}` : file;
3
+ /**
4
+ * 消息段,搭配 {@link BotApi.sendMessage} / {@link BotApi.sendGroupMessage} 等发送方法使用。
5
+ *
6
+ * ## 收发支持情况
7
+ * | 类型 | 发送 | 接收 | 说明 |
8
+ * |------|:----:|:----:|------|
9
+ * | `text` | ✅ | ✅ | 文本 |
10
+ * | `at` | ✅ | ✅ | @某人 / @全体 |
11
+ * | `reply` | ✅ | ✅ | 引用回复 |
12
+ * | `face` | ✅ | ✅ | QQ 表情 |
13
+ * | `image` | ✅ | ✅ | 图片 |
14
+ * | `file` | ✅ | ✅ | 文件 |
15
+ * | `video` | ✅ | ✅ | 短视频 |
16
+ * | `record` | ✅ | ✅ | 语音 |
17
+ * | `json` | ✅ | ✅ | JSON 消息段 |
18
+ * | `dice` | ✅ | ✅ | 骰子 |
19
+ * | `rps` | ✅ | ✅ | 猜拳 |
20
+ * | `markdown` | ✅ | ✅ | Markdown |
21
+ * | `forward` | ✅ | ✅ | 合并转发 |
22
+ * | `mface` | ✅ | — | 商城表情(收归入 `image`) |
23
+ * | `music` | ✅ | — | 音乐分享 |
24
+ * | `node` | ✅ | — | 合并转发节点 |
25
+ * | `contact` | ✅ | — | 推荐联系人/群 |
26
+ * | `poke` | — | ✅ | 戳一戳(仅接收) |
27
+ */
28
+ export class Message {
29
+ constructor(type, data) {
30
+ Object.defineProperty(this, "type", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "data", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ this.type = type;
43
+ this.data = data;
44
+ }
45
+ /** 文本消息段(发+收) */
46
+ static text(text) {
47
+ return new Message('text', { text });
48
+ }
49
+ /** @某人,传 QQ 号或 `'all'`(发+收) */
50
+ static at(qq) {
51
+ return new Message('at', { qq: String(qq) });
52
+ }
53
+ /** @全体成员(发) */
54
+ static atAll() {
55
+ return new Message('at', { qq: 'all' });
56
+ }
57
+ /** QQ 表情,传表情 ID(发+收) */
58
+ static face(id) {
59
+ return new Message('face', { id: String(id) });
60
+ }
61
+ /** 商城表情(仅发送,接收时归入 image 段) */
62
+ static mface(emoji_id, emoji_package_id, key, summary) {
63
+ return new Message('mface', { emoji_id: String(emoji_id), emoji_package_id: String(emoji_package_id), key, summary: summary ?? '' });
64
+ }
65
+ /** 图片,`file` 支持本地路径 / URL / Buffer(发+收) */
66
+ static image(file, summary, sub_type) {
67
+ return new Message('image', { file: toBase64(file), summary: summary ?? '', sub_type: sub_type != null ? String(sub_type) : '0' });
68
+ }
69
+ /** 引用回复,传消息 ID(发+收) */
70
+ static reply(id) {
71
+ return new Message('reply', { id: String(id) });
72
+ }
73
+ /** 文件,`file` 支持本地路径 / URL / Buffer(发+收) */
74
+ static file(file, name) {
75
+ return new Message('file', { file: toBase64(file), name: name ?? '' });
76
+ }
77
+ /** 短视频,`file` 支持本地路径 / URL / Buffer(发+收) */
78
+ static video(file, name, thumb) {
79
+ return new Message('video', { file: toBase64(file), name: name ?? '', thumb: thumb ?? '' });
80
+ }
81
+ /** 语音,`file` 支持本地路径 / URL / Buffer(发+收) */
82
+ static record(file, name, thumb) {
83
+ return new Message('record', { file: toBase64(file), name: name ?? '', thumb: thumb ?? '' });
84
+ }
85
+ /** JSON 消息段(发+收) */
86
+ static json(data) {
87
+ return new Message('json', { data });
88
+ }
89
+ /** 骰子(发+收) */
90
+ static dice() {
91
+ return new Message('dice', {});
92
+ }
93
+ /** 猜拳(发+收) */
94
+ static rps() {
95
+ return new Message('rps', {});
96
+ }
97
+ /** Markdown 消息(发+收) */
98
+ static markdown(content) {
99
+ return new Message('markdown', { content });
100
+ }
101
+ /** 平台音乐分享,type 支持 qq/163/kugou/migu/kuwo(仅发送) */
102
+ static music(type, id) {
103
+ return new Message('music', { type, id: String(id) });
104
+ }
105
+ /** 自定义音乐分享,需配置签名服务器(仅发送) */
106
+ static customMusic(type, url, image, audio, title, singer) {
107
+ return new Message('music', { type, url, image, audio: audio ?? '', title: title ?? '', singer: singer ?? '' });
108
+ }
109
+ /** 合并转发节点(引用已有消息,仅发送) */
110
+ static node(id, user_id, nickname, source, news, summary, prompt, time) {
111
+ return new Message('node', {
112
+ id: String(id),
113
+ user_id: user_id != null ? String(user_id) : '',
114
+ nickname: nickname ?? '',
115
+ source: source ?? '',
116
+ news: news ?? [],
117
+ summary: summary ?? '',
118
+ prompt: prompt ?? '',
119
+ time: time != null ? String(time) : '',
120
+ });
121
+ }
122
+ /** 合并转发节点(自定义内容,仅发送) */
123
+ static customNode(content, user_id, nickname, source, news, summary, prompt, time) {
124
+ return new Message('node', {
125
+ content: content.map(m => m.toJson()),
126
+ user_id: user_id != null ? String(user_id) : '',
127
+ nickname: nickname ?? '',
128
+ source: source ?? '',
129
+ news: news ?? [],
130
+ summary: summary ?? '',
131
+ prompt: prompt ?? '',
132
+ time: time != null ? String(time) : '',
133
+ });
134
+ }
135
+ /** 合并转发,传 message_id(发+收) */
136
+ static forward(message_id) {
137
+ return new Message('forward', { id: String(message_id) });
138
+ }
139
+ /** 推荐联系人/群(仅发送) */
140
+ static contact(type, id) {
141
+ return new Message('contact', { type, id: String(id) });
142
+ }
143
+ /**
144
+ * 从 napcat 事件中的 message 段构造 Message(收消息还原)
145
+ * 支持的接收类型:text/at/image/file/poke/dice/rps/face/reply/video/record/forward/json/markdown
146
+ * @param seg - `{ type: string, data: object }`
147
+ */
148
+ static from(seg) {
149
+ return new Message(seg.type, { ...seg.data });
150
+ }
151
+ /**
152
+ * 转为 napcat API 所需的 JSON 格式
153
+ * @returns `{ type, data }`
154
+ */
155
+ toJson() {
156
+ const result = { type: this.type, data: {} };
157
+ for (const key of Object.keys(this.data)) {
158
+ const val = this.data[key];
159
+ if (key === 'news' && Array.isArray(val)) {
160
+ result.data[key] = val;
161
+ }
162
+ else if (key === 'content' && Array.isArray(val)) {
163
+ result.data[key] = val;
164
+ }
165
+ else if (val !== '' && val != null) {
166
+ result.data[key] = val;
167
+ }
168
+ }
169
+ return result;
170
+ }
171
+ /**
172
+ * 转为 CQ 码字符串
173
+ * @returns 例如 `[CQ:at,qq=123456]`
174
+ */
175
+ toCQ() {
176
+ if (this.type === 'text') {
177
+ return cqEscape(this.data.text ?? '');
178
+ }
179
+ const parts = [];
180
+ for (const [k, v] of Object.entries(this.data)) {
181
+ if (v == null || v === '' || (Array.isArray(v) && v.length === 0))
182
+ continue;
183
+ parts.push(`${k}=${cqEscape(Array.isArray(v) ? JSON.stringify(v) : String(v))}`);
184
+ }
185
+ return `[CQ:${this.type},${parts.join(',')}]`;
186
+ }
187
+ }
package/logo.png ADDED
Binary file
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "hotcat-bot-qq",
3
+ "version": "0.0.1",
4
+ "description": "基于 node-napcat-ts 的 TypeScript QQ 机器人框架",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./message": {
14
+ "types": "./dist/message.d.ts",
15
+ "default": "./dist/message.js"
16
+ },
17
+ "./botApi": {
18
+ "types": "./dist/botApi.d.ts",
19
+ "default": "./dist/botApi.js"
20
+ },
21
+ "./botClient": {
22
+ "types": "./dist/botClient.d.ts",
23
+ "default": "./dist/botClient.js"
24
+ },
25
+ "./botConsole": {
26
+ "types": "./dist/botConsole.d.ts",
27
+ "default": "./dist/botConsole.js"
28
+ },
29
+ "./botEvent": {
30
+ "types": "./dist/botEvent.d.ts",
31
+ "default": "./dist/botEvent.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "LICENSE",
37
+ "README.md",
38
+ "logo.png"
39
+ ],
40
+ "scripts": {
41
+ "build": "tsc -p tsconfig.build.json",
42
+ "test": "echo \"Error: no test specified\" && exit 1",
43
+ "prepublishOnly": "bun run build",
44
+ "docs:dev": "vitepress dev docs",
45
+ "docs:build": "vitepress build docs",
46
+ "docs:preview": "vitepress preview docs"
47
+ },
48
+ "keywords": [
49
+ "qq",
50
+ "bot",
51
+ "napcat",
52
+ "onebot",
53
+ "typescript",
54
+ "chatbot"
55
+ ],
56
+ "author": "CHCAT1320",
57
+ "license": "MIT",
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "https://github.com/CHCAT1320/hotCat-bot-qq"
61
+ },
62
+ "dependencies": {
63
+ "node-napcat-ts": "^0.4.25",
64
+ "@types/node": "^26.1.2"
65
+ },
66
+ "devDependencies": {
67
+ "@types/node-schedule": "^2.1.8",
68
+ "dependency-cruiser": "^17.4.3",
69
+ "madge": "^8.0.0",
70
+ "patch-package": "^8.0.1",
71
+ "vitepress": "^1.6.4"
72
+ }
73
+ }