mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Refactoring random item getters
This commit is contained in:
@@ -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)) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import got, { HTTPError } from 'got';
|
import got from 'got';
|
||||||
|
|
||||||
import env from '@/config';
|
import env from '@/config';
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user