UNPKG

grammy-guard

Version:
141 lines (140 loc) 5.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSenderChatId = exports.isChatId = exports.isUserId = exports.isGroup = exports.isPrivate = exports.isAdmin = exports.isUserFromReply = exports.isMyChatMemberStatus = exports.isChatMemberStatus = exports.isSenderChatHasUsername = exports.isSenderChatHasId = exports.isSenderChat = exports.isBotHasUsername = exports.isBotHasId = exports.isBot = exports.isUserHasUsername = exports.isUserHasId = exports.isUser = exports.isChatHasUsername = exports.isChatHasId = exports.isChat = exports.isChannel = exports.isGroupChat = exports.isSupergroup = exports.isBasicGroup = exports.isPrivateChat = void 0; const utils_js_1 = require("./utils.js"); /// Chat filters function isPrivateChat(ctx) { return ctx.hasChatType("private"); } exports.isPrivateChat = isPrivateChat; function isBasicGroup(ctx) { return ctx.hasChatType("group"); } exports.isBasicGroup = isBasicGroup; function isSupergroup(ctx) { return ctx.hasChatType("supergroup"); } exports.isSupergroup = isSupergroup; function isGroupChat(ctx) { return isBasicGroup(ctx) || isSupergroup(ctx); } exports.isGroupChat = isGroupChat; function isChannel(ctx) { return ctx.hasChatType("channel"); } exports.isChannel = isChannel; function isChat(ctx) { return typeof ctx.chat !== "undefined"; } exports.isChat = isChat; function isChatHasId(...id) { return (ctx) => isChat(ctx) && id.includes(ctx.chat.id); } exports.isChatHasId = isChatHasId; function isChatHasUsername(...username) { username = username.map(utils_js_1.normalizeUsername); return (ctx) => isChat(ctx) && (0, utils_js_1.isChatWithUsername)(ctx.chat) && typeof ctx.chat.username === "string" && username.includes((0, utils_js_1.normalizeUsername)(ctx.chat.username)); } exports.isChatHasUsername = isChatHasUsername; /// Peer filters // User function isUser(ctx) { return ctx.from?.is_bot === false; } exports.isUser = isUser; function isUserHasId(...id) { return (ctx) => isUser(ctx) && id.includes(ctx.from.id); } exports.isUserHasId = isUserHasId; function isUserHasUsername(...username) { username = username.map(utils_js_1.normalizeUsername); return (ctx) => isUser(ctx) && typeof ctx.from.username === "string" && username.includes((0, utils_js_1.normalizeUsername)(ctx.from.username)); } exports.isUserHasUsername = isUserHasUsername; // Bot function isBot(ctx) { return ctx.from?.is_bot === true; } exports.isBot = isBot; function isBotHasId(...id) { return (ctx) => isBot(ctx) && id.includes(ctx.from.id); } exports.isBotHasId = isBotHasId; function isBotHasUsername(...username) { username = username.map(utils_js_1.normalizeUsername); return (ctx) => isBot(ctx) && typeof ctx.from.username === "string" && username.includes((0, utils_js_1.normalizeUsername)(ctx.from.username)); } exports.isBotHasUsername = isBotHasUsername; // Sender chat function isSenderChat(ctx) { return typeof ctx.senderChat !== "undefined"; } exports.isSenderChat = isSenderChat; function isSenderChatHasId(...id) { return (ctx) => isSenderChat(ctx) && id.includes(ctx.senderChat.id); } exports.isSenderChatHasId = isSenderChatHasId; function isSenderChatHasUsername(...username) { username = username.map(utils_js_1.normalizeUsername); return (ctx) => isSenderChat(ctx) && (0, utils_js_1.isChatWithUsername)(ctx.senderChat) && typeof ctx.senderChat.username === "string" && username.includes((0, utils_js_1.normalizeUsername)(ctx.senderChat.username)); } exports.isSenderChatHasUsername = isSenderChatHasUsername; /// Chat member status function isChatMemberStatus(status) { return (ctx) => ctx.chatMember?.new_chat_member.status === status; } exports.isChatMemberStatus = isChatMemberStatus; function isMyChatMemberStatus(status) { return (ctx) => ctx.myChatMember?.new_chat_member.status === status; } exports.isMyChatMemberStatus = isMyChatMemberStatus; /// Miscellaneous function isUserFromReply(ctx) { return ctx.msg?.from?.id === ctx.msg?.reply_to_message?.from?.id; } exports.isUserFromReply = isUserFromReply; async function isAdmin(ctx) { if (ctx.from?.username === "GroupAnonymousBot") { return true; } if (ctx.chat && ["channel", "private"].includes(ctx.chat.type)) { return true; } if (ctx.from) { const member = await ctx.getChatMember(ctx.from?.id); if (["creator", "administrator"].includes(member.status)) { return true; } } return false; } exports.isAdmin = isAdmin; // Deprecated. For backward compatibility // TODO: Remove in 1.0 /** * @deprecated The filter should not be used. Use `isPrivateChat` instead. */ exports.isPrivate = isPrivateChat; /** * @deprecated The filter should not be used. Use `isGroupChat` instead. */ exports.isGroup = isGroupChat; /** * @deprecated The filter should not be used. Use `isUserHasId` instead. */ exports.isUserId = isUserHasId; /** * @deprecated The filter should not be used. Use `isChatHasId` instead. */ exports.isChatId = isChatHasId; /** * @deprecated The filter should not be used. Use `isSenderChatHasId` instead. */ exports.isSenderChatId = isSenderChatHasId;