some fixes

This commit is contained in:
Timofey Gelazoniya 2022-10-21 09:58:42 +03:00
parent 257d80f0f6
commit 565645817d
Signed by: zeldon
GPG Key ID: 047886915281DD2A
2 changed files with 27 additions and 2 deletions

View File

@ -38,8 +38,23 @@ const balabolaMessage = `Выбери стилизацию ответа:
const BALABOLA_INTROS = [0, 24, 25, 11, 6, 8, 9];
filter.hear(/^(?:продолжи)\s(.*)?$/i, async ctx => {
if (ctx.isChat) return;
const spammers = new Map<number, number>();
filter.use((ctx, next) => {
if (spammers.has(ctx.senderId) && spammers.get(ctx.senderId)! > Date.now()) {
return ctx.reply('отказ', { disable_mentions: true });
}
spammers.set(ctx.senderId, Date.now() + 5000);
return next();
});
filter.hear(/^(?:продолжи)\s(.*)?$/i, async (ctx, next) => {
if (ctx.isChat) {
ctx.text = `пр 1 ${ctx.$match[1]}`;
return next();
}
await ctx.send(balabolaMessage, {
keyboard: selectStyleKeyboard(ctx)
});
@ -56,6 +71,7 @@ filter.hear(/^(?:пр)\s(1|2|3|4|5|6|7)\s(.*)?$/i, async ctx => {
filter.use(async ctx => {
if (ctx.isChat) return;
await ctx.send('генерирую!!');
const result = await throttledBalabola(() => balabola(ctx.text!, 0));
await ctx.send(result);

View File

@ -7,24 +7,30 @@ export function throttledQueue(
interval = interval / maxRequestsPerInterval;
maxRequestsPerInterval = 1;
}
const queue: Array<() => Promise<void>> = [];
let lastIntervalStart = 0;
let numRequestsPerInterval = 0;
let timeout: NodeJS.Timeout | undefined;
const dequeue = () => {
const intervalEnd = lastIntervalStart + interval;
const now = Date.now();
if (now < intervalEnd) {
timeout !== undefined && clearTimeout(timeout);
timeout = setTimeout(dequeue, intervalEnd - now);
return;
}
lastIntervalStart = now;
numRequestsPerInterval = 0;
for (const callback of queue.splice(0, maxRequestsPerInterval)) {
numRequestsPerInterval++;
void callback();
}
if (queue.length) {
timeout = setTimeout(dequeue, interval);
} else {
@ -35,11 +41,14 @@ export function throttledQueue(
return <Return = unknown>(fn: () => Promise<Return> | Return): Promise<Return> => new Promise<Return>(
(resolve, reject) => {
const callback = () => Promise.resolve().then(fn).then(resolve).catch(reject);
const now = Date.now();
if (timeout === undefined && (now - lastIntervalStart) > interval) {
lastIntervalStart = now;
numRequestsPerInterval = 0;
}
if (numRequestsPerInterval++ < maxRequestsPerInterval) {
void callback();
} else {