2022-03-07 08:36:13 +00:00
|
|
|
import { WorkMode } from '../types/definitions';
|
|
|
|
import db from './db';
|
|
|
|
import ConfigController from '../controllers/ConfigController';
|
|
|
|
import SetupController from '../controllers/SetupController';
|
|
|
|
import ForwardController from '../controllers/ForwardController';
|
|
|
|
import DeleteMessageController from '../controllers/DeleteMessageController';
|
|
|
|
import FileAndFlashPhotoController from '../controllers/FileAndFlashPhotoController';
|
|
|
|
import Telegram from '../client/Telegram';
|
|
|
|
import OicqClient from '../client/OicqClient';
|
|
|
|
import { getLogger, Logger } from 'log4js';
|
2022-03-07 10:05:14 +00:00
|
|
|
import ForwardPairs from './ForwardPairs';
|
2022-03-07 12:50:05 +00:00
|
|
|
import InstanceManageController from '../controllers/InstanceManageController';
|
2022-03-12 04:57:18 +00:00
|
|
|
import InChatCommandsController from '../controllers/InChatCommandsController';
|
2022-03-16 14:34:11 +00:00
|
|
|
import { Api } from 'telegram';
|
|
|
|
import commands from '../constants/commands';
|
|
|
|
import TelegramChat from '../client/TelegramChat';
|
2022-03-30 05:27:14 +00:00
|
|
|
import RequestController from '../controllers/RequestController';
|
2022-04-09 09:56:36 +00:00
|
|
|
import OicqErrorNotifyController from '../controllers/OicqErrorNotifyController';
|
2022-07-28 04:43:55 +00:00
|
|
|
import { MarkupLike } from 'telegram/define';
|
|
|
|
import { Button } from 'telegram/tl/custom/button';
|
|
|
|
import { CustomFile } from 'telegram/client/uploads';
|
2022-12-23 06:34:25 +00:00
|
|
|
import { QqBot } from '@prisma/client';
|
2022-12-23 08:41:20 +00:00
|
|
|
import StatusReportController from '../controllers/StatusReportController';
|
2022-12-26 04:15:44 +00:00
|
|
|
import HugController from '../controllers/HugController';
|
2022-03-07 08:36:13 +00:00
|
|
|
|
|
|
|
export default class Instance {
|
|
|
|
private _owner = 0;
|
|
|
|
private _isSetup = false;
|
|
|
|
private _workMode = '';
|
2022-12-23 06:34:25 +00:00
|
|
|
private _botSessionId = 0;
|
|
|
|
private _userSessionId = 0;
|
|
|
|
private _qq: QqBot;
|
2022-12-23 08:41:20 +00:00
|
|
|
private _reportUrl: string;
|
2022-03-07 08:36:13 +00:00
|
|
|
|
|
|
|
private readonly log: Logger;
|
|
|
|
|
2022-03-07 12:50:05 +00:00
|
|
|
private tgBot: Telegram;
|
|
|
|
private tgUser: Telegram;
|
|
|
|
private oicq: OicqClient;
|
|
|
|
|
2022-03-16 14:34:11 +00:00
|
|
|
private _ownerChat: TelegramChat;
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
public forwardPairs: ForwardPairs;
|
2022-03-07 08:36:13 +00:00
|
|
|
private setupController: SetupController;
|
2022-03-07 12:50:05 +00:00
|
|
|
private instanceManageController: InstanceManageController;
|
2022-04-09 09:56:36 +00:00
|
|
|
private oicqErrorNotifyController: OicqErrorNotifyController;
|
2022-03-30 05:27:14 +00:00
|
|
|
private requestController: RequestController;
|
2022-03-07 08:36:13 +00:00
|
|
|
private configController: ConfigController;
|
|
|
|
private deleteMessageController: DeleteMessageController;
|
2022-03-12 04:57:18 +00:00
|
|
|
private inChatCommandsController: InChatCommandsController;
|
2022-03-07 08:36:13 +00:00
|
|
|
private forwardController: ForwardController;
|
|
|
|
private fileAndFlashPhotoController: FileAndFlashPhotoController;
|
2022-12-23 08:41:20 +00:00
|
|
|
private statusReportController: StatusReportController;
|
2022-12-26 04:15:44 +00:00
|
|
|
private hugController: HugController;
|
2022-03-07 08:36:13 +00:00
|
|
|
|
|
|
|
private constructor(public readonly id: number) {
|
2022-03-07 12:50:05 +00:00
|
|
|
this.log = getLogger(`Instance - ${this.id}`);
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async load() {
|
|
|
|
const dbEntry = await db.instance.findFirst({
|
|
|
|
where: { id: this.id },
|
2022-12-23 06:34:25 +00:00
|
|
|
include: { qqBot: true },
|
2022-03-07 08:36:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!dbEntry) {
|
|
|
|
if (this.id === 0) {
|
|
|
|
// 创建零号实例
|
|
|
|
await db.instance.create({
|
|
|
|
data: { id: 0 },
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new Error('Instance not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._owner = Number(dbEntry.owner);
|
2022-12-23 06:34:25 +00:00
|
|
|
this._qq = dbEntry.qqBot;
|
|
|
|
this._botSessionId = dbEntry.botSessionId;
|
|
|
|
this._userSessionId = dbEntry.userSessionId;
|
2022-03-07 08:36:13 +00:00
|
|
|
this._isSetup = dbEntry.isSetup;
|
|
|
|
this._workMode = dbEntry.workMode;
|
2022-12-23 08:41:20 +00:00
|
|
|
this._reportUrl = dbEntry.reportUrl;
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
private async init(botToken?: string) {
|
2022-03-07 08:36:13 +00:00
|
|
|
this.log.debug('正在登录 TG Bot');
|
2022-12-23 06:34:25 +00:00
|
|
|
if (this.botSessionId) {
|
|
|
|
this.tgBot = await Telegram.connect(this._botSessionId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const token = this.id === 0 ? process.env.TG_BOT_TOKEN : botToken;
|
|
|
|
if (!token) {
|
|
|
|
throw new Error('botToken 未指定');
|
|
|
|
}
|
|
|
|
this.tgBot = await Telegram.create({
|
|
|
|
botAuthToken: token,
|
|
|
|
});
|
|
|
|
this.botSessionId = this.tgBot.sessionId;
|
|
|
|
}
|
2022-03-07 12:50:05 +00:00
|
|
|
this.log.info('TG Bot 登录完成');
|
|
|
|
(async () => {
|
2022-07-28 04:43:55 +00:00
|
|
|
if (!this.isSetup || !this._owner) {
|
2022-03-07 12:50:05 +00:00
|
|
|
this.log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
|
|
|
|
this.setupController = new SetupController(this, this.tgBot);
|
|
|
|
// 这会一直卡在这里,所以要新开一个异步来做,提前返回掉上面的
|
|
|
|
({ tgUser: this.tgUser, oicq: this.oicq } = await this.setupController.waitForFinish());
|
2022-07-28 04:43:55 +00:00
|
|
|
this._ownerChat = await this.tgBot.getChat(this.owner);
|
2022-03-07 12:50:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.log.debug('正在登录 TG UserBot');
|
2022-12-23 06:34:25 +00:00
|
|
|
this.tgUser = await Telegram.connect(this._userSessionId);
|
2022-03-07 12:50:05 +00:00
|
|
|
this.log.info('TG UserBot 登录完成');
|
2022-07-28 04:43:55 +00:00
|
|
|
this._ownerChat = await this.tgBot.getChat(this.owner);
|
2022-03-07 12:50:05 +00:00
|
|
|
this.log.debug('正在登录 OICQ');
|
|
|
|
this.oicq = await OicqClient.create({
|
2022-12-23 06:34:25 +00:00
|
|
|
id: this.qq.id,
|
|
|
|
uin: Number(this.qq.uin),
|
|
|
|
password: this.qq.password,
|
|
|
|
platform: this.qq.platform,
|
2022-07-28 04:43:55 +00:00
|
|
|
onQrCode: async (file) => {
|
|
|
|
await this.ownerChat.sendMessage({
|
|
|
|
message: '请使用已登录这个账号的手机 QQ 扫描这个二维码授权',
|
|
|
|
file: new CustomFile('qrcode.png', file.length, '', file),
|
|
|
|
buttons: Button.text('我已扫码', true, true),
|
|
|
|
});
|
|
|
|
await this.waitForOwnerInput();
|
|
|
|
},
|
|
|
|
onVerifyDevice: async (phone) => {
|
|
|
|
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
|
|
|
|
},
|
|
|
|
onVerifySlider: async (url) => {
|
2022-12-23 06:34:25 +00:00
|
|
|
return await this.waitForOwnerInput(`收到滑块验证码 <code>${url}</code>\n` +
|
|
|
|
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket',
|
|
|
|
);
|
2022-07-28 04:43:55 +00:00
|
|
|
},
|
2022-03-07 12:50:05 +00:00
|
|
|
});
|
|
|
|
this.log.info('OICQ 登录完成');
|
|
|
|
}
|
2022-12-23 08:41:20 +00:00
|
|
|
this.statusReportController = new StatusReportController(this, this.tgBot, this.tgUser, this.oicq);
|
2022-03-07 12:50:05 +00:00
|
|
|
this.forwardPairs = await ForwardPairs.load(this.id, this.oicq, this.tgBot);
|
2022-08-14 04:47:49 +00:00
|
|
|
this.setupCommands()
|
|
|
|
.then(() => this.log.info('命令设置成功'))
|
|
|
|
.catch(e => this.log.error('命令设置错误', e));
|
2022-03-07 12:50:05 +00:00
|
|
|
if (this.id === 0) {
|
|
|
|
this.instanceManageController = new InstanceManageController(this, this.tgBot);
|
|
|
|
}
|
2022-04-09 09:56:36 +00:00
|
|
|
this.oicqErrorNotifyController = new OicqErrorNotifyController(this, this.oicq);
|
2022-03-30 05:27:14 +00:00
|
|
|
this.requestController = new RequestController(this, this.tgBot, this.oicq);
|
2022-03-07 12:50:05 +00:00
|
|
|
this.configController = new ConfigController(this, this.tgBot, this.tgUser, this.oicq);
|
|
|
|
this.deleteMessageController = new DeleteMessageController(this, this.tgBot, this.tgUser, this.oicq);
|
2022-12-24 12:31:39 +00:00
|
|
|
this.inChatCommandsController = new InChatCommandsController(this, this.tgBot, this.tgUser, this.oicq);
|
2022-12-26 04:15:44 +00:00
|
|
|
if (this.workMode === 'group') {
|
|
|
|
this.hugController = new HugController(this, this.tgBot, this.oicq);
|
|
|
|
}
|
2022-03-07 12:50:05 +00:00
|
|
|
this.forwardController = new ForwardController(this, this.tgBot, this.tgUser, this.oicq);
|
|
|
|
this.fileAndFlashPhotoController = new FileAndFlashPhotoController(this, this.tgBot, this.oicq);
|
|
|
|
})()
|
|
|
|
.then(() => this.log.info('初始化已完成'));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
public async login(botToken?: string) {
|
2022-10-26 10:50:00 +00:00
|
|
|
await this.load();
|
2022-12-23 06:34:25 +00:00
|
|
|
await this.init(botToken);
|
2022-10-26 10:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
public static async start(instanceId: number, botToken?: string) {
|
2022-03-07 08:36:13 +00:00
|
|
|
const instance = new this(instanceId);
|
2022-12-23 06:34:25 +00:00
|
|
|
await instance.login(botToken);
|
2022-03-07 08:36:13 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
public static async createNew(botToken: string) {
|
2022-12-23 06:34:25 +00:00
|
|
|
const dbEntry = await db.instance.create({ data: {} });
|
|
|
|
return await this.start(dbEntry.id, botToken);
|
2022-03-07 10:05:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 14:34:11 +00:00
|
|
|
private async setupCommands() {
|
|
|
|
await this.tgBot.setCommands([], new Api.BotCommandScopeUsers());
|
|
|
|
// 设定管理员的
|
|
|
|
if (this.id === 0) {
|
|
|
|
await this.tgBot.setCommands(
|
|
|
|
this.workMode === 'personal' ? commands.personalPrivateSuperAdminCommands : commands.groupPrivateSuperAdminCommands,
|
|
|
|
new Api.BotCommandScopePeer({
|
|
|
|
peer: (this.ownerChat).inputPeer,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
await this.tgBot.setCommands(
|
|
|
|
this.workMode === 'personal' ? commands.personalPrivateCommands : commands.groupPrivateCommands,
|
|
|
|
new Api.BotCommandScopePeer({
|
|
|
|
peer: (this.ownerChat).inputPeer,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 设定群组内的
|
|
|
|
await this.tgBot.setCommands(
|
2022-04-07 07:06:50 +00:00
|
|
|
this.workMode === 'personal' ? commands.personalInChatCommands : commands.groupInChatCommands,
|
2022-03-16 14:34:11 +00:00
|
|
|
// 普通用户其实不需要这些命令,这样可以让用户的输入框少点东西
|
|
|
|
new Api.BotCommandScopeChatAdmins(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 04:43:55 +00:00
|
|
|
private async waitForOwnerInput(message?: string, buttons?: MarkupLike, remove = false) {
|
|
|
|
if (!this.owner) {
|
|
|
|
throw new Error('应该不会运行到这里');
|
|
|
|
}
|
|
|
|
message && await this.ownerChat.sendMessage({ message, buttons: buttons || Button.clear(), linkPreview: false });
|
|
|
|
const reply = await this.ownerChat.waitForInput();
|
|
|
|
remove && await reply.delete({ revoke: true });
|
|
|
|
return reply.message;
|
|
|
|
}
|
|
|
|
|
2022-03-07 08:36:13 +00:00
|
|
|
get owner() {
|
|
|
|
return this._owner;
|
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
get qq() {
|
|
|
|
return this._qq;
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
get qqUin() {
|
|
|
|
return this.oicq.uin;
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isSetup() {
|
|
|
|
return this._isSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
get workMode() {
|
|
|
|
return this._workMode as WorkMode;
|
|
|
|
}
|
|
|
|
|
2022-03-07 12:50:05 +00:00
|
|
|
get botMe() {
|
|
|
|
return this.tgBot.me;
|
|
|
|
}
|
|
|
|
|
2022-03-17 04:42:22 +00:00
|
|
|
get userMe() {
|
|
|
|
return this.tgUser.me;
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:34:11 +00:00
|
|
|
get ownerChat() {
|
|
|
|
return this._ownerChat;
|
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
get botSessionId() {
|
|
|
|
return this._botSessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userSessionId() {
|
|
|
|
return this._userSessionId;
|
|
|
|
}
|
|
|
|
|
2022-12-23 08:41:20 +00:00
|
|
|
get reportUrl() {
|
|
|
|
return this._reportUrl;
|
|
|
|
}
|
|
|
|
|
2022-03-07 08:36:13 +00:00
|
|
|
set owner(owner: number) {
|
|
|
|
this._owner = owner;
|
|
|
|
db.instance.update({
|
|
|
|
data: { owner },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(owner));
|
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
set isSetup(isSetup: boolean) {
|
|
|
|
this._isSetup = isSetup;
|
2022-03-07 08:36:13 +00:00
|
|
|
db.instance.update({
|
2022-12-23 06:34:25 +00:00
|
|
|
data: { isSetup },
|
2022-03-07 08:36:13 +00:00
|
|
|
where: { id: this.id },
|
|
|
|
})
|
2022-12-23 06:34:25 +00:00
|
|
|
.then(() => this.log.trace(isSetup));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
set workMode(workMode: WorkMode) {
|
|
|
|
this._workMode = workMode;
|
2022-03-07 08:36:13 +00:00
|
|
|
db.instance.update({
|
2022-12-23 06:34:25 +00:00
|
|
|
data: { workMode },
|
2022-03-07 08:36:13 +00:00
|
|
|
where: { id: this.id },
|
|
|
|
})
|
2022-12-23 06:34:25 +00:00
|
|
|
.then(() => this.log.trace(workMode));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
set botSessionId(sessionId: number) {
|
|
|
|
this._botSessionId = sessionId;
|
2022-03-07 08:36:13 +00:00
|
|
|
db.instance.update({
|
2022-12-23 06:34:25 +00:00
|
|
|
data: { botSessionId: sessionId },
|
2022-03-07 08:36:13 +00:00
|
|
|
where: { id: this.id },
|
|
|
|
})
|
2022-12-23 06:34:25 +00:00
|
|
|
.then(() => this.log.trace(sessionId));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
set userSessionId(sessionId: number) {
|
|
|
|
this._userSessionId = sessionId;
|
2022-03-07 08:36:13 +00:00
|
|
|
db.instance.update({
|
2022-12-23 06:34:25 +00:00
|
|
|
data: { userSessionId: sessionId },
|
2022-03-07 08:36:13 +00:00
|
|
|
where: { id: this.id },
|
|
|
|
})
|
2022-12-23 06:34:25 +00:00
|
|
|
.then(() => this.log.trace(sessionId));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 06:34:25 +00:00
|
|
|
set qqBotId(id: number) {
|
2022-03-07 08:36:13 +00:00
|
|
|
db.instance.update({
|
2022-12-23 06:34:25 +00:00
|
|
|
data: { qqBotId: id },
|
2022-03-07 08:36:13 +00:00
|
|
|
where: { id: this.id },
|
|
|
|
})
|
2022-12-23 06:34:25 +00:00
|
|
|
.then(() => this.log.trace(id));
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
2022-12-23 08:41:20 +00:00
|
|
|
|
|
|
|
set reportUrl(reportUrl: string) {
|
|
|
|
db.instance.update({
|
|
|
|
data: { reportUrl },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(reportUrl));
|
|
|
|
}
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|