UNPKG

@grammyjs/chat-members

Version:
42 lines (41 loc) 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hydrateChatMember = hydrateChatMember; const mod_js_1 = require("./mod.js"); /** * Api transformer that adds a convenient `is` method to the objects returned by * `getChatMember`, `getChatAdministrators`, and `getAuthor`. * * Example: * ```typescript * const bot = new Bot<HydrateChatMemberFlavor<Context>>(""); * bot.api.config.use(hydrateChatMember()); * * bot.on("message", async (ctx) => { * const author = await ctx.getAuthor(); * if (author.is("admin")) { * author.status; // "creator" | "administrator" * } * }); * ``` */ function hydrateChatMember() { function hydrate(chatMember) { Object.defineProperty(chatMember, "is", { value: (query) => (0, mod_js_1.chatMemberIs)(chatMember, query), }); } return async (prev, method, payload, signal) => { const res = await prev(method, payload, signal); if (!res.ok) { return res; } if (method === "getChatMember") { hydrate(res.result); } else if (method === "getChatAdministrators") { res.result.forEach(hydrate); } return res; }; }