This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/modules/commands/nearby.py

122 lines
4.5 KiB
Python
Raw Normal View History

2023-02-07 12:35:47 +02:00
from os import path
2022-12-29 16:04:00 +02:00
from traceback import print_exc
2022-12-27 19:49:02 +02:00
from app import app
2022-12-15 16:00:38 +02:00
from pyrogram import filters
2022-12-27 14:36:54 +02:00
from pyrogram.types import Message
from pyrogram.client import Client
2022-12-27 19:49:02 +02:00
from classes.holo_user import HoloUser
from modules import custom_filters
2022-12-31 00:29:17 +02:00
from modules.logging import logWrite
2023-02-07 12:35:47 +02:00
from modules.utils import configGet, jsonLoad, locale, should_quote, find_location
2022-12-31 00:29:17 +02:00
from modules.database import col_applications, col_users
2022-12-28 19:56:13 +02:00
from classes.errors.geo import PlaceNotFoundError
2022-12-15 16:00:38 +02:00
2023-03-09 17:25:06 +02:00
2022-12-15 16:00:38 +02:00
# Nearby command ===============================================================================================================
2023-03-09 17:25:06 +02:00
@app.on_message(
custom_filters.enabled_applications
& ~filters.scheduled
& (
filters.private
| (
filters.chat(configGet("admin", "groups"))
| filters.chat(configGet("users", "groups"))
)
)
& filters.command(["nearby"], prefixes=["/"])
& (custom_filters.allowed | custom_filters.admin)
& ~custom_filters.banned
)
2022-12-27 14:36:54 +02:00
async def cmd_nearby(app: Client, msg: Message):
2022-12-28 19:56:13 +02:00
holo_user = HoloUser(msg.from_user)
2022-12-29 16:04:00 +02:00
# Check if any place provided
2023-03-09 17:25:06 +02:00
if len(msg.command) == 1: # Action if no place provided
2022-12-31 00:29:17 +02:00
application = col_applications.find_one({"user": msg.from_user.id})
2022-12-27 19:49:02 +02:00
if application is None:
2023-03-09 17:25:06 +02:00
await msg.reply_text(
locale("nearby_user_empty", "message", locale=holo_user)
)
2022-12-27 19:49:02 +02:00
return
2023-03-09 17:25:06 +02:00
location = (
application["application"]["3"]["location"][0],
application["application"]["3"]["location"][1],
)
else: # Find a place from input query
2022-12-31 00:29:17 +02:00
logWrite(f"Looking for the location by query '{' '.join(msg.command[1:])}'")
2022-12-28 19:56:13 +02:00
try:
2022-12-31 00:29:17 +02:00
location_coordinates = find_location(" ".join(msg.command[1:]))
2023-03-09 17:25:06 +02:00
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),
)
2022-12-28 19:56:13 +02:00
return
2023-03-09 17:25:06 +02:00
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),
)
2022-12-28 19:56:13 +02:00
return
2022-12-27 19:49:02 +02:00
2022-12-29 16:04:00 +02:00
# Find all users registered in the area provided
2022-12-27 19:49:02 +02:00
output = []
2023-03-09 17:25:06 +02:00
applications_nearby = col_applications.find(
{
"application.3.location": {
"$nearSphere": {
"$geometry": {
"type": "Point",
"coordinates": [location[0], location[1]],
},
"$maxDistance": configGet("search_radius") * 1000,
}
}
}
)
2022-12-27 19:49:02 +02:00
2022-12-31 00:29:17 +02:00
for entry in applications_nearby:
if not entry["user"] == msg.from_user.id:
2023-03-09 17:25:06 +02:00
user = col_users.find_one({"user": entry["user"]})
2022-12-31 00:29:17 +02:00
if user is not None:
2023-03-09 17:25:06 +02:00
if entry["user"] in jsonLoad(
path.join(configGet("cache", "locations"), "group_members")
):
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"]}'
)
2023-02-07 12:35:47 +02:00
else:
2023-03-09 17:25:06 +02:00
output.append(
f'• **{user["tg_name"]}**:\n - {entry["application"]["3"]["name"]}, {entry["application"]["3"]["adminName1"]}'
)
2022-12-31 00:29:17 +02:00
2023-03-09 17:25:06 +02:00
logWrite(
f"{holo_user.id} tried to find someone nearby {location[1]} {location[0]} in the radius of {configGet('search_radius')} kilometers"
)
2022-12-27 19:49:02 +02:00
2022-12-29 16:04:00 +02:00
# Check if any users found
if len(output) > 0:
2023-03-09 17:25:06 +02:00
await msg.reply_text(
locale("nearby_result", "message", locale=holo_user).format(
"\n".join(output)
),
quote=should_quote(msg),
)
2022-12-29 16:04:00 +02:00
else:
2023-03-09 17:25:06 +02:00
await msg.reply_text(
locale("nearby_empty", "message", locale=holo_user), quote=should_quote(msg)
)
# ==============================================================================================================================