UNPKG

grammy-guard

Version:
117 lines (116 loc) 3.82 kB
import { isChatWithUsername, normalizeUsername } from "./utils.js"; /// Chat filters export function isPrivateChat(ctx) { return ctx.hasChatType("private"); } export function isBasicGroup(ctx) { return ctx.hasChatType("group"); } export function isSupergroup(ctx) { return ctx.hasChatType("supergroup"); } export function isGroupChat(ctx) { return isBasicGroup(ctx) || isSupergroup(ctx); } export function isChannel(ctx) { return ctx.hasChatType("channel"); } export function isChat(ctx) { return typeof ctx.chat !== "undefined"; } export function isChatHasId(...id) { return (ctx) => isChat(ctx) && id.includes(ctx.chat.id); } export function isChatHasUsername(...username) { username = username.map(normalizeUsername); return (ctx) => isChat(ctx) && isChatWithUsername(ctx.chat) && typeof ctx.chat.username === "string" && username.includes(normalizeUsername(ctx.chat.username)); } /// Peer filters // User export function isUser(ctx) { return ctx.from?.is_bot === false; } export function isUserHasId(...id) { return (ctx) => isUser(ctx) && id.includes(ctx.from.id); } export function isUserHasUsername(...username) { username = username.map(normalizeUsername); return (ctx) => isUser(ctx) && typeof ctx.from.username === "string" && username.includes(normalizeUsername(ctx.from.username)); } // Bot export function isBot(ctx) { return ctx.from?.is_bot === true; } export function isBotHasId(...id) { return (ctx) => isBot(ctx) && id.includes(ctx.from.id); } export function isBotHasUsername(...username) { username = username.map(normalizeUsername); return (ctx) => isBot(ctx) && typeof ctx.from.username === "string" && username.includes(normalizeUsername(ctx.from.username)); } // Sender chat export function isSenderChat(ctx) { return typeof ctx.senderChat !== "undefined"; } export function isSenderChatHasId(...id) { return (ctx) => isSenderChat(ctx) && id.includes(ctx.senderChat.id); } export function isSenderChatHasUsername(...username) { username = username.map(normalizeUsername); return (ctx) => isSenderChat(ctx) && isChatWithUsername(ctx.senderChat) && typeof ctx.senderChat.username === "string" && username.includes(normalizeUsername(ctx.senderChat.username)); } /// Chat member status export function isChatMemberStatus(status) { return (ctx) => ctx.chatMember?.new_chat_member.status === status; } export function isMyChatMemberStatus(status) { return (ctx) => ctx.myChatMember?.new_chat_member.status === status; } /// Miscellaneous export function isUserFromReply(ctx) { return ctx.msg?.from?.id === ctx.msg?.reply_to_message?.from?.id; } export 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; } // Deprecated. For backward compatibility // TODO: Remove in 1.0 /** * @deprecated The filter should not be used. Use `isPrivateChat` instead. */ export const isPrivate = isPrivateChat; /** * @deprecated The filter should not be used. Use `isGroupChat` instead. */ export const isGroup = isGroupChat; /** * @deprecated The filter should not be used. Use `isUserHasId` instead. */ export const isUserId = isUserHasId; /** * @deprecated The filter should not be used. Use `isChatHasId` instead. */ export const isChatId = isChatHasId; /** * @deprecated The filter should not be used. Use `isSenderChatHasId` instead. */ export const isSenderChatId = isSenderChatHasId;