balabola-vk/src/handlers/balabola.ts

79 lines
2.6 KiB
TypeScript

import { Keyboard, MessageContext } from "vk-io";
import { balabola } from "../balabola_api";
import { Composer } from "../composer";
import { is, isHasText } from "../filters";
import { throttledQueue } from "../utilities/throttledQueue";
export const composer = new Composer<MessageContext>();
const filter = composer
.filter(is(isHasText), composer.compose());
const throttledBalabola = throttledQueue(1, 1000);
const selectStyleKeyboard = <C extends MessageContext>(ctx: C) => {
return Keyboard.builder()
.textButton({ label: '1', payload: { command: 'пр 0 ' + ctx.$match[1] } })
.textButton({ label: '2', payload: { command: 'пр 24 ' + ctx.$match[1] } })
.textButton({ label: '3', payload: { command: 'пр 25 ' + ctx.$match[1] } })
.textButton({ label: '4', payload: { command: 'пр 11 ' + ctx.$match[1] } })
.row()
.textButton({ label: '5', payload: { command: 'пр 6 ' + ctx.$match[1] } })
.textButton({ label: '6', payload: { command: 'пр 8 ' + ctx.$match[1] } })
.textButton({ label: '7', payload: { command: 'пр 9 ' + ctx.$match[1] } })
.row()
.textButton({ label: 'чзх', payload: { command: 'help' } })
.inline();
};
const balabolaMessage = `Выбери стилизацию ответа:
1: без стилизации
2: инструкция
3: рецепт
4: мудрость
5: история
6: википедия
7: синопсис
более подробно команда help`;
const BALABOLA_INTROS = [0, 24, 25, 11, 6, 8, 9];
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)
});
});
filter.hear(/^(?:пр)\s(1|2|3|4|5|6|7)\s(.*)?$/i, async ctx => {
const intro = ctx.$match[1];
const query = ctx.$match[2];
await ctx.send('генерирую!!!');
const result = await throttledBalabola(() => balabola(query, BALABOLA_INTROS[+intro - 1]));
await ctx.send(result);
});
filter.use(async ctx => {
if (ctx.isChat) return;
await ctx.send('генерирую!!');
const result = await throttledBalabola(() => balabola(ctx.text!, 0));
await ctx.send(result);
});