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/reminder.py b/modules/reminder.py index b845303..cf2b939 100644 --- a/modules/reminder.py +++ b/modules/reminder.py @@ -39,7 +39,7 @@ async def remind(app: PyroClient) -> None: entries = await col_entries.find( { - "location": {"$in": location.id}, + "locations": location.id, "date": user_date.replace(hour=0, minute=0), } ).to_list() diff --git a/modules/search_name.py b/modules/search_name.py index 1f3caf7..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( @@ -48,7 +50,7 @@ async def search_name(app: PyroClient, message: Message) -> Union[Location, None locations = await col_locations.find(query).limit(6).to_list() - if len(locations) == 0: + if len(locations) == 0 or locations is None: await message.reply_text( app._("location_name_empty", "messages", locale=user.locale).format( cancel_notice=app._("cancel", "messages", locale=user.locale) @@ -61,6 +63,8 @@ async def search_name(app: PyroClient, message: Message) -> Union[Location, None ) continue + locations.reverse() + keyboard = ReplyKeyboard(resize_keyboard=True, row_width=2) keyboard.add(*[ReplyButton(db_record["name"]) for db_record in locations]) @@ -70,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 be94d8e..32def59 100644 --- a/modules/search_nearby.py +++ b/modules/search_nearby.py @@ -32,6 +32,8 @@ async def search_nearby(app: PyroClient, message: Message) -> Union[Location, No ) return await search_name(app, message) + locations.reverse() + keyboard = ReplyKeyboard(resize_keyboard=True, row_width=2) keyboard.add(*[ReplyButton(db_record["name"]) for db_record in locations]) @@ -41,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 262e53b..d468b0d 100644 --- a/plugins/commands/upcoming.py +++ b/plugins/commands/upcoming.py @@ -6,11 +6,12 @@ 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) @@ -37,7 +38,7 @@ async def command_upcoming(app: PyroClient, message: Message): await GarbageEntry.from_record(entry) async for entry in col_entries.find( { - "location": {"$in": user.location.id}, + "locations": user.location.id, "date": {"$gte": date_min, "$lte": date_max}, } ) 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)