UNPKG

kbotify

Version:

kaiheila bot framework

197 lines (195 loc) 7.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppCommand = exports.initFuncResult = void 0; const message_1 = require("../message"); const session_1 = require("../session"); const types_1 = require("../types"); const logger_1 = require("../logger"); function initFuncResult(data, resultType, msgSent) { const funcResult = { detail: data, resultType: resultType ? resultType : types_1.ResultTypes.PENDING, msgSent, }; return funcResult; } exports.initFuncResult = initFuncResult; /** * Class of functional command. * you need to initialize code, trigger, help, func for proper usage. * Please return ResultTypes.HELP if you need to show the help message. AppCommand.exec will handle this. * * @export * @class AppCommand * @param code 功能代号 * @param trigger 触发功能可用的字符串 * @param help 功能提示/帮助文字 * @param intro 功能简介,用于生成菜单 * @param func 负责执行功能 * @param [messageSender] 负责发送消息 * @template T */ class AppCommand { constructor() { this.code = 'code'; /** * 命令响应:仅响应频道,仅响应私聊,全部响应 */ this.response = 'guild'; /** * 默认的触发命令,如果有上级菜单需要先触发菜单 */ /** * 帮助文字,发送`.命令 帮助`时自动回复,kmarkdown消息 */ this.help = 'help'; /** * 命令介绍,自动生成菜单时调用 */ this.intro = 'intro'; /** * 接受的消息类型,默认为Button和Text,如有需要可以更改 * * @memberof AppCommand */ this.acceptMessageType = [ message_1.TextMessage, message_1.ButtonEventMessage, ]; this.parent = null; this.type = types_1.CommandTypes.APP; this.func = async (_data) => { throw new Error(`${this.code}的func尚未定义`); }; this.init = (bot) => { this.client = bot; }; /** * return true if check passed * * @param {(BaseSession | string)} sessionOrCommand * @param {(TextMessage | ButtonEventMessage)} [msg] * @memberof AppCommand */ this.checkInput = async (sessionOrCommand, msg) => { if (this.response === 'guild' && !(sessionOrCommand instanceof session_1.GuildSession)) { logger_1.kBotifyLogger.debug(`guild only command ${this.constructor.name} receiving base session. return.`); return false; } if (this.response === 'private' && !(sessionOrCommand instanceof session_1.PrivateSession)) { logger_1.kBotifyLogger.debug(`private only command ${this.constructor.name} receiving private session. return.`); return false; } msg = msg ?? sessionOrCommand.msg; for (const messageType of this.acceptMessageType) { if (msg instanceof messageType) { return true; } } return false; }; this.createSession = async (sessionOrCommand, args, msg, client) => { if (sessionOrCommand instanceof session_1.BaseSession) { let session = sessionOrCommand; // try to change basesession to guildsession if guildid exists if (sessionOrCommand.msg.guildId) { try { session = await session_1.GuildSession.fromSession(sessionOrCommand, false); } catch (error) { logger_1.kBotifyLogger.error('Error when getting guild session', sessionOrCommand); } } else if (sessionOrCommand.msg.channelType === 'PERSON') { session = await session_1.PrivateSession.fromSession(sessionOrCommand); } session.command = this; return session; } else { if (!args || !msg) { throw new Error('Missing args or msg when using exec(command, args, msg)'); } if (msg.guildId) { return new session_1.GuildSession(this, args, msg, client); } else { return new session_1.BaseSession(this, args, msg, client); } } }; // } get _botInstance() { return this.client; } async exec(sessionOrCommand, args, msg) { if (!this.client) { throw new Error('command used before assigning a bot'); } const session = await this.createSession(sessionOrCommand, args, msg); if (!this.checkInput(session, msg)) { return; } logger_1.kBotifyLogger.debug('running command', this.constructor.name); return this.run(session); /* if (sessionOrCommand instanceof BaseSession) { // try to change basesession to guildsession if guildid exists if (sessionOrCommand.msg.guildId) { try { sessionOrCommand = await GuildSession.fromSession( sessionOrCommand, false ); } catch (error) { log.error( 'Error when getting guild session', sessionOrCommand ); } } if ( !(sessionOrCommand instanceof GuildSession) && this.response == 'guild' ) { log.debug('guild only command receiving base session. return.'); return; } sessionOrCommand.command = this; return this.run(sessionOrCommand); } else { if (!args || !msg) {throw new Error( 'Missing args or msg when using exec(command, args, msg)' ); return this.run(this.createSession(this, args, msg, this.client)); }*/ } async run(session) { const args = session.args; const msg = session.msg; logger_1.kBotifyLogger.debug('running command: ', session.cmdString, args); if (!this.client) { throw new Error("'Command used before assigning a bot instance or message sender.'"); } try { if (args[0] === '帮助') { session.reply(this.help); return types_1.ResultTypes.HELP; } const result = await this.func(session); if (typeof result === 'string' || !result) { return result ? result : types_1.ResultTypes.SUCCESS; } return result.resultType; } catch (error) { logger_1.kBotifyLogger.error(error); return types_1.ResultTypes.ERROR; } } } exports.AppCommand = AppCommand; //# sourceMappingURL=command.app.js.map