feat: 可以通过 /rm 撤回消息了

This commit is contained in:
Clansty 2022-03-05 17:58:07 +08:00
parent 5f5b76d670
commit 81be36d50a
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
3 changed files with 91 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import { BigInteger } from 'big-integer';
import { Api, TelegramClient, utils } from 'telegram';
import { ButtonLike, Entity, EntityLike } from 'telegram/define';
import { ButtonLike, Entity, EntityLike, MessageIDLike } from 'telegram/define';
import WaitForMessageHelper from '../helpers/WaitForMessageHelper';
import { SendMessageParams } from 'telegram/client/messages';
import { CustomFile } from 'telegram/client/uploads';
@ -14,6 +14,9 @@ export default class TelegramChat {
constructor(public readonly parent: Telegram,
private readonly client: TelegramClient,
// Api.Chat 是上限 200 人的普通群组
// 超级群组和频道都是 Api.Channel
// 有 Channel.broadcast 和 Channel.megagroup 标识
public readonly entity: Entity,
private readonly waitForInputHelper: WaitForMessageHelper) {
this.inputPeer = utils.getInputPeer(entity);
@ -124,4 +127,20 @@ export default class TelegramChat {
}),
);
}
public async getMember(user: EntityLike) {
return await this.client.invoke(
new Api.channels.GetParticipant({
channel: this.entity,
participant: user,
}),
);
}
public async deleteMessages(messageId: MessageIDLike | MessageIDLike[]) {
if (!Array.isArray(messageId)) {
messageId = [messageId];
}
return await this.client.deleteMessages(this.entity, messageId, { revoke: true });
}
}

View File

@ -52,7 +52,13 @@ export default class ForwardController {
private onTelegramMessage = async (message: Api.Message) => {
try {
const pair = forwardPairs.find(message.chat);
if (!pair) return;
if (!pair) return false;
// TODO: 可以做成 DeleteMessageController 之类
if (message.message?.startsWith('/rm')) {
// 撤回消息
await this.forwardService.handleTelegramMessageRm(message, pair);
return true;
}
const qqMessageSent = await this.forwardService.forwardFromTelegram(message, pair);
// 返回的信息不太够
if (qqMessageSent) {

View File

@ -357,17 +357,20 @@ export default class ForwardService {
});
if (messageInfo) {
try {
await pair.qq.recallMsg(messageInfo.seq, messageInfo.rand,
const success = await pair.qq.recallMsg(messageInfo.seq, messageInfo.rand,
pair.qq instanceof Friend ? messageInfo.time : messageInfo.pktnum);
if (!success) throw new Error();
}
catch (e) {
console.log(123);
const tipMsg = await pair.tg.sendMessage({
message: '撤回 QQ 中对应的消息失败QQ Bot 需要是管理员' +
message: '撤回 QQ 中对应的消息失败' +
(config.workMode === 'group' ? 'QQ Bot 需要是管理员' : '') +
(isOthersMsg ? ',而且无法撤回其他管理员的消息' : '') +
e.message,
(e.message ? '\n' + e.message : ''),
silent: true,
});
config.workMode === 'group' && setTimeout(async () => await tipMsg.delete(), 5000);
config.workMode === 'group' && setTimeout(async () => await tipMsg.delete({ revoke: true }), 5000);
}
}
}
@ -375,4 +378,61 @@ export default class ForwardService {
this.log.error('处理 Telegram 消息删除失败', e);
}
}
/**
* TG /rm
* @param message
* @param pair
*/
async handleTelegramMessageRm(message: Api.Message, pair: Pair) {
const replyMessage = await message.getReplyMessage();
if (replyMessage instanceof Api.Message) {
// 检查权限并撤回被回复的消息
let hasPermission = config.workMode === 'personal' || replyMessage.senderId?.eq(message.senderId);
if (!hasPermission && message.chat instanceof Api.Channel) {
// 可能是超级群
try {
const member = (await pair.tg.getMember(message.sender)).participant;
hasPermission = member instanceof Api.ChannelParticipantCreator ||
(member instanceof Api.ChannelParticipantAdmin && member.adminRights.deleteMessages);
}
catch (e) {
// 不管了
}
}
if (!hasPermission && message.chat instanceof Api.Chat) {
// 不是超级群,我也不知道怎么判断,而且应该用不到
}
if (hasPermission) {
// 双平台撤回被回复的消息
// 撤回 QQ 的
await this.telegramDeleteMessage(message.replyToMsgId, pair, replyMessage.senderId?.eq(this.tgBot.me.id));
try {
// 撤回 TG 的
await pair.tg.deleteMessages(message.replyToMsgId);
}
catch (e) {
await pair.tg.sendMessage(`删除消息失败:${e.message}`);
}
}
else {
const tipMsg = await pair.tg.sendMessage({
message: '不能撤回别人的消息',
silent: true,
});
setTimeout(async () => await tipMsg.delete({ revoke: true }), 5000);
}
}
// 撤回消息本身
try {
await message.delete({ revoke: true });
}
catch (e) {
const tipMsg = await message.reply({
message: 'Bot 目前无法撤回其他用户的消息Bot 需要「删除消息」权限',
silent: true,
});
setTimeout(async () => await tipMsg.delete({ revoke: true }), 5000);
}
}
}