From 4541c84eb9d2cbc9b69d46fbaa8a4ff5c63a3aa8 Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 14 Dec 2022 13:58:06 +0100 Subject: [PATCH] Optimized for DB/OOP update --- modules/commands/reapply.py | 15 ++-- modules/handlers/confirmation.py | 124 +++++++++++++++---------------- modules/handlers/everything.py | 101 +++++++++++++------------ modules/handlers/welcome.py | 4 +- 4 files changed, 122 insertions(+), 122 deletions(-) diff --git a/modules/commands/reapply.py b/modules/commands/reapply.py index b17fa4c..bb81eca 100644 --- a/modules/commands/reapply.py +++ b/modules/commands/reapply.py @@ -1,22 +1,25 @@ from app import app from pyrogram import filters from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton -from modules.utils import configGet, configSet, locale +from classes.holo_user import HoloUser +from modules.utils import configGet, locale from modules.handlers.welcome import welcome_pass +from modules.database import col_tmp # Reapply command ============================================================================================================== @app.on_message(~ filters.scheduled & filters.private & filters.command(["reapply"], prefixes=["/"])) async def cmd_reapply(app, msg): - if configGet("approved", file=str(msg.from_user.id)) or configGet("refused", file=str(msg.from_user.id)): - if (configGet("stage", file=str(msg.from_user.id)) == 10) and not (configGet("sent", file=str(msg.from_user.id))): + holo_user = HoloUser(msg.from_user) + + if holo_user.application_state()[0] in ["approved", "rejected"]: + if (holo_user.application_state()[1] is True) and (not col_tmp.find_one({"user": holo_user.id, "type": "application"})["sent"]): left_chat = True async for member in app.get_chat_members(configGet("destination_group")): if member.user.id == msg.from_user.id: left_chat = False if not left_chat: - configSet(["reapply"], True, file=str(msg.from_user.id)) - configSet(["confirmed"], False, file=str(msg.from_user.id)) + holo_user.application_restart() await welcome_pass(app, msg, once_again=True) else: await msg.reply_text(locale("reapply_left_chat", "message"), reply_markup=InlineKeyboardMarkup([ @@ -34,7 +37,7 @@ async def cmd_reapply(app, msg): ] ])) else: - if configGet("sent", file=str(msg.from_user.id)): + if (holo_user.application_state()[0] == "fill") and (col_tmp.find_one({"user": holo_user.id, "type": "application"})["sent"] is True): await msg.reply_text(locale("reapply_forbidden", "message")) else: await msg.reply_text(locale("reapply_in_progress", "message").format(locale("confirm", "keyboard")[1][0]), reply_markup=InlineKeyboardMarkup([ diff --git a/modules/handlers/confirmation.py b/modules/handlers/confirmation.py index 95620f4..bcf4738 100644 --- a/modules/handlers/confirmation.py +++ b/modules/handlers/confirmation.py @@ -1,99 +1,93 @@ -from os import sep -from time import time from dateutil.relativedelta import relativedelta from datetime import datetime from app import app from pyrogram import filters from pyrogram.types import ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton from pyrogram.enums.parse_mode import ParseMode -from modules.utils import configGet, configSet, jsonLoad, jsonSave, locale, logWrite +from classes.holo_user import HoloUser +from modules.utils import configGet, locale, logWrite from modules.handlers.welcome import welcome_pass +from modules.database import col_tmp # Confirmation ================================================================================================================= @app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("confirm", "keyboard")[0][0]))) async def confirm_yes(app, msg): - user_stage = configGet("stage", file=str(msg.from_user.id)) + holo_user = HoloUser(msg.from_user) - if user_stage == 10: + if (holo_user.application_state()[0] == "fill") and (holo_user.application_state()[1] is True): - if not configGet("sent", file=str(msg.from_user.id)): + await msg.reply_text(locale("application_sent", "message"), reply_markup=ReplyKeyboardRemove()) - await msg.reply_text(locale("application_sent", "message"), reply_markup=ReplyKeyboardRemove()) + tmp_application = col_tmp.find_one({"user": holo_user.id, "type": "application"}) - applications = jsonLoad(f"{configGet('data', 'locations')}{sep}applications.json") + if tmp_application is None: + logWrite(f"Application of {holo_user.id} is nowhere to be found.") + return - applications[str(msg.from_user.id)] = { - "approved": False, - "approved_by": None, - "approval_date": None, - "refused": False, - "refused_by": False, - "refusal_date": False, - "application_date": int(time()), - "application": configGet("application", file=str(msg.from_user.id)) - } + application_content = [] + i = 1 - jsonSave(applications, f"{configGet('data', 'locations')}{sep}applications.json") + for question in tmp_application['application']: - application_content = [] - i = 1 - - for question in configGet("application", file=str(msg.from_user.id)): - if i == 2: - age = relativedelta(datetime.now(), datetime.strptime(configGet('application', file=str(msg.from_user.id))['2'], '%d.%m.%Y')) - application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))['2']} ({age.years} р.)") + if i == 2: + age = relativedelta(datetime.now(), tmp_application['application']['2']) + application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {tmp_application['application']['2'].strftime('%d.%m.%Y')} ({age.years} р.)") + elif i == 3: + if tmp_application['application']['3']['countryCode'] == "UA": + application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {tmp_application['application']['3']['name']}") else: - application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))[question]}") - i += 1 - - if configGet("reapply", file=str(msg.from_user.id)): - await app.send_message(chat_id=configGet("admin_group"), text=(locale("reapply_got", "message")).format(str(msg.from_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( - [ - [ - InlineKeyboardButton(text=str(locale("reapply_yes", "button")), callback_data=f"reapply_yes_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=str(locale("reapply_no", "button")), callback_data=f"reapply_no_{msg.from_user.id}") - ] - ] - ) - ) + application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {tmp_application['application']['3']['name']} ({tmp_application['application']['3']['adminName1']}, {tmp_application['application']['3']['countryName']})") else: - await app.send_message(chat_id=configGet("admin_group"), text=(locale("application_got", "message")).format(str(msg.from_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( + application_content.append(f"{locale(f'question{i}', 'message', 'question_titles')} {tmp_application['application'][question]}") + + i += 1 + + if tmp_application["reapply"]: + await app.send_message(chat_id=configGet("admin_group"), text=(locale("reapply_got", "message")).format(str(holo_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( + [ [ - [ - InlineKeyboardButton(text=str(locale("sub_yes", "button")), callback_data=f"sub_yes_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=str(locale("sub_no", "button")), callback_data=f"sub_no_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=str(locale("sub_no_aggressive", "button")), callback_data=f"sub_no_aggresive_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=str(locale("sub_no_russian", "button")), callback_data=f"sub_no_russian_{msg.from_user.id}") - ] + InlineKeyboardButton(text=str(locale("reapply_yes", "button")), callback_data=f"reapply_yes_{holo_user.id}") + ], + [ + InlineKeyboardButton(text=str(locale("reapply_no", "button")), callback_data=f"reapply_no_{holo_user.id}") ] - ) + ] ) + ) + else: + await app.send_message(chat_id=configGet("admin_group"), text=(locale("application_got", "message")).format(str(holo_user.id), msg.from_user.first_name, msg.from_user.last_name, msg.from_user.username, "\n".join(application_content)), parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton(text=str(locale("sub_yes", "button")), callback_data=f"sub_yes_{holo_user.id}") + ], + [ + InlineKeyboardButton(text=str(locale("sub_no", "button")), callback_data=f"sub_no_{holo_user.id}") + ], + [ + InlineKeyboardButton(text=str(locale("sub_no_aggressive", "button")), callback_data=f"sub_no_aggresive_{holo_user.id}") + ], + [ + InlineKeyboardButton(text=str(locale("sub_no_russian", "button")), callback_data=f"sub_no_russian_{holo_user.id}") + ] + ] + ) + ) - logWrite(f"User {msg.from_user.id} sent his application and it will now be reviewed") + logWrite(f"User {holo_user.id} sent his application and it will now be reviewed") - configSet(["sent"], True, file=str(msg.from_user.id)) - configSet(["confirmed"], True, file=str(msg.from_user.id)) + col_tmp.update_one({"user": holo_user.id, "type": "application"}, {"$set": {"sent": True}}) + + # configSet(["sent"], True, file=str(holo_user.id)) + # configSet(["confirmed"], True, file=str(holo_user.id)) @app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("confirm", "keyboard")[1][0]))) async def confirm_no(app, msg): - user_stage = configGet("stage", file=str(msg.from_user.id)) + holo_user = HoloUser(msg.from_user) - if user_stage == 10: - jsonSave(jsonLoad(f"{configGet('data', 'locations')}{sep}user_default.json"), f"{configGet('data', 'locations')}{sep}users{sep}{msg.from_user.id}.json") - configSet(["telegram_id"], str(msg.from_user.username), file=str(msg.from_user.id)) - configSet(["telegram_name"], f"{msg.from_user.first_name} {msg.from_user.last_name}", file=str(msg.from_user.id)) - configSet(["telegram_phone"], str(msg.from_user.phone_number), file=str(msg.from_user.id)) - configSet(["telegram_locale"], str(msg.from_user.language_code), file=str(msg.from_user.id)) + if (holo_user.application_state()[0] == "fill") and (holo_user.application_state()[1] is True): + holo_user.application_restart() await welcome_pass(app, msg, once_again=True) logWrite(f"User {msg.from_user.id} restarted the application due to typo in it") # ============================================================================================================================== \ No newline at end of file diff --git a/modules/handlers/everything.py b/modules/handlers/everything.py index a66ecb5..0faf9ab 100644 --- a/modules/handlers/everything.py +++ b/modules/handlers/everything.py @@ -26,12 +26,13 @@ async def any_stage(app, msg): if msg.via_bot is None: - if (msg.reply_to_message != None) and (await message_involved(msg)): + holo_user = HoloUser(msg.from_user) + + if (msg.reply_to_message is not None) and (await message_involved(msg)): context = await message_context(msg) context_message = await app.get_messages(context[0], context[1]) - holo_user = HoloUser(msg.from_user) destination_user = HoloUser(context_message.from_user) await destination_user.message( @@ -51,63 +52,65 @@ async def any_stage(app, msg): return -# user_stage = configGet("stage", file=str(msg.from_user.id)) + await holo_user.application_next(msg.text, msg=msg) + + # user_stage = configGet("stage", file=str(msg.from_user.id)) -# if user_stage == 1: -# await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) -# logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") -# configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) -# configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) + # if user_stage == 1: + # await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) + # logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") + # configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) + # configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) -# elif user_stage == 2: + # elif user_stage == 2: -# try: + # try: -# configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) + # configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) -# input_dt = datetime.strptime(msg.text, "%d.%m.%Y") + # input_dt = datetime.strptime(msg.text, "%d.%m.%Y") -# if datetime.now() <= input_dt: -# logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to joking") -# await msg.reply_text(locale("question2_joke", "message"), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply")))) + # if datetime.now() <= input_dt: + # logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to joking") + # await msg.reply_text(locale("question2_joke", "message"), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply")))) -# elif ((datetime.now() - input_dt).days) < ((datetime.now() - datetime.now().replace(year=datetime.now().year - configGet("age_allowed"))).days): -# logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to being underage") -# await msg.reply_text(locale("question2_underage", "message").format(str(configGet("age_allowed"))), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply")))) + # elif ((datetime.now() - input_dt).days) < ((datetime.now() - datetime.now().replace(year=datetime.now().year - configGet("age_allowed"))).days): + # logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to being underage") + # await msg.reply_text(locale("question2_underage", "message").format(str(configGet("age_allowed"))), reply_markup=ForceReply(placeholder=str(locale("question2", "force_reply")))) -# else: -# logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") -# await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) -# configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) + # else: + # logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") + # await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) + # configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) -# except ValueError: -# logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to sending invalid date format") -# await msg.reply_text(locale(f"question2_invalid", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage}", "force_reply")))) + # except ValueError: + # logWrite(f"User {msg.from_user.id} failed stage {user_stage} due to sending invalid date format") + # await msg.reply_text(locale(f"question2_invalid", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage}", "force_reply")))) -# else: -# if user_stage <= 9: -# logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") -# await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) -# configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) -# configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) -# else: -# if not configGet("sent", file=str(msg.from_user.id)): -# if not configGet("confirmed", file=str(msg.from_user.id)): -# configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) -# application_content = [] -# i = 1 -# for question in configGet("application", file=str(msg.from_user.id)): -# application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))[question]}") -# i += 1 -# await msg.reply_text(locale("confirm", "message").format("\n".join(application_content)), reply_markup=ReplyKeyboardMarkup(locale("confirm", "keyboard"), resize_keyboard=True)) -# #configSet("sent", True, file=str(msg.from_user.id)) -# #configSet("application_date", int(time()), file=str(msg.from_user.id)) -# else: -# if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)): -# await msg.reply_text(locale("already_sent", "message")) -# else: -# if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)): -# await msg.reply_text(locale("already_sent", "message")) + # else: + # if user_stage <= 9: + # logWrite(f"User {msg.from_user.id} completed stage {user_stage} of application") + # await msg.reply_text(locale(f"question{user_stage+1}", "message"), reply_markup=ForceReply(placeholder=str(locale(f"question{user_stage+1}", "force_reply")))) + # configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) + # configSet(["stage"], user_stage+1, file=str(msg.from_user.id)) + # else: + # if not configGet("sent", file=str(msg.from_user.id)): + # if not configGet("confirmed", file=str(msg.from_user.id)): + # configSet(["application", str(user_stage)], str(msg.text), file=str(msg.from_user.id)) + # application_content = [] + # i = 1 + # for question in configGet("application", file=str(msg.from_user.id)): + # application_content.append(f"{locale('question'+str(i), 'message', 'question_titles')} {configGet('application', file=str(msg.from_user.id))[question]}") + # i += 1 + # await msg.reply_text(locale("confirm", "message").format("\n".join(application_content)), reply_markup=ReplyKeyboardMarkup(locale("confirm", "keyboard"), resize_keyboard=True)) + # #configSet("sent", True, file=str(msg.from_user.id)) + # #configSet("application_date", int(time()), file=str(msg.from_user.id)) + # else: + # if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)): + # await msg.reply_text(locale("already_sent", "message")) + # else: + # if not configGet("approved", file=str(msg.from_user.id)) and not configGet("refused", file=str(msg.from_user.id)): + # await msg.reply_text(locale("already_sent", "message")) @app.on_message(~ filters.scheduled & filters.group) async def message_in_group(app, msg): diff --git a/modules/handlers/welcome.py b/modules/handlers/welcome.py index a484ac1..063138b 100644 --- a/modules/handlers/welcome.py +++ b/modules/handlers/welcome.py @@ -22,8 +22,8 @@ async def welcome_pass(app, msg, once_again: bool = True) -> None: logWrite(f"User {msg.from_user.id} confirmed starting the application") await msg.reply_text(locale("question1", "message"), reply_markup=ForceReply(placeholder=locale("question1", "force_reply"))) - configSet(["stage"], 1, file=str(msg.from_user.id)) - configSet(["sent"], False, file=str(msg.from_user.id)) + # configSet(["stage"], 1, file=str(msg.from_user.id)) + # configSet(["sent"], False, file=str(msg.from_user.id)) @app.on_message(~ filters.scheduled & filters.private & (filters.regex(locale("welcome", "keyboard")[1][0]))) async def welcome_reject(app, msg):