perf: 撤回消息时使用 consumer

This commit is contained in:
Clansty 2022-03-12 22:53:53 +08:00
parent 5f2c42545c
commit 5e824af1b8
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
2 changed files with 35 additions and 4 deletions

View File

@ -2,9 +2,10 @@ import Telegram from '../client/Telegram';
import { getLogger, Logger } from 'log4js';
import { Api } from 'telegram';
import db from '../models/db';
import { Friend, FriendRecallEvent, GroupRecallEvent } from 'oicq';
import { Friend, FriendRecallEvent, Group, GroupRecallEvent } from 'oicq';
import Instance from '../models/Instance';
import { Pair } from '../models/Pair';
import { consumer } from '../utils/highLevelFunces';
export default class DeleteMessageService {
private readonly log: Logger;
@ -14,6 +15,11 @@ export default class DeleteMessageService {
this.log = getLogger(`DeleteMessageService - ${instance.id}`);
}
// 500ms 内只撤回一条消息,防止频繁导致一部分消息没有成功撤回。不过这样的话,会得不到返回的结果
private recallQqMessage = consumer(async (qq: Friend | Group, seq: number, rand: number, timeOrPktnum: number) => {
await qq.recallMsg(seq, rand, timeOrPktnum);
}, 500);
/**
* QQ
* @param messageId
@ -34,12 +40,10 @@ export default class DeleteMessageService {
});
if (messageInfo) {
try {
const success = await pair.qq.recallMsg(messageInfo.seq, messageInfo.rand,
this.recallQqMessage(pair.qq, 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 中对应的消息失败' +
(this.instance.workMode === 'group' ? 'QQ Bot 需要是管理员' : '') +

View File

@ -0,0 +1,27 @@
export function debounce<TArgs extends any[], TRet>(fn: (...originArgs: TArgs) => TRet, dur = 100) {
let timer: NodeJS.Timeout;
return function (...args: TArgs) {
clearTimeout(timer);
timer = setTimeout(() => {
// @ts-ignore
fn.apply(this, args);
}, dur);
};
}
export function consumer<TArgs extends any[], TRet>(fn: (...originArgs: TArgs) => TRet, time = 100) {
let tasks = [], timer: NodeJS.Timeout;
return function (...args: TArgs) {
tasks.push(fn.bind(this, ...args));
if (timer == null) {
timer = setInterval(() => {
tasks.shift().call(this);
if (tasks.length <= 0) {
clearInterval(timer);
timer = null;
}
}, time);
}
};
}