Inline mode optimized for DB/OOP

This commit is contained in:
Profitroll 2022-12-16 13:19:50 +01:00
parent e61aa17a72
commit bc8cd8a8ce

View File

@ -1,12 +1,13 @@
from datetime import datetime from datetime import datetime
from os import path, sep from os import path, sep
from app import app, isAnAdmin
from pyrogram.types import InlineQueryResultArticle, InputTextMessageContent from pyrogram.types import InlineQueryResultArticle, InputTextMessageContent
from pyrogram.enums.chat_type import ChatType from pyrogram.enums.chat_type import ChatType
from pyrogram.enums.chat_members_filter import ChatMembersFilter from pyrogram.enums.chat_members_filter import ChatMembersFilter
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from classes.holo_user import HoloUser, UserInvalidError, UserNotFoundError
from app import app, isAnAdmin from modules.utils import configGet, locale
from modules.utils import configGet, jsonLoad, locale from modules.database import col_applications
@app.on_inline_query() @app.on_inline_query()
async def inline_answer(client, inline_query): async def inline_answer(client, inline_query):
@ -25,7 +26,23 @@ async def inline_answer(client, inline_query):
) )
return return
if (path.exists(f"{configGet('data', 'locations')}{sep}users{sep}{inline_query.from_user.id}.json") and jsonLoad(f"{configGet('data', 'locations')}{sep}users{sep}{inline_query.from_user.id}.json")["approved"]) or (await isAnAdmin(inline_query.from_user.id) is True): try:
holo_user = HoloUser(inline_query.from_user)
except (UserNotFoundError, UserInvalidError):
await inline_query.answer(
results=[
InlineQueryResultArticle(
title=locale("title", "inline", "forbidden"),
input_message_content=InputTextMessageContent(
locale("message_content", "inline", "forbidden")
),
description=locale("description", "inline", "forbidden")
)
]
)
return
if holo_user.application_approved() or (await isAnAdmin(holo_user.id) is True):
list_of_users = [] list_of_users = []
async for m in app.get_chat_members(configGet("destination_group"), limit=configGet("inline_preview_count"), filter=ChatMembersFilter.SEARCH, query=inline_query.query): async for m in app.get_chat_members(configGet("destination_group"), limit=configGet("inline_preview_count"), filter=ChatMembersFilter.SEARCH, query=inline_query.query):
@ -33,26 +50,30 @@ async def inline_answer(client, inline_query):
results = [] results = []
applications = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json")
for match in list_of_users: for match in list_of_users:
try: application = col_applications.find_one({"user": match.user.id})
application_content = []
i = 1 if application is None:
for question in applications[str(match.user.id)]["application"]: continue
if i == 2:
age = relativedelta(datetime.now(), datetime.strptime(applications[str(match.user.id)]['application']['2'], '%d.%m.%Y')) application_content = []
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {applications[str(match.user.id)]['application']['2']} ({age.years} р.)") i = 1
for question in application['application']:
if i == 2:
age = relativedelta(datetime.now(), application['application']['2'])
application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {application['application']['2'].strftime('%d.%m.%Y')} ({age.years} р.)")
elif i == 3:
if application['application']['3']['countryCode'] == "UA":
application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {application['application']['3']['name']}")
else: else:
application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {applications[str(match.user.id)]['application'][question]}") application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {application['application']['3']['name']} ({application['application']['3']['adminName1']}, {application['application']['3']['countryName']})")
i += 1 else:
except KeyError: application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {application['application'][question]}")
continue
except FileNotFoundError: i += 1
continue
except TypeError:
continue
if match.user.photo != None: if match.user.photo != None:
try: try:
@ -104,17 +125,3 @@ async def inline_answer(client, inline_query):
results=results, results=results,
cache_time=10 cache_time=10
) )
else:
await inline_query.answer(
results=[
InlineQueryResultArticle(
title=locale("title", "inline", "forbidden"),
input_message_content=InputTextMessageContent(
locale("message_content", "inline", "forbidden")
),
description=locale("description", "inline", "forbidden")
)
]
)
return