kbotify
Version:
kaiheila bot framework
237 lines • 9.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSession = void 0;
const mention_by_id_1 = require("../../utils/mention-by-id");
const command_app_1 = require("../command/command.app");
const bot_object_1 = require("../base/bot.object");
const channel_1 = require("../channel");
const message_1 = require("../message");
const types_1 = require("../types");
const user_1 = require("../user");
const card_1 = require("../card");
const logger_1 = require("../logger");
const guild_1 = require("../guild");
class BaseSession extends bot_object_1.BaseObject {
constructor(command, args, msg, client) {
super(client ?? msg.client);
this.reply = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: true,
mention: true,
temp: false,
});
};
this.replyTemp = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: true,
mention: true,
temp: true,
});
};
this.replyCard = async (content, options) => {
return this._sendCard(content, { ...options, reply: true });
};
this.replyCardTemp = async (content, options) => {
return this._sendCard(content, { ...options, temp: true, reply: true });
};
/**
* Send message with quotation (no @ included)
*/
this.quote = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: true,
mention: false,
temp: false,
});
};
/**
* Send temporary message with quotation (no @ included)
*/
this.quoteTemp = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: true,
mention: false,
temp: true,
});
};
this.mention = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: false,
mention: true,
temp: false,
});
};
this.mentionTemp = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: false,
mention: true,
temp: true,
});
};
this.send = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: false,
mention: false,
temp: false,
});
};
this.sendTemp = async (content, resultType = types_1.ResultTypes.SUCCESS) => {
return this._send(content, resultType, {
reply: false,
mention: false,
temp: true,
});
};
this.sendCard = async (content, options) => {
return this._sendCard(content, options);
};
this.sendCardTemp = async (content, options) => {
return this._sendCard(content, { ...options, temp: true });
};
/**
*
* @param messageId
* @param content
* @param quote
*/
this.updateMessage = async (messageId, content, quote, tempTargetId) => {
if (Array.isArray(content)) {
content = JSON.stringify(content);
}
const result = await this.client.API.message.update(messageId, content, quote, tempTargetId);
return command_app_1.initFuncResult(result, result ? types_1.ResultTypes.SUCCESS : types_1.ResultTypes.FAIL);
};
/**
*
* @param messageId
* @param content
* @param quote
*/
this.updateMessageTemp = async (messageId, content, quote) => {
return await this.updateMessage(messageId, content, quote, this.user.id);
};
/**
* 设置文字回复触发事件
*
* @param condition 文字满足的要求,包含的文字或正则表达式
* @param timeout 单位:ms 回复触发有效时间,默认为1分钟
* @param callback 触发后的回调函数
* @returns
*/
this.setTextTrigger = (condition, timeout = 6e4, callback) => {
const func = (msg) => {
msg = msg;
if (msg.authorId != this.userId) {
return;
}
if (condition instanceof RegExp) {
if (!condition.test(msg.content)) {
return;
}
}
else if (!msg.content.includes(condition)) {
return;
}
callback(msg);
this.client.message.off('text', func);
};
this.client.message.on('text', func);
if (timeout) {
setTimeout(() => {
this.client.message.off('text', func);
}, timeout);
}
return () => {
this.client.message.off('text', func);
};
};
/**
* 发送消息。
* ### 接收
* 如果接收的消息为文字消息,则可以使用引用和@。
* 如果接收的消息为按钮点击事件消息,则可以使用@。
* ### 发送
* 如果发送的消息为文字或kmarkdown,则可以使用引用和@。
* 如果发送的消息为卡片消息,则可以使用引用。
*
* @param content
* @param [resultType=ResultTypes.SUCCESS]
* @param [sendOptions]
* @memberof BaseSession
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
this._send = async (content, resultType = types_1.ResultTypes.SUCCESS, sendOptions) => {
if (typeof content !== 'string') {
content = await content();
}
// decide if msg should be sent in specific channel.
let replyChannelId = this.msg.channelId;
replyChannelId = sendOptions?.channel ?? replyChannelId;
// decide if need mention at the start.
const msgType = sendOptions?.msgType ?? 9;
let withMention = sendOptions?.mention ?? false;
if (!this.client) {
throw new Error('session send used before bot assigned.');
}
if (msgType == 10) {
if (withMention) {
logger_1.kBotifyLogger.info('发送卡片消息时使用了mention!', this);
}
withMention = false;
content = content.replace(/(\r\n|\n|\r)/gm, '');
}
content = (withMention ? `${mention_by_id_1.mentionById(this.userId)}` : '') + content;
if (this.msg instanceof message_1.ButtonEventMessage) {
if (sendOptions?.reply) {
logger_1.kBotifyLogger.info('回复按钮点击事件时使用了引用!', this);
sendOptions.reply = undefined;
}
}
try {
const msgSent = await this.client.API.message.create(msgType, replyChannelId, content, sendOptions?.reply ? this.msg.msgId : undefined, sendOptions?.temp ? this.userId : undefined);
return command_app_1.initFuncResult(this, resultType, msgSent);
}
catch (error) {
logger_1.kBotifyLogger.error(error);
}
return command_app_1.initFuncResult(this, resultType);
};
this._sendCard = async (content, options = { reply: false, temp: false }) => {
const str = content instanceof card_1.Card
? JSON.stringify([content])
: Array.isArray(content)
? JSON.stringify(content)
: content;
return this._send(str, types_1.ResultTypes.SUCCESS, {
msgType: 10,
reply: options.reply,
mention: false,
temp: options.temp,
});
};
this.command = command;
this.args = args;
this.msg = msg;
if (msg instanceof message_1.TextMessage) {
this.userId = msg.authorId;
this.user = new user_1.BaseUser(msg.author, this.client);
}
else {
this.userId = msg.userId;
this.user = new user_1.BaseUser(msg.user, this.client);
}
if (msg.guildId) {
this.guild = new guild_1.Guild(msg.guildId, this.client);
this.channel = new channel_1.GuildChannel({ id: msg.channelId }, this.client);
}
else {
this.channel = new channel_1.PrivateChannel({ id: msg.channelId }, this.client);
}
// console.debug(this.user);
}
get guildId() {
return this.guild?.id;
}
}
exports.BaseSession = BaseSession;
//# sourceMappingURL=session.base.js.map