from datetime import datetime, timedelta from typing import Union from app import app from pyrogram import filters from pyrogram.types import ( InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply, Message, ) from pyrogram.client import Client from classes.holo_user import HoloUser from modules import custom_filters from modules.utils import locale, should_quote from modules.database import col_sponsorships from convopyro import listen_message def is_none_or_cancel(message: Union[Message, None]) -> bool: if ( message is None or message.text is not None and message.text.lower() == "/cancel" ): return True return False @app.on_message( custom_filters.enabled_sponsorships & ~filters.scheduled & filters.command(["sponsorship"], prefixes=["/"]) & ~custom_filters.banned & (custom_filters.allowed | custom_filters.admin) ) async def cmd_sponsorship(app: Client, msg: Message): holo_user = HoloUser(msg.from_user) if holo_user.application_state()[0] == "fill": await msg.reply_text( locale("finish_application", "message", locale=msg.from_user), quote=should_quote(msg), ) return if holo_user.spoiler_state() is True: await msg.reply_text(locale("spoiler_in_progress", "message", locale=holo_user)) return existent = col_sponsorships.find_one( { "user": msg.from_user.id, "sponsorship.expires": {"$gt": datetime.now() - timedelta(days=1)}, } ) if existent is None: await msg.reply_text( locale("sponsorship_apply", "message", locale=msg.from_user), reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton( text=str( locale("sponsor_apply", "button", locale=msg.from_user) ), callback_data=f"sponsor_apply_{msg.from_user.id}", ) ] ] ), quote=should_quote(msg), ) return await msg.reply_text( f'You have an active membership for **{existent["sponsorship"]["streamer"]}**. Wanna resubmit it once more?', reply_markup=ReplyKeyboardMarkup( [["Yep, use old data"], ["Nope, refill it once more"]], resize_keyboard=True, one_time_keyboard=True, ), ) answer_decision = await listen_message(app, msg.chat.id) if is_none_or_cancel(answer_decision): return input_streamer = existent["sponsorship"]["streamer"] if answer_decision.text.lower() == "yep, use old data": await answer_decision.reply_text( "Okay, reusing the old data.\n\nUntil when is your sub?\n\nEnter the date as DD.MM.YYYY", reply_markup=ForceReply(placeholder="Expiry date as DD.MM.YYYY"), ) while True: answer_date = await listen_message(app, msg.chat.id) if is_none_or_cancel(answer_date): return try: input_dt = datetime.strptime(answer_date.text, "%d.%m.%Y") break except ValueError: await answer_date.reply_text( "Invalid date! Provide as DD.MM.YYYY", reply_markup=ForceReply(placeholder="Expiry date as DD.MM.YYYY"), ) continue while True: await answer_date.reply_text( "Alright. Now provide your proof **as a single screenshot**" ) answer_proof = await listen_message(app, msg.chat.id) if is_none_or_cancel(answer_proof): return if answer_proof.photo is None: await answer_proof.reply_text( "Please, provide proof **as a single screenshot**" ) continue input_proof = answer_proof.photo.file_id break await msg.reply_text( f'Almost done. Do you want to keep the label **{existent["sponsorship"]["label"]}** or you want to change it?', reply_markup=ReplyKeyboardMarkup( [["Keep the old one"], ["Set a new one instead"]], resize_keyboard=True, one_time_keyboard=True, ), ) while True: answer_label_decision = await listen_message(app, msg.chat.id) if is_none_or_cancel(answer_label_decision): return if answer_label_decision.text is None: await answer_label_decision.reply_text( "Please, choose a valid option.", reply_markup=ReplyKeyboardMarkup( [["Keep the old one"], ["Set a new one instead"]], resize_keyboard=True, one_time_keyboard=True, ), ) continue if answer_label_decision.text.lower() == "keep the old one": input_label = existent["sponsorship"]["label"] elif answer_label_decision.text.lower() == "set a new one instead": await answer_label_decision.reply_text( "Okay. Please provide a new label up to 16 characters long", reply_markup=ForceReply(placeholder="New label"), ) while True: answer_label = await listen_message(app, msg.chat.id) if is_none_or_cancel(answer_label_decision): return if answer_label.text is None: await answer_label.reply_text( "Please provide valid label", reply_markup=ForceReply(placeholder="New label"), ) continue elif len(answer_label.text) > 16: await answer_label.reply_text( "Please provide a label not longer than 16 characters long", reply_markup=ForceReply(placeholder="New label"), ) continue input_label = answer_label.text break await msg.reply_text( f"So we did it for streamer **{input_streamer}**, til {input_dt.strftime('%d.%m.%Y')}, proofed by `{input_proof}` and labeled as **{input_label}**.", reply_markup=ReplyKeyboardRemove(), ) return elif answer_decision.text.lower() == "nope, refill it once more": await msg.reply_text( locale("sponsorship_apply", "message", locale=msg.from_user), reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton( text=str( locale("sponsor_apply", "button", locale=msg.from_user) ), callback_data=f"sponsor_apply_{msg.from_user.id}", ) ] ] ), quote=should_quote(msg), ) return else: await answer_decision.reply_text( "Invalid option!", reply_markup=ReplyKeyboardRemove() ) return # else: # await msg.reply_text(locale("sponsorship_application_empty", "message"))