UNPKG

xinc

Version:

基于napcat,node-napcat-ts的bot框架

339 lines 14.1 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _NCEventBus_events, _NCEventBus_ws; Object.defineProperty(exports, "__esModule", { value: true }); exports.NCEventBus = void 0; const Utils_1 = require("./Utils"); class NCEventBus { constructor(ws) { _NCEventBus_events.set(this, new Map()); _NCEventBus_ws.set(this, void 0); __classPrivateFieldSet(this, _NCEventBus_ws, ws, "f"); } on(event, handler) { const handlers = __classPrivateFieldGet(this, _NCEventBus_events, "f").get(event) ?? []; // @ts-ignore 表达式过于复杂无法表示 if (handlers.indexOf(handler) >= 0) return this; handlers.push(handler); __classPrivateFieldGet(this, _NCEventBus_events, "f").set(event, handlers); return this; } off(event, handler) { const handlers = __classPrivateFieldGet(this, _NCEventBus_events, "f").get(event) ?? []; const index = handlers.indexOf(handler); if (index >= 0) { handlers.splice(index, 1); __classPrivateFieldGet(this, _NCEventBus_events, "f").set(event, handlers); } return this; } once(event, handler) { const onceHandler = (context) => { handler(context); this.off(event, onceHandler); }; this.on(event, onceHandler); return this; } emit(event, context) { const handlers = __classPrivateFieldGet(this, _NCEventBus_events, "f").get(event) ?? []; for (const handler of handlers) { handler(context); } // 触发总类 const indexOf = event.lastIndexOf('.'); if (indexOf > 0) return this.emit(event.slice(0, indexOf), context); return true; } parseMessage(json) { const post_type = json['post_type']; switch (post_type) { case 'meta_event': this.meta_event(json); break; case 'message': this.message(json); break; case 'message_sent': this.message_sent(json); break; case 'request': this.request(json); break; case 'notice': this.notice(json); break; default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown post_type: ${post_type}`); return false; } return true; } meta_event(json) { const meta_event_type = json['meta_event_type']; switch (meta_event_type) { case 'lifecycle': return this.life_cycle(json); case 'heartbeat': return this.emit('meta_event.heartbeat', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown meta_event_type: ${meta_event_type}`); return false; } } life_cycle(json) { const subType = json['sub_type']; switch (subType) { case 'connect': return this.emit('meta_event.lifecycle.connect', json); case 'enable': return this.emit('meta_event.lifecycle.enable', json); case 'disable': return this.emit('meta_event.lifecycle.disable', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown meta_event.lifecycle_type: ${subType}`); return false; } } message(json) { const messageType = json['message_type']; switch (messageType) { case 'private': return this.message_private(json); case 'group': return this.message_group(json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_type: ${messageType}`); return false; } } message_private(json) { json.quick_action = (reply) => __classPrivateFieldGet(this, _NCEventBus_ws, "f").send('.handle_quick_operation', { context: json, operation: { reply } }); const subType = json['sub_type']; switch (subType) { case 'group': return this.emit('message.private.group', json); case 'friend': return this.emit('message.private.friend', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_private_type: ${subType}`); return false; } } message_group(json) { json.quick_action = (reply, at_sender = false) => __classPrivateFieldGet(this, _NCEventBus_ws, "f").send('.handle_quick_operation', { context: json, operation: { reply, at_sender } }); const subType = json['sub_type']; switch (subType) { case 'normal': return this.emit('message.group.normal', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_group_type: ${subType}`); return false; } } message_sent(json) { const messageType = json['message_type']; switch (messageType) { case 'private': return this.message_sent_private(json); case 'group': return this.message_sent_group(json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_type: ${messageType}`); return false; } } message_sent_private(json) { const subType = json['sub_type']; switch (subType) { case 'group': return this.emit('message_sent.private.group', json); case 'friend': return this.emit('message_sent.private.friend', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_private_type: ${subType}`); return false; } } message_sent_group(json) { const subType = json['sub_type']; switch (subType) { case 'normal': return this.emit('message_sent.group.normal', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown message_sent_group_type: ${subType}`); return false; } } request(json) { const request_type = json['request_type']; switch (request_type) { case 'friend': json.quick_action = (approve = true) => __classPrivateFieldGet(this, _NCEventBus_ws, "f").send('.handle_quick_operation', { context: json, operation: { approve } }); return this.emit('request.friend', json); case 'group': return this.request_group(json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown request_type: ${request_type}`); return false; } } request_group(json) { json.quick_action = (approve = true, reason) => __classPrivateFieldGet(this, _NCEventBus_ws, "f").send('.handle_quick_operation', { context: json, operation: { approve, reason } }); const subType = json['sub_type']; switch (subType) { case 'add': return this.emit('request.group.add', json); case 'invite': return this.emit('request.group.invte', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown request_group_type: ${subType}`); return false; } } notice(json) { const notice_type = json['notice_type']; switch (notice_type) { case 'bot_offline': return this.emit('notice.bot_offline', json); case 'friend_add': return this.emit('notice.friend_add', json); case 'friend_recall': return this.emit('notice.friend_recall', json); case 'group_admin': return this.notice_group_admin(json); case 'group_ban': return this.notice_group_ban(json); case 'group_card': return this.emit('notice.group_card', json); case 'group_decrease': return this.notice_group_decrease(json); case 'essence': return this.notice_essence(json); case 'group_increase': return this.notice_group_increase(json); case 'notify': return this.notice_notify(json); case 'group_recall': return this.emit('notice.group_recall', json); case 'group_upload': return this.emit('notice.group_upload', json); case 'group_msg_emoji_like': return this.emit('notice.group_msg_emoji_like', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_type: ${notice_type}`); return false; } } notice_group_admin(json) { const subType = json['sub_type']; switch (subType) { case 'set': return this.emit('notice.group_admin.set', json); case 'unset': return this.emit('notice.group_admin.unset', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_admin_type: ${subType}`); return false; } } notice_group_ban(json) { const subType = json['sub_type']; switch (subType) { case 'ban': return this.emit('notice.group_ban.ban', json); case 'lift_ban': return this.emit('notice.group_ban.lift_ban', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_ban_type: ${subType}`); return false; } } notice_group_decrease(json) { const subType = json['sub_type']; switch (subType) { case 'leave': return this.emit('notice.group_decrease.leave', json); case 'kick': return this.emit('notice.group_decrease.kick', json); case 'kick_me': return this.emit('notice.group_decrease.kick_me', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_decrease_type: ${subType}`); return false; } } notice_group_increase(json) { const subType = json['sub_type']; switch (subType) { case 'approve': return this.emit('notice.group_increase.approve', json); case 'invite': return this.emit('notice.group_increase.invite', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_group_increase_type: ${subType}`); return false; } } notice_essence(json) { const subType = json['sub_type']; switch (subType) { case 'add': return this.emit('notice.essence.add', json); case 'delete': return this.emit('notice.essence.delete', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_essence_type: ${subType}`); return false; } } notice_notify(json) { const sub_type = json['sub_type']; switch (sub_type) { case 'group_name': return this.emit('notice.notify.group_name', json); case 'title': return this.emit('notice.notify.title', json); case 'input_status': return this.notice_notify_input_status(json); case 'poke': return this.notice_notify_poke(json); case 'profile_like': return this.emit('notice.notify.profile_like', json); default: Utils_1.logger.warn('[node-napcat-ts]', '[eventBus]', `unknown notice_notify_type: ${sub_type}`); return false; } } notice_notify_input_status(json) { if (json.group_id !== 0) { return this.emit('notice.notify.input_status.group', json); } else { return this.emit('notice.notify.input_status.friend', json); } } notice_notify_poke(json) { if ('group_id' in json) { return this.emit('notice.notify.poke.group', json); } else { return this.emit('notice.notify.poke.friend', json); } } } exports.NCEventBus = NCEventBus; _NCEventBus_events = new WeakMap(), _NCEventBus_ws = new WeakMap(); //# sourceMappingURL=NCEventBus.js.map