@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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import { EventHandleMap, EventKey, HandlerResMap, type MessageHandler, type MessageSentHandler, type MetaEventHandler, type NoticeHandler, type RequestHandler, type WSReceiveHandler } from './Interfaces.js';
2
+ import type { NCWebsocketBase } from './NCWebsocketBase.js';
3
+ export declare class NCEventBus {
4
+ #private;
5
+ constructor(ws: NCWebsocketBase);
6
+ on<T extends EventKey>(event: T, handler: EventHandleMap[T]): this;
7
+ off<T extends EventKey>(event: T, handler: EventHandleMap[T]): this;
8
+ once<T extends EventKey>(event: T, handler: EventHandleMap[T]): this;
9
+ subscribe<T extends EventKey>(event: T, handler: EventHandleMap[T]): (() => void);
10
+ subscribeOnce<T extends EventKey>(event: T, handler: EventHandleMap[T]): (() => void);
11
+ emit<T extends EventKey>(event: T, context: HandlerResMap[T]): boolean;
12
+ parseMessage(json: WSReceiveHandler[keyof WSReceiveHandler]): boolean;
13
+ meta_event(json: MetaEventHandler[keyof MetaEventHandler]): boolean;
14
+ life_cycle(json: MetaEventHandler['meta_event.lifecycle']): boolean;
15
+ message(json: MessageHandler[keyof MessageHandler]): boolean;
16
+ message_private(json: MessageHandler['message.private']): boolean;
17
+ message_group(json: MessageHandler['message.group']): boolean;
18
+ message_sent(json: MessageSentHandler[keyof MessageSentHandler]): boolean;
19
+ message_sent_private(json: MessageSentHandler['message_sent.private']): boolean;
20
+ message_sent_group(json: MessageSentHandler['message_sent.group']): boolean;
21
+ request(json: RequestHandler[keyof RequestHandler]): boolean;
22
+ request_group(json: RequestHandler['request.group']): boolean;
23
+ notice(json: NoticeHandler[keyof NoticeHandler]): boolean;
24
+ notice_group_admin(json: NoticeHandler['notice.group_admin']): boolean;
25
+ notice_group_ban(json: NoticeHandler['notice.group_ban']): boolean;
26
+ notice_group_decrease(json: NoticeHandler['notice.group_decrease']): boolean;
27
+ notice_group_increase(json: NoticeHandler['notice.group_increase']): boolean;
28
+ notice_essence(json: NoticeHandler['notice.essence']): boolean;
29
+ notice_notify(json: NoticeHandler['notice.notify']): boolean;
30
+ notice_notify_input_status(json: NoticeHandler['notice.notify.input_status']): boolean;
31
+ notice_notify_poke(json: NoticeHandler['notice.notify.poke']): boolean;
32
+ }
@@ -0,0 +1,336 @@
1
+ import { logger } from './Utils.js';
2
+ export class NCEventBus {
3
+ #events = new Map();
4
+ #ws;
5
+ constructor(ws) {
6
+ this.#ws = ws;
7
+ }
8
+ on(event, handler) {
9
+ const handlers = this.#events.get(event) ?? [];
10
+ // @ts-ignore 表达式过于复杂无法表示
11
+ if (handlers.indexOf(handler) >= 0)
12
+ return this;
13
+ handlers.push(handler);
14
+ this.#events.set(event, handlers);
15
+ return this;
16
+ }
17
+ off(event, handler) {
18
+ const handlers = this.#events.get(event) ?? [];
19
+ const index = handlers.indexOf(handler);
20
+ if (index >= 0) {
21
+ handlers.splice(index, 1);
22
+ this.#events.set(event, handlers);
23
+ }
24
+ return this;
25
+ }
26
+ once(event, handler) {
27
+ const onceHandler = (context) => {
28
+ handler(context);
29
+ this.off(event, onceHandler);
30
+ };
31
+ this.on(event, onceHandler);
32
+ return this;
33
+ }
34
+ subscribe(event, handler) {
35
+ this.on(event, handler);
36
+ return () => {
37
+ this.off(event, handler);
38
+ };
39
+ }
40
+ subscribeOnce(event, handler) {
41
+ const onceHandler = (context) => {
42
+ handler(context);
43
+ this.off(event, onceHandler);
44
+ };
45
+ this.on(event, onceHandler);
46
+ return () => {
47
+ this.off(event, onceHandler);
48
+ };
49
+ }
50
+ emit(event, context) {
51
+ const handlers = this.#events.get(event) ?? [];
52
+ for (const handler of handlers)
53
+ handler(context);
54
+ // 触发总类
55
+ const indexOf = event.lastIndexOf('.');
56
+ if (indexOf > 0)
57
+ return this.emit(event.slice(0, indexOf), context);
58
+ return true;
59
+ }
60
+ parseMessage(json) {
61
+ const post_type = json['post_type'];
62
+ switch (post_type) {
63
+ case 'meta_event':
64
+ this.meta_event(json);
65
+ break;
66
+ case 'message':
67
+ this.message(json);
68
+ break;
69
+ case 'message_sent':
70
+ this.message_sent(json);
71
+ break;
72
+ case 'request':
73
+ this.request(json);
74
+ break;
75
+ case 'notice':
76
+ this.notice(json);
77
+ break;
78
+ default:
79
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown post_type: ${post_type}`);
80
+ return false;
81
+ }
82
+ return true;
83
+ }
84
+ meta_event(json) {
85
+ const meta_event_type = json['meta_event_type'];
86
+ switch (meta_event_type) {
87
+ case 'lifecycle':
88
+ return this.life_cycle(json);
89
+ case 'heartbeat':
90
+ return this.emit('meta_event.heartbeat', json);
91
+ default:
92
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown meta_event_type: ${meta_event_type}`);
93
+ return false;
94
+ }
95
+ }
96
+ life_cycle(json) {
97
+ const subType = json['sub_type'];
98
+ switch (subType) {
99
+ case 'connect':
100
+ return this.emit('meta_event.lifecycle.connect', json);
101
+ case 'enable':
102
+ return this.emit('meta_event.lifecycle.enable', json);
103
+ case 'disable':
104
+ return this.emit('meta_event.lifecycle.disable', json);
105
+ default:
106
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown meta_event.lifecycle_type: ${subType}`);
107
+ return false;
108
+ }
109
+ }
110
+ message(json) {
111
+ const messageType = json['message_type'];
112
+ switch (messageType) {
113
+ case 'private':
114
+ return this.message_private(json);
115
+ case 'group':
116
+ return this.message_group(json);
117
+ default:
118
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_type: ${messageType}`);
119
+ return false;
120
+ }
121
+ }
122
+ message_private(json) {
123
+ json.quick_action = (reply) => this.#ws.send('.handle_quick_operation', { context: json, operation: { reply } });
124
+ const subType = json['sub_type'];
125
+ switch (subType) {
126
+ case 'group':
127
+ return this.emit('message.private.group', json);
128
+ case 'friend':
129
+ return this.emit('message.private.friend', json);
130
+ default:
131
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_private_type: ${subType}`);
132
+ return false;
133
+ }
134
+ }
135
+ message_group(json) {
136
+ json.quick_action = (reply, at_sender) => this.#ws.send('.handle_quick_operation', { context: json, operation: { reply, at_sender } });
137
+ const subType = json['sub_type'];
138
+ switch (subType) {
139
+ case 'normal':
140
+ return this.emit('message.group.normal', json);
141
+ default:
142
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_group_type: ${subType}`);
143
+ return false;
144
+ }
145
+ }
146
+ message_sent(json) {
147
+ const messageType = json['message_type'];
148
+ switch (messageType) {
149
+ case 'private':
150
+ return this.message_sent_private(json);
151
+ case 'group':
152
+ return this.message_sent_group(json);
153
+ default:
154
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_type: ${messageType}`);
155
+ return false;
156
+ }
157
+ }
158
+ message_sent_private(json) {
159
+ const subType = json['sub_type'];
160
+ switch (subType) {
161
+ case 'group':
162
+ return this.emit('message_sent.private.group', json);
163
+ case 'friend':
164
+ return this.emit('message_sent.private.friend', json);
165
+ default:
166
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_private_type: ${subType}`);
167
+ return false;
168
+ }
169
+ }
170
+ message_sent_group(json) {
171
+ const subType = json['sub_type'];
172
+ switch (subType) {
173
+ case 'normal':
174
+ return this.emit('message_sent.group.normal', json);
175
+ default:
176
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_group_type: ${subType}`);
177
+ return false;
178
+ }
179
+ }
180
+ request(json) {
181
+ const request_type = json['request_type'];
182
+ switch (request_type) {
183
+ case 'friend':
184
+ json.quick_action = (approve) => this.#ws.send('.handle_quick_operation', { context: json, operation: { approve } });
185
+ return this.emit('request.friend', json);
186
+ case 'group':
187
+ return this.request_group(json);
188
+ default:
189
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown request_type: ${request_type}`);
190
+ return false;
191
+ }
192
+ }
193
+ request_group(json) {
194
+ json.quick_action = (approve, reason) => this.#ws.send('.handle_quick_operation', { context: json, operation: { approve, reason } });
195
+ const subType = json['sub_type'];
196
+ switch (subType) {
197
+ case 'add':
198
+ return this.emit('request.group.add', json);
199
+ case 'invite':
200
+ return this.emit('request.group.invite', json);
201
+ default:
202
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown request_group_type: ${subType}`);
203
+ return false;
204
+ }
205
+ }
206
+ notice(json) {
207
+ const notice_type = json['notice_type'];
208
+ switch (notice_type) {
209
+ case 'bot_offline':
210
+ return this.emit('notice.bot_offline', json);
211
+ case 'friend_add':
212
+ return this.emit('notice.friend_add', json);
213
+ case 'friend_recall':
214
+ return this.emit('notice.friend_recall', json);
215
+ case 'group_admin':
216
+ return this.notice_group_admin(json);
217
+ case 'group_ban':
218
+ return this.notice_group_ban(json);
219
+ case 'group_card':
220
+ return this.emit('notice.group_card', json);
221
+ case 'group_decrease':
222
+ return this.notice_group_decrease(json);
223
+ case 'essence':
224
+ return this.notice_essence(json);
225
+ case 'group_increase':
226
+ return this.notice_group_increase(json);
227
+ case 'notify':
228
+ return this.notice_notify(json);
229
+ case 'group_recall':
230
+ return this.emit('notice.group_recall', json);
231
+ case 'group_upload':
232
+ return this.emit('notice.group_upload', json);
233
+ case 'group_msg_emoji_like':
234
+ return this.emit('notice.group_msg_emoji_like', json);
235
+ default:
236
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_type: ${notice_type}`);
237
+ return false;
238
+ }
239
+ }
240
+ notice_group_admin(json) {
241
+ const subType = json['sub_type'];
242
+ switch (subType) {
243
+ case 'set':
244
+ return this.emit('notice.group_admin.set', json);
245
+ case 'unset':
246
+ return this.emit('notice.group_admin.unset', json);
247
+ default:
248
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_admin_type: ${subType}`);
249
+ return false;
250
+ }
251
+ }
252
+ notice_group_ban(json) {
253
+ const subType = json['sub_type'];
254
+ switch (subType) {
255
+ case 'ban':
256
+ return this.emit('notice.group_ban.ban', json);
257
+ case 'lift_ban':
258
+ return this.emit('notice.group_ban.lift_ban', json);
259
+ default:
260
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_ban_type: ${subType}`);
261
+ return false;
262
+ }
263
+ }
264
+ notice_group_decrease(json) {
265
+ const subType = json['sub_type'];
266
+ switch (subType) {
267
+ case 'leave':
268
+ return this.emit('notice.group_decrease.leave', json);
269
+ case 'kick':
270
+ return this.emit('notice.group_decrease.kick', json);
271
+ case 'kick_me':
272
+ return this.emit('notice.group_decrease.kick_me', json);
273
+ default:
274
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_decrease_type: ${subType}`);
275
+ return false;
276
+ }
277
+ }
278
+ notice_group_increase(json) {
279
+ const subType = json['sub_type'];
280
+ switch (subType) {
281
+ case 'approve':
282
+ return this.emit('notice.group_increase.approve', json);
283
+ case 'invite':
284
+ return this.emit('notice.group_increase.invite', json);
285
+ default:
286
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_increase_type: ${subType}`);
287
+ return false;
288
+ }
289
+ }
290
+ notice_essence(json) {
291
+ const subType = json['sub_type'];
292
+ switch (subType) {
293
+ case 'add':
294
+ return this.emit('notice.essence.add', json);
295
+ case 'delete':
296
+ return this.emit('notice.essence.delete', json);
297
+ default:
298
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_essence_type: ${subType}`);
299
+ return false;
300
+ }
301
+ }
302
+ notice_notify(json) {
303
+ const sub_type = json['sub_type'];
304
+ switch (sub_type) {
305
+ case 'group_name':
306
+ return this.emit('notice.notify.group_name', json);
307
+ case 'title':
308
+ return this.emit('notice.notify.title', json);
309
+ case 'input_status':
310
+ return this.notice_notify_input_status(json);
311
+ case 'poke':
312
+ return this.notice_notify_poke(json);
313
+ case 'profile_like':
314
+ return this.emit('notice.notify.profile_like', json);
315
+ default:
316
+ logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_notify_type: ${sub_type}`);
317
+ return false;
318
+ }
319
+ }
320
+ notice_notify_input_status(json) {
321
+ if (json.group_id !== 0) {
322
+ return this.emit('notice.notify.input_status.group', json);
323
+ }
324
+ else {
325
+ return this.emit('notice.notify.input_status.friend', json);
326
+ }
327
+ }
328
+ notice_notify_poke(json) {
329
+ if ('group_id' in json) {
330
+ return this.emit('notice.notify.poke.group', json);
331
+ }
332
+ else {
333
+ return this.emit('notice.notify.poke.friend', json);
334
+ }
335
+ }
336
+ }