File size: 2,961 Bytes
21bc372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import asyncio
from box import Box
from datetime import datetime as dt
from datetime import timedelta
from pytz import timezone
from pyrogram.types import *
from akn.utils.database import db
from akn.utils.logger import LOGS

WHY_TRY_DO = """
Hey, {user}

Sorry, your bot {bot} has expired. 

You can upgrade to premium for an extended period:
- 1 month or 2 months of premium
- But don’t worry, you still get a free 7-day trial every week!

If you're interested, feel free to purchase premium at any time!
"""

reply_markup = InlineKeyboardMarkup(
    [
        [
            InlineKeyboardButton(
                text="🆕 New expiry",
                callback_data="expiredtt"
            )
        ]
    ]
)

async def add_time_watch(
    user_id,
    first_name,
    username,
    bot_token,
    client_name,
    days,
    disconnected: bool = False
):
    try:
        now = dt.now(timezone("Asia/Jakarta"))
        expire_date = now + timedelta(days=days)
        LOGS.info(f"Setting expiration time for user {user_id}: {expire_date}")
        await db.set_expired_date(
            user_id,
            first_name,
            username,
            expire_date,
            bot_token,
            client_name,
            disconnected
        )
    except Exception as e:
        LOGS.info(f"Error: {e}")

async def watch_do_time(user_id, client, assistant):
    while True:
        now = dt.now(timezone("Asia/Jakarta"))
        time = now.strftime("%d-%m-%Y")
        
        exp = await db.get_expired_date(user_id)
        exp_obj = Box(exp or {})
        if not exp_obj:
            await asyncio.sleep(30)
            continue
        expired_on = exp_obj.expire_date
        if not expired_on:
            await asyncio.sleep(30)
            continue
        if isinstance(expired_on, str):
            expired_on = dt.strptime(expired_on, "%d-%m-%Y")

        if expired_on.tzinfo is None:
            expired_on = timezone("Asia/Jakarta").localize(expired_on)

        LOGS.info(f"Expiration date for user {user_id}: {expired_on.strftime('%d-%m-%Y')}")
        
        if now >= expired_on:
            send_mention = f"<a href='tg://user?id={exp_obj.user_id}'>{exp_obj.first_name}</a>"
            await client.stop()
            await assistant.send_message(
                exp_obj.user_id,
                WHY_TRY_DO.format(
                    user=send_mention,
                    bot=exp_obj.username
                )
            )
            await db.set_expired_date(
                user_id=exp_obj.user_id,
                first_name=exp_obj.first_name,
                username=exp_obj.username,
                expire_date=None,
                bot_token=exp_obj.user_client.bot_token,
                client_name=exp_obj.user_client.client_name,
                disconnected=True
            )
            LOGS.info(f"bot {exp_obj.username} has expired")
            break
        else:
            await asyncio.sleep(30)