2022-03-07 08:36:13 +00:00
|
|
|
import { WorkMode } from '../types/definitions';
|
|
|
|
import db from './db';
|
|
|
|
import { Platform } from 'oicq';
|
|
|
|
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-03-07 08:36:13 +00:00
|
|
|
|
|
|
|
export default class Instance {
|
|
|
|
private _owner = 0;
|
|
|
|
private _qqUin = 0;
|
|
|
|
private _qqPassword = '';
|
|
|
|
private _qqPlatform = 0;
|
|
|
|
private _isSetup = false;
|
|
|
|
private _workMode = '';
|
2022-03-07 10:05:14 +00:00
|
|
|
private _botToken = '';
|
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-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;
|
|
|
|
|
|
|
|
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 },
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
this._qqUin = Number(dbEntry.qqUin);
|
|
|
|
this._qqPassword = dbEntry.qqPassword;
|
|
|
|
this._qqPlatform = dbEntry.qqPlatform;
|
|
|
|
this._isSetup = dbEntry.isSetup;
|
|
|
|
this._workMode = dbEntry.workMode;
|
2022-03-07 10:05:14 +00:00
|
|
|
this._botToken = dbEntry.botToken;
|
2022-03-07 08:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async init() {
|
|
|
|
this.log.debug('正在登录 TG Bot');
|
2022-03-07 12:50:05 +00:00
|
|
|
this.tgBot = await Telegram.create({
|
2022-03-07 10:05:14 +00:00
|
|
|
botAuthToken: this.botToken,
|
2022-03-07 12:50:05 +00:00
|
|
|
}, `bot:${this.id}`);
|
|
|
|
this.log.info('TG Bot 登录完成');
|
|
|
|
(async () => {
|
|
|
|
if (!this.isSetup) {
|
|
|
|
this.log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
|
|
|
|
this.setupController = new SetupController(this, this.tgBot);
|
|
|
|
// 这会一直卡在这里,所以要新开一个异步来做,提前返回掉上面的
|
|
|
|
({ tgUser: this.tgUser, oicq: this.oicq } = await this.setupController.waitForFinish());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.log.debug('正在登录 TG UserBot');
|
|
|
|
this.tgUser = await Telegram.connect(`user:${this.id}`);
|
|
|
|
this.log.info('TG UserBot 登录完成');
|
|
|
|
this.log.debug('正在登录 OICQ');
|
|
|
|
this.oicq = await OicqClient.create({
|
|
|
|
uin: this.qqUin,
|
|
|
|
password: this.qqPassword,
|
|
|
|
platform: this.qqPlatform,
|
|
|
|
onVerifyDevice: () => null,
|
|
|
|
onVerifySlider: () => null,
|
|
|
|
onQrCode: () => null,
|
|
|
|
});
|
|
|
|
this.log.info('OICQ 登录完成');
|
|
|
|
}
|
2022-03-16 14:34:11 +00:00
|
|
|
this._ownerChat = await this.tgBot.getChat(this.owner);
|
2022-03-07 12:50:05 +00:00
|
|
|
this.forwardPairs = await ForwardPairs.load(this.id, this.oicq, this.tgBot);
|
2022-03-16 14:34:11 +00:00
|
|
|
this.setupCommands();
|
2022-03-07 12:50:05 +00:00
|
|
|
if (this.id === 0) {
|
|
|
|
this.instanceManageController = new InstanceManageController(this, this.tgBot);
|
|
|
|
}
|
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-03-12 04:57:18 +00:00
|
|
|
this.inChatCommandsController = new InChatCommandsController(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
|
|
|
}
|
|
|
|
|
|
|
|
public static async start(instanceId: number) {
|
|
|
|
const instance = new this(instanceId);
|
|
|
|
await instance.load();
|
|
|
|
await instance.init();
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
public static async createNew(botToken: string) {
|
|
|
|
const dbEntry = await db.instance.create({
|
|
|
|
data: { botToken },
|
|
|
|
});
|
|
|
|
return await this.start(dbEntry.id);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
this.workMode === 'personal' ? commands.privateInChatCommands : commands.groupInChatCommands,
|
|
|
|
// 普通用户其实不需要这些命令,这样可以让用户的输入框少点东西
|
|
|
|
new Api.BotCommandScopeChatAdmins(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-07 08:36:13 +00:00
|
|
|
get owner() {
|
|
|
|
return this._owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
get qqUin() {
|
|
|
|
return this._qqUin;
|
|
|
|
}
|
|
|
|
|
|
|
|
get qqPassword() {
|
|
|
|
return this._qqPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
get qqPlatform() {
|
|
|
|
return this._qqPlatform as Platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isSetup() {
|
|
|
|
return this._isSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
get workMode() {
|
|
|
|
return this._workMode as WorkMode;
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:05:14 +00:00
|
|
|
get botToken() {
|
|
|
|
return this.id === 0 ? process.env.TG_BOT_TOKEN : this._botToken;
|
|
|
|
}
|
|
|
|
|
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-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));
|
|
|
|
}
|
|
|
|
|
|
|
|
set qqUin(qqUin: number) {
|
|
|
|
this._qqUin = qqUin;
|
|
|
|
db.instance.update({
|
|
|
|
data: { qqUin },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(qqUin));
|
|
|
|
}
|
|
|
|
|
|
|
|
set qqPassword(qqPassword: string) {
|
|
|
|
this._qqPassword = qqPassword;
|
|
|
|
db.instance.update({
|
|
|
|
data: { qqPassword },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(qqPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
set qqPlatform(qqPlatform: Platform) {
|
|
|
|
this._qqPlatform = qqPlatform;
|
|
|
|
db.instance.update({
|
|
|
|
data: { qqPlatform },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(qqPlatform));
|
|
|
|
}
|
|
|
|
|
|
|
|
set isSetup(isSetup: boolean) {
|
|
|
|
this._isSetup = isSetup;
|
|
|
|
db.instance.update({
|
|
|
|
data: { isSetup },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(isSetup));
|
|
|
|
}
|
|
|
|
|
|
|
|
set workMode(workMode: WorkMode) {
|
|
|
|
this._workMode = workMode;
|
|
|
|
db.instance.update({
|
|
|
|
data: { workMode },
|
|
|
|
where: { id: this.id },
|
|
|
|
})
|
|
|
|
.then(() => this.log.trace(workMode));
|
|
|
|
}
|
|
|
|
}
|