from traceback import print_exc from app import app from pyrogram import filters from pyrogram.types import Message from pyrogram.client import Client from classes.holo_user import HoloUser from modules import custom_filters from modules.logging import logWrite from modules.utils import configGet, locale, should_quote, find_location from modules.database import col_applications, col_users from classes.errors.geo import PlaceNotFoundError # Nearby command =============================================================================================================== @app.on_message(~ filters.scheduled & (filters.private | (filters.chat(configGet("admin_group")) | filters.chat(configGet("destination_group")))) & filters.command(["nearby"], prefixes=["/"]) & (custom_filters.allowed | custom_filters.admin)) async def cmd_nearby(app: Client, msg: Message): holo_user = HoloUser(msg.from_user) # Check if any place provided if len(msg.command) == 1: # Action if no place provided application = col_applications.find_one({"user": msg.from_user.id}) if application is None: await msg.reply_text(locale("nearby_user_empty", "message", locale=holo_user)) return location = application["application"]["3"]["loc"][0], application["application"]["3"]["loc"][1] else: # Find a place from input query logWrite(f"Looking for the location by query '{' '.join(msg.command[1:])}'") try: location_coordinates = find_location(" ".join(msg.command[1:])) location = float(location_coordinates["lng"]), float(location_coordinates["lat"]) except PlaceNotFoundError: # Place is not found await msg.reply_text(locale("nearby_invalid", "message", locale=holo_user), quote=should_quote(msg)) return except Exception as exp: # Error occurred while finding the place await msg.reply_text(locale("nearby_error", "message", locale=holo_user).format(exp, print_exc()), quote=should_quote(msg)) return # Find all users registered in the area provided output = [] applications_nearby = col_applications.find( {"application.3.loc": { "$nearSphere": {"$geometry": {"type": "Point", "coordinates": [location[0], location[1]]}, "$maxDistance": configGet("search_radius")*1000} } } ) # {"application": {"3": {"loc": {"$near": { "$geometry": { "type": "Point", "coordinates": location }, "$maxDistance": 30000 }} } } } ) for entry in applications_nearby: if not entry["user"] == msg.from_user.id: user = col_users.find_one( {"user": entry["user"]} ) if user is not None: if user["tg_username"] not in [None, "None", ""]: # Check if user has any name output.append(f'• {user["tg_name"]} (@{user["tg_username"]}):\n {entry["application"]["3"]["name"]}, {entry["application"]["3"]["adminName1"]}') else: output.append(f'• {user["tg_name"]}:\n {entry["application"]["3"]["name"]}, {entry["application"]["3"]["adminName1"]}') logWrite(f"{holo_user.id} tried to find someone nearby {location[1]} {location[0]} in the radius of {configGet('search_radius')} kilometers") # Check if any users found if len(output) > 0: await msg.reply_text(locale("nearby_result", "message", locale=holo_user).format("\n".join(output)), quote=should_quote(msg)) else: await msg.reply_text(locale("nearby_empty", "message", locale=holo_user), quote=should_quote(msg)) # if not path.exists(f"{configGet('data', 'locations')}{sep}sponsors{sep}{msg.from_user.id}.json"): # jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}sponsor_default.json"), f"{configGet('data', 'locations')}{sep}sponsors{sep}{msg.from_user.id}.json") # sponsor = jsonLoad(f"{configGet('data', 'locations')}{sep}sponsors{sep}{msg.from_user.id}.json") # if sponsor["approved"]: # if sponsor["expires"] is not None: # if datetime.strptime(sponsor["expires"], "%d.%m.%Y") > datetime.now(): # await msg.reply_text(f"You have an active sub til **{sponsor['expires']}**.") # else: # await msg.reply_text(f"Your sub expired {int((datetime.now()-datetime.strptime(sponsor['expires'], '%d.%m.%Y')).days)} days ago.") # elif sponsor["approved"]: # await msg.reply_text(f"Your sub expiration date is not valid.") # else: # await msg.reply_text(f"You have no active subscription.") # ==============================================================================================================================