From 57ff7b67652873137f1d6fcaa6a803f130498437 Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 30 Aug 2023 14:41:34 +0200 Subject: [PATCH] Context handling implemented --- classes/pyroclient.py | 1 + modules/custom_filters.py | 5 +++++ modules/search_name.py | 4 ++++ modules/search_nearby.py | 3 +++ plugins/commands/checkout.py | 7 ++++++- plugins/commands/help.py | 3 ++- plugins/commands/import.py | 8 ++++++-- plugins/commands/remove_commands.py | 3 ++- plugins/commands/set_offset.py | 5 ++++- plugins/commands/set_time.py | 5 ++++- plugins/commands/setup.py | 5 ++++- plugins/commands/shutdown.py | 4 +++- plugins/commands/start.py | 5 ++++- plugins/commands/toggle.py | 3 ++- plugins/commands/upcoming.py | 4 ++-- plugins/language.py | 3 ++- 16 files changed, 54 insertions(+), 14 deletions(-) diff --git a/classes/pyroclient.py b/classes/pyroclient.py index f4b5ebc..db580c8 100644 --- a/classes/pyroclient.py +++ b/classes/pyroclient.py @@ -18,6 +18,7 @@ class PyroClient(LibPyroClient): self.scheduler.add_job( remind, CronTrigger.from_crontab("* * * * *"), args=(self,) ) + self.contexts = [] async def start(self, **kwargs): await col_locations.create_index( diff --git a/modules/custom_filters.py b/modules/custom_filters.py index 37b019e..4b3c709 100644 --- a/modules/custom_filters.py +++ b/modules/custom_filters.py @@ -10,4 +10,9 @@ async def _owner_func(_, __: PyroClient, message: Message): return False if message.from_user is None else __.owner == message.from_user.id +async def _context_func(_, __: PyroClient, message: Message): + return message.from_user.id in __.contexts + + owner = filters.create(_owner_func) +context = filters.create(_context_func) diff --git a/modules/search_name.py b/modules/search_name.py index f569407..5fae746 100644 --- a/modules/search_name.py +++ b/modules/search_name.py @@ -22,7 +22,9 @@ async def search_name(app: PyroClient, message: Message) -> Union[Location, None ) while location is None: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( @@ -72,7 +74,9 @@ async def search_name(app: PyroClient, message: Message) -> Union[Location, None ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( diff --git a/modules/search_nearby.py b/modules/search_nearby.py index 1681995..32def59 100644 --- a/modules/search_nearby.py +++ b/modules/search_nearby.py @@ -43,7 +43,10 @@ async def search_nearby(app: PyroClient, message: Message) -> Union[Location, No ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) + location: Union[Location, None] = None if answer is None or answer.text == "/cancel": diff --git a/plugins/commands/checkout.py b/plugins/commands/checkout.py index e5d2528..20210c5 100644 --- a/plugins/commands/checkout.py +++ b/plugins/commands/checkout.py @@ -8,12 +8,13 @@ from pyrogram.types import Message, ReplyKeyboardRemove from ujson import dumps from classes.pyroclient import PyroClient +from modules import custom_filters logger = logging.getLogger(__name__) @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["checkout"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["checkout"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_checkout(app: PyroClient, message: Message): user = await app.find_user(message.from_user) @@ -42,7 +43,9 @@ async def command_checkout(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer_delete = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer_delete is None or answer_delete.text == "/cancel": await message.reply_text( @@ -84,7 +87,9 @@ async def command_checkout(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer_confirm = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer_confirm is None or answer_confirm.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/help.py b/plugins/commands/help.py index 6e211e9..f96f56d 100644 --- a/plugins/commands/help.py +++ b/plugins/commands/help.py @@ -2,10 +2,11 @@ from pyrogram import filters from pyrogram.types import Message from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["help"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["help"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_help(app: PyroClient, message: Message): user = await app.find_user(message.from_user) diff --git a/plugins/commands/import.py b/plugins/commands/import.py index df32cb2..64c38e1 100644 --- a/plugins/commands/import.py +++ b/plugins/commands/import.py @@ -12,18 +12,22 @@ from modules.database import col_entries @PyroClient.on_message( - ~filters.scheduled & filters.private & custom_filters.owner & filters.command(["import"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & custom_filters.owner & filters.command(["import"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_import(app: PyroClient, message: Message): user = await app.find_user(message.from_user) await message.reply_text( app._("import", "messages", locale=user.locale), - reply_markup=ForceReply(placeholder=""), + reply_markup=ForceReply( + placeholder=app._("import", "force_replies", locale=user.locale) + ), ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/remove_commands.py b/plugins/commands/remove_commands.py index e15ebd7..5541d3e 100644 --- a/plugins/commands/remove_commands.py +++ b/plugins/commands/remove_commands.py @@ -2,10 +2,11 @@ from pyrogram import filters from pyrogram.types import Message from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["remove_commands"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["remove_commands"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_remove_commands(app: PyroClient, message: Message): user = await app.find_user(message.from_user) diff --git a/plugins/commands/set_offset.py b/plugins/commands/set_offset.py index 042c32c..c0981de 100644 --- a/plugins/commands/set_offset.py +++ b/plugins/commands/set_offset.py @@ -6,12 +6,13 @@ from pyrogram import filters from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient +from modules import custom_filters logger = logging.getLogger(__name__) @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["set_offset"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["set_offset"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_set_offset(app: PyroClient, message: Message): user = await app.find_user(message.from_user) @@ -24,7 +25,9 @@ async def command_set_offset(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/set_time.py b/plugins/commands/set_time.py index b4c6e49..fbee4fb 100644 --- a/plugins/commands/set_time.py +++ b/plugins/commands/set_time.py @@ -6,12 +6,13 @@ from pyrogram import filters from pyrogram.types import ForceReply, Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient +from modules import custom_filters logger = logging.getLogger(__name__) @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["set_time"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["set_time"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_set_time(app: PyroClient, message: Message): user = await app.find_user(message.from_user) @@ -24,7 +25,9 @@ async def command_set_time(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/setup.py b/plugins/commands/setup.py index a82acf8..5bebffe 100644 --- a/plugins/commands/setup.py +++ b/plugins/commands/setup.py @@ -8,6 +8,7 @@ from pyrogram import filters from pyrogram.types import Message, ReplyKeyboardRemove from classes.pyroclient import PyroClient +from modules import custom_filters from modules.search_name import search_name from modules.search_nearby import search_nearby @@ -15,7 +16,7 @@ logger = logging.getLogger(__name__) @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["setup"] + i18n.sync.in_all_locales("start_configure", "buttons"), prefixes=["/", ""]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["setup"] + i18n.sync.in_all_locales("start_configure", "buttons"), prefixes=["/", ""]) & ~custom_filters.context # type: ignore ) async def command_setup(app: PyroClient, message: Message): user = await app.find_user(message.from_user) @@ -34,7 +35,9 @@ async def command_setup(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer_type = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer_type is None or answer_type.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/shutdown.py b/plugins/commands/shutdown.py index aa971c5..470241c 100644 --- a/plugins/commands/shutdown.py +++ b/plugins/commands/shutdown.py @@ -4,12 +4,14 @@ from pyrogram import filters from pyrogram.types import Message from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( ~filters.scheduled & filters.private - & filters.command(["shutdown", "reboot", "restart"], prefixes=["/"]) # type: ignore + & filters.command(["shutdown", "reboot", "restart"], prefixes=["/"]) + & ~custom_filters.context # type: ignore ) async def command_shutdown(app: PyroClient, msg: Message): if msg.from_user.id == app.owner: diff --git a/plugins/commands/start.py b/plugins/commands/start.py index cb5ee98..3df7e34 100644 --- a/plugins/commands/start.py +++ b/plugins/commands/start.py @@ -10,10 +10,11 @@ from pyrogram.types import ( ) from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["start"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_start(app: PyroClient, message: Message): user = await app.find_user(message.from_user) @@ -52,7 +53,9 @@ async def command_start(app: PyroClient, message: Message): ) while True: + app.contexts.append(message.from_user.id) answer = await listen_message(app, message.chat.id, 300) + app.contexts.remove(message.from_user.id) if answer is None or answer.text == "/cancel": await message.reply_text( diff --git a/plugins/commands/toggle.py b/plugins/commands/toggle.py index 91263e0..ac97f2b 100644 --- a/plugins/commands/toggle.py +++ b/plugins/commands/toggle.py @@ -4,10 +4,11 @@ from pyrogram import filters from pyrogram.types import Message from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["toggle"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["toggle"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_toggle(app: PyroClient, message: Message): user = await app.find_user(message.from_user) diff --git a/plugins/commands/upcoming.py b/plugins/commands/upcoming.py index ac29e63..d468b0d 100644 --- a/plugins/commands/upcoming.py +++ b/plugins/commands/upcoming.py @@ -1,17 +1,17 @@ from datetime import datetime, timedelta, timezone -from pymongo.errors import OperationFailure from pyrogram import filters from pyrogram.types import Message from pytz import timezone as pytz_timezone from classes.garbage_entry import GarbageEntry from classes.pyroclient import PyroClient +from modules import custom_filters from modules.database import col_entries @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["upcoming"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["upcoming"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_upcoming(app: PyroClient, message: Message): user = await app.find_user(message.from_user) diff --git a/plugins/language.py b/plugins/language.py index 673b72b..54ac366 100644 --- a/plugins/language.py +++ b/plugins/language.py @@ -6,10 +6,11 @@ from pyrogram.types import CallbackQuery, Message from classes.callbacks import CallbackLanguage from classes.pyroclient import PyroClient +from modules import custom_filters @PyroClient.on_message( - ~filters.scheduled & filters.private & filters.command(["language"], prefixes=["/"]) # type: ignore + ~filters.scheduled & filters.private & filters.command(["language"], prefixes=["/"]) & ~custom_filters.context # type: ignore ) async def command_language(app: PyroClient, message: Message): user = await app.find_user(message.from_user)