mirror of https://github.com/Nofated095/Q2TG.git
feat: 似了吗
This commit is contained in:
parent
c0cadee504
commit
e76c4b4db9
|
@ -0,0 +1,54 @@
|
|||
import Instance from '../models/Instance';
|
||||
import Telegram from '../client/Telegram';
|
||||
import OicqClient from '../client/OicqClient';
|
||||
import { Api } from 'telegram';
|
||||
|
||||
export default class AliveCheckController {
|
||||
constructor(private readonly instance: Instance,
|
||||
private readonly tgBot: Telegram,
|
||||
private readonly tgUser: Telegram,
|
||||
private readonly oicq: OicqClient) {
|
||||
tgBot.addNewMessageEventHandler(this.handleMessage);
|
||||
}
|
||||
|
||||
private handleMessage = async (message: Api.Message) => {
|
||||
if (!message.sender.id.eq(this.instance.owner) || !message.isPrivate) {
|
||||
return false;
|
||||
}
|
||||
if (!['似了吗', '/alive'].includes(message.message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await message.reply({
|
||||
message: this.genMessage(this.instance.id === 0 ? Instance.instances : [this.instance]),
|
||||
});
|
||||
};
|
||||
|
||||
private genMessage(instances: Instance[]): string {
|
||||
const boolToStr = (value: boolean) => {
|
||||
return value ? '好' : '坏';
|
||||
};
|
||||
const messageParts: string[] = [];
|
||||
|
||||
for (const instance of instances) {
|
||||
const oicq = instance.oicq;
|
||||
const tgBot = instance.tgBot;
|
||||
const tgUser = instance.tgUser;
|
||||
|
||||
const tgUserName = (tgUser.me.username || tgUser.me.usernames.length) ?
|
||||
'@' + (tgUser.me.username || tgUser.me.usernames[0].username) : tgUser.me.firstName;
|
||||
messageParts.push([
|
||||
`Instance #${instance.id}`,
|
||||
|
||||
`QQ <code>${instance.qqUin}</code>\t` +
|
||||
`${boolToStr(oicq.isOnline())}\t${oicq.stat.msg_cnt_per_min} msg/min`,
|
||||
|
||||
`TG @${tgBot.me.username}\t${boolToStr(tgBot.isOnline)}`,
|
||||
|
||||
`TG User ${tgUserName}\t${boolToStr(tgBot.isOnline)}`,
|
||||
].join('\n'));
|
||||
}
|
||||
|
||||
return messageParts.join('\n\n');
|
||||
};
|
||||
}
|
|
@ -22,20 +22,19 @@ import db from './models/db';
|
|||
});
|
||||
const instanceEntries = await db.instance.findMany();
|
||||
|
||||
const instances = [] as Instance[];
|
||||
if (!instanceEntries.length) {
|
||||
instances.push(await Instance.start(0));
|
||||
await Instance.start(0);
|
||||
}
|
||||
else {
|
||||
for (const instanceEntry of instanceEntries) {
|
||||
instances.push(await Instance.start(instanceEntry.id));
|
||||
await Instance.start(instanceEntry.id);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
for (const instance of instances.filter(it => it.workMode === 'group')) {
|
||||
for (const instance of Instance.instances.filter(it => it.workMode === 'group')) {
|
||||
try {
|
||||
await instance.forwardPairs.initMapInstance(instances.filter(it => it.workMode === 'personal'));
|
||||
await instance.forwardPairs.initMapInstance(Instance.instances.filter(it => it.workMode === 'personal'));
|
||||
}
|
||||
catch {
|
||||
}
|
||||
|
|
|
@ -25,8 +25,11 @@ import HugController from '../controllers/HugController';
|
|||
import QuotLyController from '../controllers/QuotLyController';
|
||||
import MiraiSkipFilterController from '../controllers/MiraiSkipFilterController';
|
||||
import env from './env';
|
||||
import AliveCheckController from '../controllers/AliveCheckController';
|
||||
|
||||
export default class Instance {
|
||||
public static readonly instances: Instance[] = [];
|
||||
|
||||
private _owner = 0;
|
||||
private _isSetup = false;
|
||||
private _workMode = '';
|
||||
|
@ -58,6 +61,7 @@ export default class Instance {
|
|||
private hugController: HugController;
|
||||
private quotLyController: QuotLyController;
|
||||
private miraiSkipFilterController: MiraiSkipFilterController;
|
||||
private aliveCheckController: AliveCheckController;
|
||||
|
||||
private constructor(public readonly id: number) {
|
||||
this.log = getLogger(`Instance - ${this.id}`);
|
||||
|
@ -151,6 +155,7 @@ export default class Instance {
|
|||
this.oicqErrorNotifyController = new OicqErrorNotifyController(this, this.oicq);
|
||||
this.requestController = new RequestController(this, this.tgBot, this.oicq);
|
||||
this.configController = new ConfigController(this, this.tgBot, this.tgUser, this.oicq);
|
||||
this.aliveCheckController = new AliveCheckController(this, this.tgBot, this.tgUser, this.oicq);
|
||||
this.deleteMessageController = new DeleteMessageController(this, this.tgBot, this.tgUser, this.oicq);
|
||||
this.miraiSkipFilterController = new MiraiSkipFilterController(this, this.tgBot, this.tgUser, this.oicq);
|
||||
this.inChatCommandsController = new InChatCommandsController(this, this.tgBot, this.tgUser, this.oicq);
|
||||
|
@ -171,6 +176,7 @@ export default class Instance {
|
|||
|
||||
public static async start(instanceId: number, botToken?: string) {
|
||||
const instance = new this(instanceId);
|
||||
Instance.instances.push(instance);
|
||||
await instance.login(botToken);
|
||||
return instance;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue