UNPKG

grammy-guard

Version:
30 lines (29 loc) 986 B
export function not(predicate) { return (ctx) => Promise.resolve(predicate(ctx)).then((v) => !v); } export function and(...predicate) { return (ctx) => Promise.resolve(predicate.map((predicate) => predicate(ctx))).then((v) => v.every(Boolean)); } export function or(...predicate) { return (ctx) => Promise.resolve(predicate.map((predicate) => predicate(ctx))).then((v) => v.some(Boolean)); } export function reply(errorMessage, options) { const { replyToMessage, } = options ?? { replyToMessage: false, }; return async (ctx) => { const text = typeof errorMessage === "function" ? await errorMessage(ctx) : errorMessage; if (ctx.callbackQuery) { return await ctx.answerCallbackQuery({ text, }); } return await ctx.reply(text, { ...(replyToMessage && { reply_to_message_id: ctx.msg?.message_id, }), }); }; }