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]; const BALABOLA_INTROS = [0, 24, 25, 11, 6, 8, 9];
filter.hear(/^(?:продолжи)\s(.*)?$/i, async ctx => { const spammers = new Map<number, number>();
if (ctx.isChat) return;
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, { await ctx.send(balabolaMessage, {
keyboard: selectStyleKeyboard(ctx) 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 => { filter.use(async ctx => {
if (ctx.isChat) return; if (ctx.isChat) return;
await ctx.send('генерирую!!'); await ctx.send('генерирую!!');
const result = await throttledBalabola(() => balabola(ctx.text!, 0)); const result = await throttledBalabola(() => balabola(ctx.text!, 0));
await ctx.send(result); await ctx.send(result);

View File

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