2022-02-20 08:25:30 +00:00
|
|
|
import { Api } from 'telegram';
|
2022-02-23 09:11:04 +00:00
|
|
|
import Telegram from '../client/Telegram';
|
|
|
|
import OicqClient from '../client/OicqClient';
|
2022-02-20 08:25:30 +00:00
|
|
|
import ConfigService from '../services/ConfigService';
|
2022-02-20 10:01:07 +00:00
|
|
|
import regExps from '../constants/regExps';
|
2022-03-02 06:07:21 +00:00
|
|
|
import { GroupMessageEvent, MemberIncreaseEvent, PrivateMessageEvent } from 'oicq';
|
2022-03-07 08:36:13 +00:00
|
|
|
import Instance from '../models/Instance';
|
2022-03-08 06:38:40 +00:00
|
|
|
import { getLogger, Logger } from 'log4js';
|
2022-02-20 08:25:30 +00:00
|
|
|
|
|
|
|
export default class ConfigController {
|
|
|
|
private readonly configService: ConfigService;
|
2022-03-01 13:11:14 +00:00
|
|
|
private readonly createPrivateMessageGroupBlockList = new Map<number, Promise<void>>();
|
2022-03-08 06:38:40 +00:00
|
|
|
private readonly log: Logger;
|
2022-02-20 08:25:30 +00:00
|
|
|
|
2022-03-07 08:36:13 +00:00
|
|
|
constructor(private readonly instance: Instance,
|
|
|
|
private readonly tgBot: Telegram,
|
2022-02-20 08:25:30 +00:00
|
|
|
private readonly tgUser: Telegram,
|
|
|
|
private readonly oicq: OicqClient) {
|
2022-03-08 06:38:40 +00:00
|
|
|
this.log = getLogger(`ConfigController - ${instance.id}`);
|
2022-03-07 08:36:13 +00:00
|
|
|
this.configService = new ConfigService(this.instance, tgBot, tgUser, oicq);
|
2022-02-20 08:25:30 +00:00
|
|
|
tgBot.addNewMessageEventHandler(this.handleMessage);
|
2022-03-01 08:14:04 +00:00
|
|
|
tgBot.addNewServiceMessageEventHandler(this.handleServiceMessage);
|
2022-03-08 06:38:40 +00:00
|
|
|
tgBot.addChannelParticipantEventHandler(this.handleChannelParticipant);
|
2022-03-01 13:11:14 +00:00
|
|
|
oicq.addNewMessageEventHandler(this.handleQqMessage);
|
2022-03-07 08:36:13 +00:00
|
|
|
this.instance.workMode === 'personal' && oicq.on('notice.group.increase', this.handleMemberIncrease);
|
|
|
|
this.instance.workMode === 'personal' && this.configService.setupFilter();
|
2022-02-20 08:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private handleMessage = async (message: Api.Message) => {
|
2022-03-07 08:36:13 +00:00
|
|
|
if (!message.sender.id.eq(this.instance.owner)) {
|
2022-02-20 08:25:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-20 10:01:07 +00:00
|
|
|
const messageSplit = message.message.split(' ');
|
|
|
|
if (message.isGroup) {
|
2022-03-07 13:15:16 +00:00
|
|
|
if (messageSplit.length === 2 && messageSplit[0] === `/start@${this.tgBot.me.username}` && regExps.roomId.test(messageSplit[1])) {
|
2022-02-21 06:30:02 +00:00
|
|
|
await this.configService.createLinkGroup(Number(messageSplit[1]), Number(message.chat.id));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2022-02-20 10:01:07 +00:00
|
|
|
}
|
|
|
|
else if (message.isPrivate) {
|
2022-03-07 08:36:13 +00:00
|
|
|
if (this.instance.workMode === 'personal') {
|
2022-02-22 08:24:43 +00:00
|
|
|
switch (messageSplit[0]) {
|
|
|
|
case '/addfriend':
|
2022-02-22 11:17:17 +00:00
|
|
|
await this.configService.addFriend();
|
2022-02-22 08:24:43 +00:00
|
|
|
return true;
|
|
|
|
case '/addgroup':
|
2022-02-22 11:17:17 +00:00
|
|
|
await this.configService.addGroup();
|
2022-02-22 08:24:43 +00:00
|
|
|
return true;
|
2022-03-05 12:50:35 +00:00
|
|
|
case '/migrate':
|
|
|
|
await this.configService.migrateAllChats();
|
|
|
|
return true;
|
2022-02-22 08:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (messageSplit[0]) {
|
|
|
|
case '/add':
|
|
|
|
// 加的参数永远是正的群号
|
|
|
|
if (messageSplit.length === 3 && regExps.qq.test(messageSplit[1]) && !isNaN(Number(messageSplit[2]))) {
|
|
|
|
await this.configService.createLinkGroup(-Number(messageSplit[1]), Number(messageSplit[2]));
|
|
|
|
}
|
|
|
|
else if (messageSplit[1] && regExps.qq.test(messageSplit[1])) {
|
|
|
|
await this.configService.addExact(Number(messageSplit[1]));
|
|
|
|
}
|
|
|
|
else {
|
2022-02-22 11:17:17 +00:00
|
|
|
await this.configService.addGroup();
|
2022-02-22 08:24:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-20 10:01:07 +00:00
|
|
|
}
|
2022-02-20 08:25:30 +00:00
|
|
|
}
|
|
|
|
};
|
2022-03-01 08:14:04 +00:00
|
|
|
|
|
|
|
private handleServiceMessage = async (message: Api.MessageService) => {
|
2022-03-02 06:07:21 +00:00
|
|
|
// 用于检测群升级为超级群的情况
|
2022-03-01 08:14:04 +00:00
|
|
|
if (message.action instanceof Api.MessageActionChatMigrateTo) {
|
2022-03-07 10:05:14 +00:00
|
|
|
const pair = this.instance.forwardPairs.find((message.peerId as Api.PeerChat).chatId);
|
2022-03-01 08:14:04 +00:00
|
|
|
if (!pair) return;
|
2022-03-05 12:23:52 +00:00
|
|
|
// 会自动写入数据库
|
2022-03-01 08:14:04 +00:00
|
|
|
pair.tg = await this.tgBot.getChat(message.action.channelId);
|
2022-03-05 12:23:52 +00:00
|
|
|
// 升级之后 bot 的管理权限可能没了,需要修复一下
|
2022-03-07 08:36:13 +00:00
|
|
|
if (this.instance.workMode === 'personal') {
|
2022-03-05 12:23:52 +00:00
|
|
|
const chatForUser = await this.tgUser.getChat(message.action.channelId);
|
|
|
|
await chatForUser.setAdmin(this.tgBot.me.username);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
await pair.tg.sendMessage({
|
|
|
|
message: '本群已升级为超级群,可能需要重新设置一下管理员权限',
|
|
|
|
silent: true,
|
|
|
|
});
|
|
|
|
}
|
2022-03-01 08:14:04 +00:00
|
|
|
}
|
|
|
|
};
|
2022-03-01 13:11:14 +00:00
|
|
|
|
2022-03-08 06:38:40 +00:00
|
|
|
private handleChannelParticipant = async (event: Api.UpdateChannelParticipant) => {
|
2022-03-08 06:53:17 +00:00
|
|
|
if (event.prevParticipant && 'userId' in event.prevParticipant &&
|
|
|
|
event.prevParticipant.userId.eq(this.tgBot.me.id) &&
|
2022-03-08 06:46:56 +00:00
|
|
|
!event.newParticipant) {
|
2022-03-08 06:38:40 +00:00
|
|
|
this.log.warn(`群 ${event.channelId.toString()} 删除了`);
|
|
|
|
const pair = this.instance.forwardPairs.find(event.channelId);
|
|
|
|
if (pair) {
|
|
|
|
await this.instance.forwardPairs.remove(pair);
|
|
|
|
this.log.info(`已删除关联 ID: ${pair.dbId}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-01 13:11:14 +00:00
|
|
|
private handleQqMessage = async (message: GroupMessageEvent | PrivateMessageEvent) => {
|
2022-03-07 13:04:53 +00:00
|
|
|
if (message.message_type !== 'private' || this.instance.workMode === 'group') return false;
|
2022-03-07 10:05:14 +00:00
|
|
|
const pair = this.instance.forwardPairs.find(message.friend);
|
2022-03-01 13:11:14 +00:00
|
|
|
if (pair) return false;
|
|
|
|
// 如果正在创建中,应该阻塞
|
|
|
|
let promise = this.createPrivateMessageGroupBlockList.get(message.from_id);
|
|
|
|
if (promise) {
|
|
|
|
await promise;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 有未创建转发群的新私聊消息时自动创建
|
|
|
|
promise = this.configService.createGroupAndLink(message.from_id, message.friend.remark || message.friend.nickname, true);
|
|
|
|
this.createPrivateMessageGroupBlockList.set(message.from_id, promise);
|
|
|
|
await promise;
|
|
|
|
return false;
|
|
|
|
};
|
2022-03-02 06:07:21 +00:00
|
|
|
|
|
|
|
private handleMemberIncrease = async (event: MemberIncreaseEvent) => {
|
2022-03-07 10:05:14 +00:00
|
|
|
if (event.user_id !== this.oicq.uin || await this.instance.forwardPairs.find(event.group)) return;
|
2022-03-02 06:07:21 +00:00
|
|
|
// 是新群并且是自己加入了
|
2022-03-05 12:23:52 +00:00
|
|
|
await this.configService.promptNewGroup(event.group);
|
2022-03-02 06:07:21 +00:00
|
|
|
};
|
2022-02-20 08:25:30 +00:00
|
|
|
}
|