Refactoring random item getters

This commit is contained in:
2022-01-02 13:15:59 +03:00
parent 24590b3d43
commit 41fe429bde
3 changed files with 26 additions and 43 deletions

View File

@@ -14,7 +14,7 @@ import { getBookCacheBuffer } from './services/book_cache_buffer';
import { download } from './services/downloader'; import { download } from './services/downloader';
import { createOrUpdateUserSettings } from './services/user_settings'; import { createOrUpdateUserSettings } from './services/user_settings';
import { formatBook, formatAuthor, formatSequence } from './format'; import { formatBook, formatAuthor, formatSequence } from './format';
import { getPaginatedMessage, registerPaginationCommand } from './utils'; import { getPaginatedMessage, registerPaginationCommand, registerRandomItemCallback } from './utils';
import { getRandomKeyboard } from './keyboard'; import { getRandomKeyboard } from './keyboard';
@@ -74,47 +74,9 @@ export async function createApprovedBot(token: string, state: BotState): Promise
}) })
}); });
bot.action(CallbackData.RANDOM_BOOK, async (ctx: Context) => { registerRandomItemCallback(bot, CallbackData.RANDOM_BOOK, BookLibrary.getRandomBook, formatBook);
const book = await BookLibrary.getRandomBook(); registerRandomItemCallback(bot, CallbackData.RANDOM_AUTHOR, BookLibrary.getRandomAuthor, formatAuthor);
registerRandomItemCallback(bot, CallbackData.RANDOM_SEQUENCE, BookLibrary.getRandomSequence, formatSequence);
const keyboard = Markup.inlineKeyboard([
[Markup.button.callback("Повторить?", CallbackData.RANDOM_BOOK)]
]);
ctx.editMessageReplyMarkup(undefined);
ctx.reply(formatBook(book), {
reply_markup: keyboard.reply_markup,
});
});
bot.action(CallbackData.RANDOM_AUTHOR, async (ctx: Context) => {
const author = await BookLibrary.getRandomAuthor();
const keyboard = Markup.inlineKeyboard([
[Markup.button.callback("Повторить?", CallbackData.RANDOM_AUTHOR)]
]);
ctx.editMessageReplyMarkup(undefined);
ctx.reply(formatAuthor(author), {
reply_markup: keyboard.reply_markup,
});
});
bot.action(CallbackData.RANDOM_SEQUENCE, async (ctx: Context) => {
const sequence = await BookLibrary.getRandomSequence();
const keyboard = Markup.inlineKeyboard([
[Markup.button.callback("Повторить?", CallbackData.RANDOM_SEQUENCE)]
]);
ctx.editMessageReplyMarkup(undefined);
ctx.reply(formatSequence(sequence), {
reply_markup: keyboard.reply_markup,
});
});
bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx: Context) => { bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx: Context) => {
if (!ctx.message || !('text' in ctx.message)) { if (!ctx.message || !('text' in ctx.message)) {

View File

@@ -1,4 +1,4 @@
import got, { HTTPError } from 'got'; import got from 'got';
import env from '@/config'; import env from '@/config';

View File

@@ -53,3 +53,24 @@ export function registerPaginationCommand<T>(
} }
}) })
} }
export function registerRandomItemCallback<T>(
bot: Telegraf,
callback_data: string,
itemGetter: () => Promise<T>,
itemFormatter: (item: T) => string,
) {
bot.action(callback_data, async (ctx: Context) => {
const item = await itemGetter();
const keyboard = Markup.inlineKeyboard([
[Markup.button.callback("Повторить?", callback_data)]
]);
ctx.editMessageReplyMarkup(undefined);
ctx.reply(itemFormatter(item), {
reply_markup: keyboard.reply_markup,
});
});
}