@rdjksp/node-napcat-ts 0.4.20
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/LICENSE +21 -0
- package/README.md +22 -0
- package/dist/Interfaces.d.ts +2309 -0
- package/dist/Interfaces.js +1 -0
- package/dist/NCEventBus.d.ts +32 -0
- package/dist/NCEventBus.js +336 -0
- package/dist/NCWebsocketApi.d.ts +1793 -0
- package/dist/NCWebsocketApi.js +1011 -0
- package/dist/NCWebsocketBase.d.ts +58 -0
- package/dist/NCWebsocketBase.js +276 -0
- package/dist/Structs.d.ts +379 -0
- package/dist/Structs.js +230 -0
- package/dist/Utils.d.ts +18 -0
- package/dist/Utils.js +65 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/package.json +74 -0
package/dist/Structs.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
export const Structs = {
|
|
2
|
+
/**
|
|
3
|
+
* 发送文字消息
|
|
4
|
+
* @param text 要发送的文字
|
|
5
|
+
* @returns { type: 'text', data: { text } }
|
|
6
|
+
*/
|
|
7
|
+
text: function (text) {
|
|
8
|
+
return { type: 'text', data: { text } };
|
|
9
|
+
},
|
|
10
|
+
/**
|
|
11
|
+
* @某人
|
|
12
|
+
* @param qq at的QQ号
|
|
13
|
+
* @returns { type: 'at', data: { qq } }
|
|
14
|
+
*/
|
|
15
|
+
at: function (qq) {
|
|
16
|
+
return { type: 'at', data: { qq: qq.toString() } };
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* 回复消息
|
|
20
|
+
* @param id 回复的消息id
|
|
21
|
+
* @returns { type: 'reply', data: { id } }
|
|
22
|
+
*/
|
|
23
|
+
reply: function (id) {
|
|
24
|
+
return { type: 'reply', data: { id: id.toString() } };
|
|
25
|
+
},
|
|
26
|
+
/**
|
|
27
|
+
* 发送QQ表情
|
|
28
|
+
* @param id QQ 表情 ID
|
|
29
|
+
* @returns { type: 'face', data: { id, resultId, chainCount } }
|
|
30
|
+
*/
|
|
31
|
+
face: function (id) {
|
|
32
|
+
return { type: 'face', data: { id: id.toString() } };
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* 发送QQ表情包
|
|
36
|
+
* @param emoji_id 表情id
|
|
37
|
+
* @param emoji_package_id 表情包id
|
|
38
|
+
* @param key 未知(必要)
|
|
39
|
+
* @param summary 表情简介,可选
|
|
40
|
+
* @returns { type: 'mface', data: { summary, emoji_id, emoji_package_id, key } }
|
|
41
|
+
*/
|
|
42
|
+
mface: function (emoji_id, emoji_package_id, key, summary) {
|
|
43
|
+
return {
|
|
44
|
+
type: 'mface',
|
|
45
|
+
data: {
|
|
46
|
+
summary,
|
|
47
|
+
emoji_id: emoji_id.toString(),
|
|
48
|
+
emoji_package_id: emoji_package_id.toString(),
|
|
49
|
+
key,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
/**
|
|
54
|
+
* 发送图片
|
|
55
|
+
* @param file 网络图片地址, 文件路径或者Buffer
|
|
56
|
+
* @param name 图片名
|
|
57
|
+
* @param summary 图片简介
|
|
58
|
+
* @param sub_type 图片类型
|
|
59
|
+
* @returns { type: 'image', data: { file, summary, sub_type } }
|
|
60
|
+
*/
|
|
61
|
+
image: function (file, summary, sub_type) {
|
|
62
|
+
return {
|
|
63
|
+
type: 'image',
|
|
64
|
+
data: {
|
|
65
|
+
file: Buffer.isBuffer(file) ? `base64://${file.toString('base64')}` : file,
|
|
66
|
+
summary,
|
|
67
|
+
sub_type: sub_type?.toString(),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
/**
|
|
72
|
+
* 发文件
|
|
73
|
+
* @param file 网络文件地址, 文件路径或者Buffer
|
|
74
|
+
* @param name 文件名
|
|
75
|
+
* @returns { type: 'file', data: { file, name } }
|
|
76
|
+
*/
|
|
77
|
+
file: function (file, name) {
|
|
78
|
+
return {
|
|
79
|
+
type: 'file',
|
|
80
|
+
data: {
|
|
81
|
+
file: Buffer.isBuffer(file) ? `base64://${file.toString('base64')}` : file,
|
|
82
|
+
name,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
/**
|
|
87
|
+
* 发视频
|
|
88
|
+
* @param file 网络视频地址, 文件路径或者Buffer
|
|
89
|
+
* @param name 视频名
|
|
90
|
+
* @param thumb 预览图
|
|
91
|
+
* @returns { type: 'video', data: { file, name, thumb } }
|
|
92
|
+
*/
|
|
93
|
+
video: function (file, name, thumb) {
|
|
94
|
+
return {
|
|
95
|
+
type: 'video',
|
|
96
|
+
data: {
|
|
97
|
+
file: Buffer.isBuffer(file) ? `base64://${file.toString('base64')}` : file,
|
|
98
|
+
name,
|
|
99
|
+
thumb,
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
/**
|
|
104
|
+
* 发语音
|
|
105
|
+
* @param file 网络语音地址, 文件路径或者Buffer
|
|
106
|
+
* @param name 语音备注
|
|
107
|
+
* @returns { type: 'record', data: { file, name } }
|
|
108
|
+
*/
|
|
109
|
+
record: function (file) {
|
|
110
|
+
return {
|
|
111
|
+
type: 'record',
|
|
112
|
+
data: {
|
|
113
|
+
file: Buffer.isBuffer(file) ? `base64://${file.toString('base64')}` : file,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* 发送json消息
|
|
119
|
+
* @param data json信息(序列化后)
|
|
120
|
+
* @returns { type: 'json', data: { data } }
|
|
121
|
+
*/
|
|
122
|
+
json: function (data) {
|
|
123
|
+
return { type: 'json', data: { data } };
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* 发送骰子魔法表情
|
|
127
|
+
* @returns { type: 'dice', data: {} }
|
|
128
|
+
*/
|
|
129
|
+
dice: function () {
|
|
130
|
+
return { type: 'dice', data: {} };
|
|
131
|
+
},
|
|
132
|
+
/**
|
|
133
|
+
* 发送猜拳魔法
|
|
134
|
+
* @returns { type: 'rps', data: {} }
|
|
135
|
+
*/
|
|
136
|
+
rps: function () {
|
|
137
|
+
return { type: 'rps', data: {} };
|
|
138
|
+
},
|
|
139
|
+
/**
|
|
140
|
+
* 发送markdown
|
|
141
|
+
* @param data markdown内容
|
|
142
|
+
* @returns { type: 'markdown', data: {} }
|
|
143
|
+
*/
|
|
144
|
+
markdown: function (content) {
|
|
145
|
+
return { type: 'markdown', data: { content } };
|
|
146
|
+
},
|
|
147
|
+
/**
|
|
148
|
+
* 音乐分享
|
|
149
|
+
* @param type QQ音乐或网易云音乐QQ音乐
|
|
150
|
+
* @param id 音乐id
|
|
151
|
+
* @returns { type: 'music', data: { type, id } }
|
|
152
|
+
*/
|
|
153
|
+
music: function (type, id) {
|
|
154
|
+
return { type: 'music', data: { type, id: id.toString() } };
|
|
155
|
+
},
|
|
156
|
+
/**
|
|
157
|
+
* 分享非qq、网易云音乐 需要配置签名服务器
|
|
158
|
+
* @param url 点击后跳转目标 URL
|
|
159
|
+
* @param audio 音乐 URL
|
|
160
|
+
* @param title 标题
|
|
161
|
+
* @param image 发送时可选,内容描述
|
|
162
|
+
* @param singer 发送时可选,图片 URL
|
|
163
|
+
* @returns { type: 'music', data: { type: 'custom', url, audio, title, image, singer } }
|
|
164
|
+
*/
|
|
165
|
+
customMusic: function (type, url, image, audio, title, singer) {
|
|
166
|
+
return { type: 'music', data: { type, url, audio, title, image, singer } };
|
|
167
|
+
},
|
|
168
|
+
/**
|
|
169
|
+
* 转发消息节点
|
|
170
|
+
* @param id 消息id
|
|
171
|
+
* @param user_id 消息id
|
|
172
|
+
* @param nickname 消息id
|
|
173
|
+
* @param source 消息id
|
|
174
|
+
* @param id 消息id
|
|
175
|
+
* @param id 消息id
|
|
176
|
+
* @returns { type: 'node', data: { id } }
|
|
177
|
+
*/
|
|
178
|
+
node: function (id, user_id, nickname, source, news, summary, prompt, time) {
|
|
179
|
+
return {
|
|
180
|
+
type: 'node',
|
|
181
|
+
data: {
|
|
182
|
+
id: id.toString(),
|
|
183
|
+
user_id: user_id?.toString(),
|
|
184
|
+
nickname,
|
|
185
|
+
source,
|
|
186
|
+
news,
|
|
187
|
+
summary,
|
|
188
|
+
prompt,
|
|
189
|
+
time: time?.toString(),
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
/**
|
|
194
|
+
* 自定义转发消息节点
|
|
195
|
+
* @param content 消息内容
|
|
196
|
+
* @returns { type: 'node', data: { content } }
|
|
197
|
+
*/
|
|
198
|
+
customNode: function (content, user_id, nickname, source, news, summary, prompt, time) {
|
|
199
|
+
return {
|
|
200
|
+
type: 'node',
|
|
201
|
+
data: {
|
|
202
|
+
content,
|
|
203
|
+
user_id: user_id?.toString(),
|
|
204
|
+
nickname,
|
|
205
|
+
source,
|
|
206
|
+
news,
|
|
207
|
+
summary,
|
|
208
|
+
prompt,
|
|
209
|
+
time: time?.toString(),
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
},
|
|
213
|
+
/**
|
|
214
|
+
* 转发消息
|
|
215
|
+
* @param message_id 消息id
|
|
216
|
+
* @return { type: 'forward', data: { id }}
|
|
217
|
+
*/
|
|
218
|
+
forward: function (message_id) {
|
|
219
|
+
return { type: 'forward', data: { id: message_id.toString() } };
|
|
220
|
+
},
|
|
221
|
+
/**
|
|
222
|
+
* 发送名片
|
|
223
|
+
* @param type 名片类型
|
|
224
|
+
* @param id 联系人QQ号
|
|
225
|
+
* @returns { type: 'contact', data: { id } }
|
|
226
|
+
*/
|
|
227
|
+
contact: function (type, id) {
|
|
228
|
+
return { type: 'contact', data: { type, id: id.toString() } };
|
|
229
|
+
},
|
|
230
|
+
};
|
package/dist/Utils.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Receive, SendMessageSegment, UnSafeStruct } from './Structs.js';
|
|
2
|
+
export declare const logger: {
|
|
3
|
+
warn: (...args: any[]) => void;
|
|
4
|
+
debug: (...args: any[]) => void;
|
|
5
|
+
dir: (json: any) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare const SPLIT: RegExp;
|
|
8
|
+
export declare const CQ_TAG_REGEXP: RegExp;
|
|
9
|
+
/**
|
|
10
|
+
* CQ码转JSON
|
|
11
|
+
*/
|
|
12
|
+
export declare function convertCQCodeToJSON(msg: string): Receive[keyof Receive][] | SendMessageSegment[];
|
|
13
|
+
/**
|
|
14
|
+
* JSON转CQ码
|
|
15
|
+
*/
|
|
16
|
+
export declare function convertJSONToCQCode(json: UnSafeStruct | UnSafeStruct[]): string;
|
|
17
|
+
export declare function CQCodeDecode(str: string | any): string;
|
|
18
|
+
export declare function CQCodeEncode(str: string): string;
|
package/dist/Utils.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const getTime = () => new Date().toLocaleString();
|
|
2
|
+
export const logger = {
|
|
3
|
+
warn: (...args) => {
|
|
4
|
+
console.warn(`[${getTime()}]`, ...args);
|
|
5
|
+
},
|
|
6
|
+
debug: (...args) => {
|
|
7
|
+
console.debug(`[${getTime()}]`, ...args);
|
|
8
|
+
},
|
|
9
|
+
dir: (json) => {
|
|
10
|
+
console.dir(json, { depth: null });
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
export const SPLIT = /(?=\[CQ:)|(?<=])/;
|
|
14
|
+
export const CQ_TAG_REGEXP = /^\[CQ:([a-z]+)(?:,([^\]]+))?]$/;
|
|
15
|
+
/**
|
|
16
|
+
* CQ码转JSON
|
|
17
|
+
*/
|
|
18
|
+
export function convertCQCodeToJSON(msg) {
|
|
19
|
+
return CQCodeDecode(msg)
|
|
20
|
+
.split(SPLIT)
|
|
21
|
+
.map((tagStr) => {
|
|
22
|
+
const match = CQ_TAG_REGEXP.exec(tagStr);
|
|
23
|
+
if (match === null)
|
|
24
|
+
return { type: 'text', data: { text: tagStr } };
|
|
25
|
+
const [, tagName, value] = match;
|
|
26
|
+
if (value === undefined)
|
|
27
|
+
return { type: tagName, data: {} };
|
|
28
|
+
const data = Object.fromEntries(value.split(',').map((item) => item.split('=')));
|
|
29
|
+
return { type: tagName, data };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const _conver = (json) => {
|
|
33
|
+
if (json.type === 'text')
|
|
34
|
+
return json.data.text;
|
|
35
|
+
return `[CQ:${json.type}${Object.entries(json.data)
|
|
36
|
+
.map(([k, v]) => (v ? `,${k}=${v}` : ''))
|
|
37
|
+
.join('')}]`;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* JSON转CQ码
|
|
41
|
+
*/
|
|
42
|
+
export function convertJSONToCQCode(json) {
|
|
43
|
+
if (Array.isArray(json)) {
|
|
44
|
+
return json.map((item) => _conver(item)).join('');
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return _conver(json);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function CQCodeDecode(str) {
|
|
51
|
+
if (typeof str !== 'string')
|
|
52
|
+
return String(str || ''); // 尝试转换为字符串,或返回空字符串
|
|
53
|
+
return str
|
|
54
|
+
.replace(/,/g, ',')
|
|
55
|
+
.replace(/[/g, '[')
|
|
56
|
+
.replace(/]/g, ']')
|
|
57
|
+
.replace(/&/g, '&');
|
|
58
|
+
}
|
|
59
|
+
export function CQCodeEncode(str) {
|
|
60
|
+
return str
|
|
61
|
+
.replace(/,/g, ',')
|
|
62
|
+
.replace(/\[/g, '[')
|
|
63
|
+
.replace(/]/g, ']')
|
|
64
|
+
.replace(/&/g, '&');
|
|
65
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rdjksp/node-napcat-ts",
|
|
3
|
+
"version": "0.4.20",
|
|
4
|
+
"description": "napcat SDK for Node",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"./dist/**/*"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"websocket",
|
|
14
|
+
"qq-robot",
|
|
15
|
+
"cq-websocket",
|
|
16
|
+
"onebot11",
|
|
17
|
+
"typescript",
|
|
18
|
+
"napcat"
|
|
19
|
+
],
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "huankong233",
|
|
22
|
+
"url": "https://github.com/huankong233"
|
|
23
|
+
},
|
|
24
|
+
"contributors": [
|
|
25
|
+
{
|
|
26
|
+
"name": "kanocence",
|
|
27
|
+
"url": "https://github.com/kanocence"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "jacksixth",
|
|
31
|
+
"url": "https://github.com/jacksixth"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "YunYouJun",
|
|
35
|
+
"url": "https://github.com/YunYouJun"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"name": "RongDuJiKsp",
|
|
39
|
+
"url": "https://github.com/RongDuJiKsp"
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/RongDuJiKsp/node-napcat-ts.git"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc",
|
|
48
|
+
"dev": "nodemon",
|
|
49
|
+
"test": "tsx test/index.ts",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"docs:dev": "pnpm -C docs run dev",
|
|
52
|
+
"docs:build": "pnpm -C docs run build",
|
|
53
|
+
"docs:preview": "pnpm -C docs run preview"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"isomorphic-ws": "^5.0.0",
|
|
57
|
+
"nanoid": "^5.1.5",
|
|
58
|
+
"ws": "^8.18.2"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@types/node": "^22.15.19",
|
|
62
|
+
"@types/ws": "^8.18.1",
|
|
63
|
+
"dotenv": "^16.5.0",
|
|
64
|
+
"nodemon": "^3.1.10",
|
|
65
|
+
"tsx": "^4.19.4",
|
|
66
|
+
"typescript": "^5.8.3"
|
|
67
|
+
},
|
|
68
|
+
"pnpm": {
|
|
69
|
+
"onlyBuiltDependencies": [
|
|
70
|
+
"esbuild",
|
|
71
|
+
"vue-demi"
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
}
|