From 048e01c06e2a2ee2a23baf2688e3a2bf21f63867 Mon Sep 17 00:00:00 2001 From: thedragonsinn <98635854+thedragonsinn@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:56:20 +0530 Subject: [PATCH] bugfix [pm permit|guard]: use a custom filter for filterting permit chats. --- app/__main__.py | 2 +- app/plugins/tg_tools/pm_n_tag_logger.py | 18 ++++----- app/plugins/tg_tools/pm_permit.py | 50 ++++++++++++++----------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/app/__main__.py b/app/__main__.py index 2eb2a6c..6f60e9d 100644 --- a/app/__main__.py +++ b/app/__main__.py @@ -4,7 +4,7 @@ from app import LOGGER, Config, bot if Config.CMD_TRIGGER == Config.SUDO_TRIGGER: LOGGER.error("CMD_TRIGGER and SUDO_TRIGGER can't be the same") - sys.exit(0) + sys.exit(1) if __name__ == "__main__": diff --git a/app/plugins/tg_tools/pm_n_tag_logger.py b/app/plugins/tg_tools/pm_n_tag_logger.py index a5afa3e..3973239 100644 --- a/app/plugins/tg_tools/pm_n_tag_logger.py +++ b/app/plugins/tg_tools/pm_n_tag_logger.py @@ -11,7 +11,6 @@ from app import BOT, Config, CustomDB, Message, bot, extra_config LOGGER = CustomDB("COMMON_SETTINGS") MESSAGE_CACHE: dict[int, list[Message]] = defaultdict(list) - FLOOD_LIST: list[int] = [] @@ -58,7 +57,7 @@ async def logger_switch(bot: BOT, message: Message): ) -basic_filters = ( +BASIC_FILTERS = ( ~filters.channel & ~filters.bot & ~filters.service @@ -69,7 +68,7 @@ basic_filters = ( @bot.on_message( - filters=basic_filters + filters=BASIC_FILTERS & filters.private & filters.create(lambda _, __, ___: extra_config.PM_LOGGER), group=2, @@ -79,11 +78,11 @@ async def pm_logger(bot: BOT, message: Message): cache_message(message) -tag_filter = filters.create(lambda _, __, ___: extra_config.TAG_LOGGER) +TAG_FILTER = filters.create(lambda _, __, ___: extra_config.TAG_LOGGER) @bot.on_message( - filters=(basic_filters & filters.reply & tag_filter) & ~filters.private, + filters=(BASIC_FILTERS & filters.reply & TAG_FILTER) & ~filters.private, group=2, is_command=False, ) @@ -98,7 +97,7 @@ async def reply_logger(bot: BOT, message: Message): @bot.on_message( - filters=(basic_filters & filters.mentioned & tag_filter) & ~filters.private, + filters=(BASIC_FILTERS & filters.mentioned & TAG_FILTER) & ~filters.private, group=2, is_command=False, ) @@ -114,7 +113,7 @@ async def mention_logger(bot: BOT, message: Message): @bot.on_message( - filters=(basic_filters & (filters.text | filters.media) & tag_filter) + filters=(BASIC_FILTERS & (filters.text | filters.media) & TAG_FILTER) & ~filters.private, group=2, is_command=False, @@ -143,7 +142,6 @@ async def runner(): last_pm_logged_id = 0 while True: - cached_keys = list(MESSAGE_CACHE.keys()) if not cached_keys: await asyncio.sleep(5) @@ -151,22 +149,20 @@ async def runner(): first_key = cached_keys[0] cached_list = MESSAGE_CACHE.copy()[first_key] - if not cached_list: MESSAGE_CACHE.pop(first_key) for idx, msg in enumerate(cached_list): - if msg.chat.type == ChatType.PRIVATE: if last_pm_logged_id != first_key: last_pm_logged_id = first_key - log_info = True else: log_info = False coro = log_pm(message=msg, log_info=log_info) + else: coro = log_chat(message=msg) diff --git a/app/plugins/tg_tools/pm_permit.py b/app/plugins/tg_tools/pm_permit.py index 7b55868..7af0541 100644 --- a/app/plugins/tg_tools/pm_permit.py +++ b/app/plugins/tg_tools/pm_permit.py @@ -8,15 +8,9 @@ from ub_core.utils.helpers import get_name from app import BOT, CustomDB, Message, bot, extra_config PM_USERS = CustomDB("PM_USERS") - PM_GUARD = CustomDB("COMMON_SETTINGS") ALLOWED_USERS: list[int] = [] - -allowed_filter = filters.create(lambda _, __, m: m.chat.id in ALLOWED_USERS) - -guard_check = filters.create(lambda _, __, ___: extra_config.PM_GUARD) - RECENT_USERS: dict = defaultdict(int) @@ -26,14 +20,32 @@ async def init_task(): [ALLOWED_USERS.append(user_id["_id"]) async for user_id in PM_USERS.find()] -@bot.on_message( - (guard_check & filters.private & filters.incoming) - & (~allowed_filter & ~filters.bot) - & ~filters.chat(chats=[bot.me.id]) - & ~filters.service, - group=0, - is_command=False, -) +async def pm_permit_filter(_, __, message: Message): + # Return False if: + if ( + # PM_GUARD is False + not extra_config.PM_GUARD + # Chat is not Private + or message.chat.type != ChatType.PRIVATE + # Chat is already approved + or message.chat.id in ALLOWED_USERS + # Saved Messages + or message.chat.id == bot.me.id + # PM is BOT + or message.from_user.is_bot + # Telegram Service Messages like OTPs. + or message.from_user.is_support + # Chat Service Messages like pinned a pic etc + or message.service + ): + return False + return True + + +PERMIT_FILTER = filters.create(pm_permit_filter) + + +@bot.on_message(PERMIT_FILTER & filters.incoming, group=0) async def handle_new_pm(bot: BOT, message: Message): user_id = message.from_user.id if RECENT_USERS[user_id] == 0: @@ -59,13 +71,7 @@ async def handle_new_pm(bot: BOT, message: Message): await message.reply("You are not authorised to PM.") -@bot.on_message( - (guard_check & filters.private & filters.outgoing) - & (~allowed_filter & ~filters.bot) - & ~filters.chat(chats=[bot.me.id]), - group=2, - is_command=False, -) +@bot.on_message(PERMIT_FILTER & filters.outgoing, group=2) async def auto_approve(bot: BOT, message: Message): message = Message.parse(message=message) ALLOWED_USERS.append(message.chat.id) @@ -76,7 +82,7 @@ async def auto_approve(bot: BOT, message: Message): @bot.add_cmd(cmd="pmguard") -async def pmguard(bot: BOT, message: Message): +async def pm_guard(bot: BOT, message: Message): """ CMD: PMGUARD INFO: Enable/Disable PM GUARD.