Q2TG/src/controllers/InChatCommandsController.ts

71 lines
2.6 KiB
TypeScript
Raw Normal View History

import InChatCommandsService from '../services/InChatCommandsService';
import { getLogger, Logger } from 'log4js';
import Instance from '../models/Instance';
import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import { Api } from 'telegram';
2022-09-02 03:41:24 +00:00
import { Group } from 'oicq';
export default class InChatCommandsController {
private readonly service: InChatCommandsService;
private readonly log: Logger;
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram,
private readonly oicq: OicqClient) {
this.log = getLogger(`InChatCommandsController - ${instance.id}`);
this.service = new InChatCommandsService(instance, tgBot, oicq);
tgBot.addNewMessageEventHandler(this.onTelegramMessage);
}
private onTelegramMessage = async (message: Api.Message) => {
if (!message.message) return;
const messageParts = message.message.split(' ');
if (!messageParts.length || !messageParts[0].startsWith('/')) return;
2022-09-02 03:41:24 +00:00
let command: string = messageParts.shift();
const params = messageParts.join(' ');
if (command.includes('@')) {
let target: string;
[command, target] = command.split('@');
if (target !== this.tgBot.me.username) return false;
}
const pair = this.instance.forwardPairs.find(message.chat);
if (!pair) return false;
switch (command) {
case '/info':
await this.service.info(message, pair);
return true;
2022-04-07 07:06:50 +00:00
case '/poke':
await this.service.poke(message, pair);
return true;
case '/forwardoff':
pair.enable = false;
await message.reply({ message: '转发已禁用' });
return true;
case '/forwardon':
pair.enable = true;
await message.reply({ message: '转发已启用' });
return true;
2022-03-16 14:51:44 +00:00
case '/refresh':
if (this.instance.workMode !== 'personal' || !message.senderId?.eq(this.instance.owner)) return false;
await pair.updateInfo();
await message.reply({ message: '<i>刷新成功</i>' });
2022-03-16 14:51:44 +00:00
return true;
2022-09-02 03:41:24 +00:00
case '/nick':
if (this.instance.workMode !== 'personal' || !message.senderId?.eq(this.instance.owner)) return false;
if (!(pair.qq instanceof Group)) return;
if (!params) {
await message.reply({
message: `群名片:<i>${pair.qq.pickMember(this.instance.qqUin, true).card}</i>`,
});
return true;
}
const result = await pair.qq.setCard(this.instance.qqUin, params);
await message.reply({
message: '设置' + (result ? '成功' : '失败'),
});
return true;
}
};
}