napcat-sdk 0.0.0-snapshot.0 → 0.1.0
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/.node-version +1 -0
- package/dist/index.cjs +653 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1054 -0
- package/dist/index.d.mts +1054 -0
- package/dist/index.mjs +612 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -8
- package/src/index.ts +4 -0
- package/src/logger.ts +27 -0
- package/src/napcat.ts +497 -0
- package/src/onebot.ts +1381 -0
- package/src/segment.ts +59 -0
- package/src/types.ts +34 -0
- package/tsconfig.json +1 -26
- package/tsdown.config.ts +12 -0
- package/bun.lock +0 -20
- package/index.js +0 -1
- package/index.ts +0 -195
- package/readme.md +0 -2
package/.node-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
24.11.1
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
let node_crypto = require("node:crypto");
|
|
29
|
+
node_crypto = __toESM(node_crypto);
|
|
30
|
+
let mitt = require("mitt");
|
|
31
|
+
mitt = __toESM(mitt);
|
|
32
|
+
|
|
33
|
+
//#region package.json
|
|
34
|
+
var name$1 = "napcat-sdk";
|
|
35
|
+
var version$1 = "0.1.0";
|
|
36
|
+
var package_default = {
|
|
37
|
+
name: name$1,
|
|
38
|
+
type: "module",
|
|
39
|
+
version: version$1,
|
|
40
|
+
packageManager: "pnpm@10.26.0",
|
|
41
|
+
description: "A simple SDK for NapCat OneBot v11.",
|
|
42
|
+
keywords: [
|
|
43
|
+
"napcat",
|
|
44
|
+
"onebot",
|
|
45
|
+
"onebot v11",
|
|
46
|
+
"sdk"
|
|
47
|
+
],
|
|
48
|
+
repository: {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/vikiboss/mioki.git",
|
|
51
|
+
"directory": "packages/napcat-sdk"
|
|
52
|
+
},
|
|
53
|
+
scripts: {
|
|
54
|
+
"dev": "tsdown -w",
|
|
55
|
+
"build": "tsdown"
|
|
56
|
+
},
|
|
57
|
+
exports: {
|
|
58
|
+
".": {
|
|
59
|
+
"require": "./dist/index.cjs",
|
|
60
|
+
"import": "./dist/index.mjs",
|
|
61
|
+
"types": "./dist/index.d.ts"
|
|
62
|
+
},
|
|
63
|
+
"./package.json": "./package.json"
|
|
64
|
+
},
|
|
65
|
+
author: "Viki <hi@viki.moe> (https://github.com/vikiboss)",
|
|
66
|
+
license: "MIT",
|
|
67
|
+
devDependencies: {
|
|
68
|
+
"@types/node": "catalog:dev",
|
|
69
|
+
"tsdown": "catalog:dev",
|
|
70
|
+
"typescript": "catalog:dev"
|
|
71
|
+
},
|
|
72
|
+
dependencies: { "mitt": "^3.0.1" }
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/segment.ts
|
|
77
|
+
function createSegment(type$1, data) {
|
|
78
|
+
return {
|
|
79
|
+
type: type$1,
|
|
80
|
+
...data
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 消息片段构造器
|
|
85
|
+
*/
|
|
86
|
+
const segment = {
|
|
87
|
+
text: (text) => createSegment("text", { text }),
|
|
88
|
+
at: (qq) => createSegment("at", { qq }),
|
|
89
|
+
face: (id) => createSegment("face", { id }),
|
|
90
|
+
reply: (id) => createSegment("reply", { id }),
|
|
91
|
+
image: (file, options) => createSegment("image", {
|
|
92
|
+
file,
|
|
93
|
+
...options
|
|
94
|
+
}),
|
|
95
|
+
record: (file, options) => createSegment("record", {
|
|
96
|
+
file,
|
|
97
|
+
...options
|
|
98
|
+
}),
|
|
99
|
+
video: (file, options) => createSegment("video", {
|
|
100
|
+
file,
|
|
101
|
+
...options
|
|
102
|
+
}),
|
|
103
|
+
mface: (options) => createSegment("mface", { ...options }),
|
|
104
|
+
bface: (id) => createSegment("bface", { id }),
|
|
105
|
+
contact: (type$1, id) => createSegment("contact", {
|
|
106
|
+
id,
|
|
107
|
+
sub_type: type$1
|
|
108
|
+
}),
|
|
109
|
+
poke: () => createSegment("poke", {}),
|
|
110
|
+
music: (platform, id) => createSegment("music", {
|
|
111
|
+
platform,
|
|
112
|
+
id
|
|
113
|
+
}),
|
|
114
|
+
musicCustom: (title, audio, url, options) => createSegment("music", {
|
|
115
|
+
platform: "custom",
|
|
116
|
+
url,
|
|
117
|
+
audio,
|
|
118
|
+
title,
|
|
119
|
+
...options
|
|
120
|
+
}),
|
|
121
|
+
forward: (id) => createSegment("forward", { id }),
|
|
122
|
+
json: (data) => createSegment("json", { data }),
|
|
123
|
+
file: (file, options) => createSegment("file", {
|
|
124
|
+
file,
|
|
125
|
+
...options
|
|
126
|
+
}),
|
|
127
|
+
markdown: () => createSegment("markdown", {}),
|
|
128
|
+
lightapp: () => createSegment("lightapp", {})
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/logger.ts
|
|
133
|
+
const LOG_LEVEL_KEYS = Object.keys({
|
|
134
|
+
fatal: 0,
|
|
135
|
+
error: 1,
|
|
136
|
+
warn: 2,
|
|
137
|
+
info: 3,
|
|
138
|
+
debug: 4,
|
|
139
|
+
trace: 5
|
|
140
|
+
});
|
|
141
|
+
const noop = () => {};
|
|
142
|
+
const ABSTRACT_LOGGER = Object.fromEntries(LOG_LEVEL_KEYS.map((level) => [level, noop]));
|
|
143
|
+
const CONSOLE_LOGGER = Object.fromEntries(LOG_LEVEL_KEYS.map((level) => [level, (...args) => {
|
|
144
|
+
console[level === "fatal" ? "error" : level === "trace" ? "debug" : level](...args);
|
|
145
|
+
}]));
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/onebot.ts
|
|
149
|
+
/**
|
|
150
|
+
* NapCat 通知类型映射表(notify 类型)
|
|
151
|
+
* @description 将 NapCat 特有的通知类型映射到标准的 notice_type 和 sub_type
|
|
152
|
+
*/
|
|
153
|
+
const NAPCAT_NOTICE_NOTIFY_MAP = {
|
|
154
|
+
input_status: {
|
|
155
|
+
notice_type: "friend",
|
|
156
|
+
sub_type: "input"
|
|
157
|
+
},
|
|
158
|
+
profile_like: {
|
|
159
|
+
notice_type: "friend",
|
|
160
|
+
sub_type: "like"
|
|
161
|
+
},
|
|
162
|
+
title: {
|
|
163
|
+
notice_type: "group",
|
|
164
|
+
sub_type: "title"
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* NapCat 通知事件映射表(notice 类型)
|
|
169
|
+
* @description 将 NapCat 的原始通知事件类型映射到标准的 notice_type 和 sub_type
|
|
170
|
+
*/
|
|
171
|
+
const NAPCAT_NOTICE_EVENT_MAP = {
|
|
172
|
+
friend_add: {
|
|
173
|
+
notice_type: "friend",
|
|
174
|
+
sub_type: "increase"
|
|
175
|
+
},
|
|
176
|
+
friend_recall: {
|
|
177
|
+
notice_type: "friend",
|
|
178
|
+
sub_type: "recall"
|
|
179
|
+
},
|
|
180
|
+
offline_file: {
|
|
181
|
+
notice_type: "friend",
|
|
182
|
+
sub_type: "offline_file"
|
|
183
|
+
},
|
|
184
|
+
client_status: {
|
|
185
|
+
notice_type: "client",
|
|
186
|
+
sub_type: "status"
|
|
187
|
+
},
|
|
188
|
+
group_admin: {
|
|
189
|
+
notice_type: "group",
|
|
190
|
+
sub_type: "admin"
|
|
191
|
+
},
|
|
192
|
+
group_ban: {
|
|
193
|
+
notice_type: "group",
|
|
194
|
+
sub_type: "ban"
|
|
195
|
+
},
|
|
196
|
+
group_card: {
|
|
197
|
+
notice_type: "group",
|
|
198
|
+
sub_type: "card"
|
|
199
|
+
},
|
|
200
|
+
group_upload: {
|
|
201
|
+
notice_type: "group",
|
|
202
|
+
sub_type: "upload"
|
|
203
|
+
},
|
|
204
|
+
group_decrease: {
|
|
205
|
+
notice_type: "group",
|
|
206
|
+
sub_type: "decrease"
|
|
207
|
+
},
|
|
208
|
+
group_increase: {
|
|
209
|
+
notice_type: "group",
|
|
210
|
+
sub_type: "increase"
|
|
211
|
+
},
|
|
212
|
+
group_msg_emoji_like: {
|
|
213
|
+
notice_type: "group",
|
|
214
|
+
sub_type: "reaction"
|
|
215
|
+
},
|
|
216
|
+
essence: {
|
|
217
|
+
notice_type: "group",
|
|
218
|
+
sub_type: "essence"
|
|
219
|
+
},
|
|
220
|
+
group_recall: {
|
|
221
|
+
notice_type: "group",
|
|
222
|
+
sub_type: "recall"
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/napcat.ts
|
|
228
|
+
const name = name$1;
|
|
229
|
+
const version = version$1;
|
|
230
|
+
const DEFAULT_NAPCAT_OPTIONS = {
|
|
231
|
+
protocol: "ws",
|
|
232
|
+
host: "localhost",
|
|
233
|
+
port: 3333,
|
|
234
|
+
logger: ABSTRACT_LOGGER
|
|
235
|
+
};
|
|
236
|
+
var NapCat = class {
|
|
237
|
+
/** WebSocket 实例 */
|
|
238
|
+
#ws = null;
|
|
239
|
+
/** 事件发射器 */
|
|
240
|
+
#event = (0, mitt.default)();
|
|
241
|
+
/** Echo 事件发射器 */
|
|
242
|
+
#echoEvent = (0, mitt.default)();
|
|
243
|
+
constructor(options) {
|
|
244
|
+
this.options = options;
|
|
245
|
+
}
|
|
246
|
+
/** 配置项 */
|
|
247
|
+
get #config() {
|
|
248
|
+
return {
|
|
249
|
+
protocol: this.options.protocol || DEFAULT_NAPCAT_OPTIONS.protocol,
|
|
250
|
+
host: this.options.host || DEFAULT_NAPCAT_OPTIONS.host,
|
|
251
|
+
port: this.options.port || DEFAULT_NAPCAT_OPTIONS.port,
|
|
252
|
+
logger: this.options.logger || DEFAULT_NAPCAT_OPTIONS.logger,
|
|
253
|
+
token: this.options.token
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** WebSocket 实例 */
|
|
257
|
+
get ws() {
|
|
258
|
+
if (!this.#ws) {
|
|
259
|
+
this.logger.error("WebSocket is not connected.");
|
|
260
|
+
throw new Error("WebSocket is not connected.");
|
|
261
|
+
}
|
|
262
|
+
return this.#ws;
|
|
263
|
+
}
|
|
264
|
+
/** 日志记录器 */
|
|
265
|
+
get logger() {
|
|
266
|
+
return this.#config.logger;
|
|
267
|
+
}
|
|
268
|
+
/** 消息段构建器 */
|
|
269
|
+
get segment() {
|
|
270
|
+
return segment;
|
|
271
|
+
}
|
|
272
|
+
/** 生成唯一的 echo ID */
|
|
273
|
+
#echoId() {
|
|
274
|
+
return node_crypto.default.randomBytes(16).toString("hex");
|
|
275
|
+
}
|
|
276
|
+
/** 构建 WebSocket 连接地址 */
|
|
277
|
+
#buildWsUrl() {
|
|
278
|
+
return `${this.#config.protocol}://${this.#config.host}:${this.#config.port}?access_token=${this.#config.token}`;
|
|
279
|
+
}
|
|
280
|
+
/** 包装回复消息 */
|
|
281
|
+
#wrapReply(sendable, message_id, reply) {
|
|
282
|
+
const sendableList = typeof sendable === "string" ? [sendable] : [sendable].flat();
|
|
283
|
+
if (reply && message_id) return [segment.reply(String(message_id)), ...sendableList];
|
|
284
|
+
return sendableList;
|
|
285
|
+
}
|
|
286
|
+
/** 确保 WebSocket 已连接 */
|
|
287
|
+
#ensureWsConnection(ws) {
|
|
288
|
+
if (!ws) {
|
|
289
|
+
this.logger.error("WebSocket is not connected.");
|
|
290
|
+
throw new Error("WebSocket is not connected.");
|
|
291
|
+
}
|
|
292
|
+
if (ws.readyState !== WebSocket.OPEN) {
|
|
293
|
+
this.logger.error("WebSocket is not open.");
|
|
294
|
+
throw new Error("WebSocket is not open.");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/** 标准化可发送消息元素 */
|
|
298
|
+
#normalizeSendable(msg) {
|
|
299
|
+
return [msg].flat(2).map((item) => {
|
|
300
|
+
if (typeof item === "string") return {
|
|
301
|
+
type: "text",
|
|
302
|
+
data: { text: item }
|
|
303
|
+
};
|
|
304
|
+
if (item.type === "at") return {
|
|
305
|
+
type: "at",
|
|
306
|
+
data: { qq: String(item.qq) }
|
|
307
|
+
};
|
|
308
|
+
const { type: type$1, ...data } = item;
|
|
309
|
+
return {
|
|
310
|
+
type: type$1,
|
|
311
|
+
data
|
|
312
|
+
};
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
/** 等待服务器响应操作 */
|
|
316
|
+
#waitForAction(echoId) {
|
|
317
|
+
const eventName = `echo#${echoId}`;
|
|
318
|
+
return new Promise((resolve, reject) => {
|
|
319
|
+
const handle = (data) => {
|
|
320
|
+
if (!data || data.echo !== echoId) return;
|
|
321
|
+
this.#echoEvent.off(eventName, handle);
|
|
322
|
+
if (data.retcode === 0) resolve(data.data);
|
|
323
|
+
else reject(`Server Error: ${data.message}`);
|
|
324
|
+
};
|
|
325
|
+
this.#echoEvent.on(eventName, handle);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/** 构建群对象 */
|
|
329
|
+
#buildGroup(group_id, group_name = "", extraInfo = {}) {
|
|
330
|
+
return {
|
|
331
|
+
...extraInfo,
|
|
332
|
+
group_id,
|
|
333
|
+
group_name,
|
|
334
|
+
doSign: () => this.api("set_group_sign", { group_id }),
|
|
335
|
+
getInfo: () => this.api("get_group_info", { group_id }),
|
|
336
|
+
getMemberList: async () => this.api("get_group_member_list", { group_id }),
|
|
337
|
+
getMemberInfo: (user_id) => this.api("get_group_member_info", {
|
|
338
|
+
group_id,
|
|
339
|
+
user_id
|
|
340
|
+
}),
|
|
341
|
+
setTitle: (title) => this.api("set_group_special_title", {
|
|
342
|
+
group_id,
|
|
343
|
+
title
|
|
344
|
+
}),
|
|
345
|
+
setCard: (user_id, card) => this.api("set_group_card", {
|
|
346
|
+
group_id,
|
|
347
|
+
user_id,
|
|
348
|
+
card
|
|
349
|
+
}),
|
|
350
|
+
addEssence: (message_id) => this.api("set_essence_msg", { message_id }),
|
|
351
|
+
delEssence: (message_id) => this.api("delete_essence_msg", { message_id }),
|
|
352
|
+
recall: (message_id) => this.api("delete_msg", { message_id }),
|
|
353
|
+
banMember: (user_id, duration) => this.api("set_group_ban", {
|
|
354
|
+
group_id,
|
|
355
|
+
user_id,
|
|
356
|
+
duration
|
|
357
|
+
}),
|
|
358
|
+
sendMsg: (sendable) => this.sendGroupMsg(group_id, sendable)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
/** 构建好友对象 */
|
|
362
|
+
#buildFriend(user_id, nickname = "", extraInfo = {}) {
|
|
363
|
+
return {
|
|
364
|
+
...extraInfo,
|
|
365
|
+
user_id,
|
|
366
|
+
nickname,
|
|
367
|
+
delete: (block, both) => this.api("delete_friend", {
|
|
368
|
+
user_id,
|
|
369
|
+
temp_block: block,
|
|
370
|
+
temp_both_del: both
|
|
371
|
+
}),
|
|
372
|
+
sendMsg: (sendable) => this.sendPrivateMsg(user_id, sendable),
|
|
373
|
+
getInfo: () => this.api("get_stranger_info", { user_id })
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/** 构建群消息事件 */
|
|
377
|
+
#buildPrivateMessageEvent(event) {
|
|
378
|
+
return {
|
|
379
|
+
...event,
|
|
380
|
+
message: (event.message || []).map((el) => ({
|
|
381
|
+
type: el.type,
|
|
382
|
+
...el.data
|
|
383
|
+
})),
|
|
384
|
+
friend: this.#buildFriend(event.user_id, event.sender?.nickname || ""),
|
|
385
|
+
recall: () => this.api("delete_msg", { message_id: event.message_id }),
|
|
386
|
+
reply: (sendable, reply = false) => this.sendPrivateMsg(event.user_id, this.#wrapReply(sendable, event.message_id, reply))
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
/** 构建群消息事件对象 */
|
|
390
|
+
#buildGroupMessageEvent(event) {
|
|
391
|
+
return {
|
|
392
|
+
...event,
|
|
393
|
+
message: (event.message || []).map((el) => ({
|
|
394
|
+
type: el.type,
|
|
395
|
+
...el.data
|
|
396
|
+
})),
|
|
397
|
+
group: this.#buildGroup(event.group_id, event.group?.group_name || ""),
|
|
398
|
+
recall: () => this.api("delete_msg", { message_id: event.message_id }),
|
|
399
|
+
addReaction: (id) => this.api("set_msg_emoji_like", {
|
|
400
|
+
message_id: event.message_id,
|
|
401
|
+
emoji_id: id,
|
|
402
|
+
set: true
|
|
403
|
+
}),
|
|
404
|
+
delReaction: (id) => this.api("set_msg_emoji_like", {
|
|
405
|
+
message_id: event.message_id,
|
|
406
|
+
emoji_id: id,
|
|
407
|
+
set: false
|
|
408
|
+
}),
|
|
409
|
+
addEssence: () => this.api("set_essence_msg", { message_id: event.message_id }),
|
|
410
|
+
delEssence: () => this.api("delete_essence_msg", { message_id: event.message_id }),
|
|
411
|
+
reply: (sendable, reply = false) => this.sendGroupMsg(event.group_id, this.#wrapReply(sendable, event.message_id, reply))
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
/** 绑定内部事件处理器 */
|
|
415
|
+
#bindInternalEvents(data) {
|
|
416
|
+
if (data.echo) {
|
|
417
|
+
this.#echoEvent.emit(`echo#${data.echo}`, data);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (data.post_type) {
|
|
421
|
+
switch (data.post_type) {
|
|
422
|
+
case "meta_event":
|
|
423
|
+
this.logger.trace(`received meta_event: ${JSON.stringify(data)}`);
|
|
424
|
+
this.#event.emit("meta_event", data);
|
|
425
|
+
if (data.meta_event_type) {
|
|
426
|
+
this.#event.emit(`meta_event.${data.meta_event_type}`, data);
|
|
427
|
+
if (data.sub_type) this.#event.emit(`meta_event.${data.meta_event_type}.${data.sub_type}`, data);
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
case "message":
|
|
431
|
+
if (data.message_type === "private") data = this.#buildPrivateMessageEvent(data);
|
|
432
|
+
else data = this.#buildGroupMessageEvent(data);
|
|
433
|
+
this.#event.emit("message", data);
|
|
434
|
+
switch (data.message_type) {
|
|
435
|
+
case "private":
|
|
436
|
+
this.logger.trace(`received private message: ${JSON.stringify(data)}`);
|
|
437
|
+
this.#event.emit("message.private", data);
|
|
438
|
+
this.#event.emit(`message.private.${data.sub_type}`, data);
|
|
439
|
+
break;
|
|
440
|
+
case "group":
|
|
441
|
+
this.logger.trace(`received group message: ${JSON.stringify(data)}`);
|
|
442
|
+
this.#event.emit("message.group", data);
|
|
443
|
+
this.#event.emit(`message.group.${data.sub_type}`, data);
|
|
444
|
+
break;
|
|
445
|
+
default:
|
|
446
|
+
this.logger.debug(`received unknown message type: ${JSON.stringify(data)}`);
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
case "message_sent":
|
|
451
|
+
this.logger.trace(`received message_sent: ${JSON.stringify(data)}`);
|
|
452
|
+
this.#event.emit("message_sent", data);
|
|
453
|
+
if (data.message_type) {
|
|
454
|
+
this.#event.emit(`message_sent.${data.message_type}`, data);
|
|
455
|
+
if (data.sub_type) this.#event.emit(`message_sent.${data.message_type}.${data.sub_type}`, data);
|
|
456
|
+
}
|
|
457
|
+
break;
|
|
458
|
+
case "notice": {
|
|
459
|
+
this.logger.trace(`received notice: ${JSON.stringify(data)}`);
|
|
460
|
+
if (!data.notice_type) {
|
|
461
|
+
this.logger.debug(`received unknown notice type: ${JSON.stringify(data)}`);
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
const isNotify = data.notice_type === "notify";
|
|
465
|
+
const isPoke = data.sub_type === "poke";
|
|
466
|
+
const isGroup = !!data.group_id;
|
|
467
|
+
const { notice_type, sub_type } = isNotify ? isPoke ? {
|
|
468
|
+
notice_type: isGroup ? "group" : "friend",
|
|
469
|
+
sub_type: "poke"
|
|
470
|
+
} : NAPCAT_NOTICE_NOTIFY_MAP[data.sub_type] || {} : NAPCAT_NOTICE_EVENT_MAP[data.notice_type] || {};
|
|
471
|
+
data.original_notice_type = data.notice_type;
|
|
472
|
+
data.notice_type = notice_type || data.notice_type;
|
|
473
|
+
if (data.sub_type && data.sub_type !== sub_type) data.action_type = data.sub_type;
|
|
474
|
+
data.sub_type = sub_type || data.sub_type;
|
|
475
|
+
if (isGroup) data.group = this.#buildGroup(data.group_id, data.group_name || "");
|
|
476
|
+
else data.friend = this.#buildFriend(data.user_id, data.nickname || "");
|
|
477
|
+
this.#event.emit("notice", data);
|
|
478
|
+
if (notice_type) {
|
|
479
|
+
this.#event.emit(`notice.${notice_type}`, data);
|
|
480
|
+
if (sub_type) this.#event.emit(`notice.${notice_type}.${sub_type}`, data);
|
|
481
|
+
}
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
case "request":
|
|
485
|
+
this.logger.trace(`received request: ${JSON.stringify(data)}`);
|
|
486
|
+
if (data.request_type === "friend") {
|
|
487
|
+
data.reject = () => this.api("set_friend_request", {
|
|
488
|
+
flag: data.flag,
|
|
489
|
+
approve: false
|
|
490
|
+
});
|
|
491
|
+
data.approve = () => this.api("set_friend_request", {
|
|
492
|
+
flag: data.flag,
|
|
493
|
+
approve: true
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
if (data.request_type === "group") {
|
|
497
|
+
data.reject = (reason) => this.api("set_group_add_request", {
|
|
498
|
+
flag: data.flag,
|
|
499
|
+
approve: false,
|
|
500
|
+
reason
|
|
501
|
+
});
|
|
502
|
+
data.approve = () => this.api("set_group_add_request", {
|
|
503
|
+
flag: data.flag,
|
|
504
|
+
approve: true
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
this.#event.emit("request", data);
|
|
508
|
+
if (data.request_type) {
|
|
509
|
+
this.#event.emit(`request.${data.request_type}`, data);
|
|
510
|
+
if (data.sub_type) this.#event.emit(`request.${data.request_type}.${data.sub_type}`, data);
|
|
511
|
+
}
|
|
512
|
+
break;
|
|
513
|
+
default:
|
|
514
|
+
this.logger.debug(`received: ${JSON.stringify(data)}`);
|
|
515
|
+
this.#event.emit(data.post_type, data);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** 获取一个群的信息,可以用于发送群消息等操作 */
|
|
522
|
+
async pickGroup(group_id) {
|
|
523
|
+
const groupInfo = await this.api("get_group_info", { group_id });
|
|
524
|
+
return this.#buildGroup(group_id, groupInfo.group_name, groupInfo);
|
|
525
|
+
}
|
|
526
|
+
/** 获取一个好友的信息,可以用于发送私聊消息等操作 */
|
|
527
|
+
async pickFriend(user_id) {
|
|
528
|
+
const friendInfo = await this.api("get_stranger_info", { user_id });
|
|
529
|
+
return this.#buildFriend(user_id, friendInfo.nickname, friendInfo);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* 注册一次性事件监听器
|
|
533
|
+
*/
|
|
534
|
+
once(type$1, handler) {
|
|
535
|
+
const onceHandler = (event) => {
|
|
536
|
+
handler(event);
|
|
537
|
+
this.#event.off(type$1, onceHandler);
|
|
538
|
+
};
|
|
539
|
+
this.logger.debug(`registering once: ${String(type$1)}`);
|
|
540
|
+
this.#event.on(type$1, onceHandler);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* 注册事件监听器,支持主类型或者点分子类型
|
|
544
|
+
*
|
|
545
|
+
* 如: `notice`、`message.private`、`request.group.invite` 等
|
|
546
|
+
*
|
|
547
|
+
* 如果需要移除监听器,请调用 `off` 方法
|
|
548
|
+
*/
|
|
549
|
+
on(type$1, handler) {
|
|
550
|
+
this.logger.debug(`registering: ${String(type$1)}`);
|
|
551
|
+
this.#event.on(type$1, handler);
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* 移除事件监听器
|
|
555
|
+
*/
|
|
556
|
+
off(type$1, handler) {
|
|
557
|
+
this.logger.debug(`unregistering: ${String(type$1)}`);
|
|
558
|
+
this.#event.off(type$1, handler);
|
|
559
|
+
}
|
|
560
|
+
api(action, params = {}) {
|
|
561
|
+
this.#ensureWsConnection(this.#ws);
|
|
562
|
+
this.logger.debug(`calling api action: ${action} with params: ${JSON.stringify(params)}`);
|
|
563
|
+
const echo = this.#echoId();
|
|
564
|
+
this.#ws.send(JSON.stringify({
|
|
565
|
+
echo,
|
|
566
|
+
action,
|
|
567
|
+
params
|
|
568
|
+
}));
|
|
569
|
+
return this.#waitForAction(echo);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* 发送私聊消息
|
|
573
|
+
*/
|
|
574
|
+
sendPrivateMsg(user_id, sendable) {
|
|
575
|
+
return this.api("send_private_msg", {
|
|
576
|
+
user_id,
|
|
577
|
+
message: this.#normalizeSendable(sendable)
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* 发送群消息
|
|
582
|
+
*/
|
|
583
|
+
sendGroupMsg(group_id, sendable) {
|
|
584
|
+
return this.api("send_group_msg", {
|
|
585
|
+
group_id,
|
|
586
|
+
message: this.#normalizeSendable(sendable)
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
/** 启动 NapCat SDK 实例,建立 WebSocket 连接 */
|
|
590
|
+
async bootstrap() {
|
|
591
|
+
const { logger: _, ...config } = this.#config;
|
|
592
|
+
this.logger.info(`bootstrap with config: ${JSON.stringify(config)}`);
|
|
593
|
+
return new Promise((resolve, reject) => {
|
|
594
|
+
const ws = new WebSocket(this.#buildWsUrl());
|
|
595
|
+
ws.onmessage = (event) => {
|
|
596
|
+
const data = (() => {
|
|
597
|
+
try {
|
|
598
|
+
return JSON.parse(event.data);
|
|
599
|
+
} catch {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
})();
|
|
603
|
+
if (!data) {
|
|
604
|
+
this.logger.warn(`received non-json message: ${event.data}`);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
this.#event.emit("ws.message", data);
|
|
608
|
+
this.#bindInternalEvents(data);
|
|
609
|
+
};
|
|
610
|
+
ws.onclose = () => {
|
|
611
|
+
this.logger.info("closed");
|
|
612
|
+
this.#event.emit("ws.close");
|
|
613
|
+
};
|
|
614
|
+
ws.onerror = (error) => {
|
|
615
|
+
this.logger.error(`error: ${error}`);
|
|
616
|
+
this.#event.emit("ws.error", error);
|
|
617
|
+
reject(error);
|
|
618
|
+
};
|
|
619
|
+
ws.onopen = () => {
|
|
620
|
+
this.logger.info("connected");
|
|
621
|
+
this.#event.emit("ws.open");
|
|
622
|
+
resolve();
|
|
623
|
+
};
|
|
624
|
+
this.#ws = ws;
|
|
625
|
+
this.logger.trace(`WebSocket instance created: ${this.#ws}`);
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
/** 销毁 NapCat SDK 实例,关闭 WebSocket 连接 */
|
|
629
|
+
async destroy() {
|
|
630
|
+
if (this.#ws) {
|
|
631
|
+
this.logger.info("destroying NapCat SDK instance...");
|
|
632
|
+
this.#ws.close();
|
|
633
|
+
this.#ws = null;
|
|
634
|
+
this.logger.info("NapCat SDK instance destroyed.");
|
|
635
|
+
} else this.logger.warn("NapCat SDK instance is not initialized.");
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
//#endregion
|
|
640
|
+
exports.ABSTRACT_LOGGER = ABSTRACT_LOGGER;
|
|
641
|
+
exports.CONSOLE_LOGGER = CONSOLE_LOGGER;
|
|
642
|
+
exports.DEFAULT_NAPCAT_OPTIONS = DEFAULT_NAPCAT_OPTIONS;
|
|
643
|
+
exports.NapCat = NapCat;
|
|
644
|
+
Object.defineProperty(exports, 'PKG', {
|
|
645
|
+
enumerable: true,
|
|
646
|
+
get: function () {
|
|
647
|
+
return package_default;
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
exports.name = name;
|
|
651
|
+
exports.segment = segment;
|
|
652
|
+
exports.version = version;
|
|
653
|
+
//# sourceMappingURL=index.cjs.map
|