gitscope
https://dev.kronoz.win/r/marzneshin.git
app/telegram/handlers/user.py · v0.3.0-alpha1
Source
1 a47eb4c Dawsh 2y ago from datetime import datetime
2 a47eb4c Dawsh 2y ago from app.db import GetDB, crud
3 a47eb4c Dawsh 2y ago from app.models.user import UserResponse
4 a47eb4c Dawsh 2y ago from app.telegram import bot
5 01cdf2e Dawsh 2y ago
6 a47eb4c Dawsh 2y ago # from pytz import UTC
7 a47eb4c Dawsh 2y ago from telebot.custom_filters import ChatFilter
8 a47eb4c Dawsh 2y ago from telebot.util import extract_arguments
9 a47eb4c Dawsh 2y ago
10 a47eb4c Dawsh 2y ago from app.utils.system import readable_size
11 a47eb4c Dawsh 2y ago
12 a47eb4c Dawsh 2y ago bot.add_custom_filter(ChatFilter())
13 a47eb4c Dawsh 2y ago
14 a47eb4c Dawsh 2y ago
15 01cdf2e Dawsh 2y ago @bot.message_handler(commands=["usage"])
16 a47eb4c Dawsh 2y ago def usage_command(message):
17 a47eb4c Dawsh 2y ago username = extract_arguments(message.text)
18 a47eb4c Dawsh 2y ago if not username:
19 01cdf2e Dawsh 2y ago return bot.reply_to(
20 01cdf2e Dawsh 2y ago message, "Usage: `/usage <username>`", parse_mode="MarkdownV2"
21 01cdf2e Dawsh 2y ago )
22 a47eb4c Dawsh 2y ago
23 a47eb4c Dawsh 2y ago with GetDB() as db:
24 a47eb4c Dawsh 2y ago dbuser = crud.get_user(db, username)
25 a47eb4c Dawsh 2y ago
26 a47eb4c Dawsh 2y ago if not dbuser:
27 a47eb4c Dawsh 2y ago return bot.reply_to(message, "No user found with this username")
28 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(dbuser)
29 a47eb4c Dawsh 2y ago
30 01cdf2e Dawsh 2y ago statuses = {"active": "✅", "expired": "🕰", "limited": "📵", "disabled": "❌"}
31 a47eb4c Dawsh 2y ago
32 01cdf2e Dawsh 2y ago text = f"""\
33 a47eb4c Dawsh 2y ago ┌─{statuses[user.status]} <b>Status:</b> <code>{user.status.title()}</code>
34 a47eb4c Dawsh 2y ago │ └─<b>Username:</b> <code>{user.username}</code>
35 a47eb4c Dawsh 2y ago │
36 a47eb4c Dawsh 2y ago ├─🔋 <b>Data limit:</b> <code>{readable_size(user.data_limit) if user.data_limit else 'Unlimited'}</code>
37 a47eb4c Dawsh 2y ago │ └─<b>Data Used:</b> <code>{readable_size(user.used_traffic) if user.used_traffic else "-"}</code>
38 a47eb4c Dawsh 2y ago │
39 a47eb4c Dawsh 2y ago └─📅 <b>Expiry Date:</b> <code>{datetime.fromtimestamp(user.expire).date() if user.expire else 'Never'}</code>
40 01cdf2e Dawsh 2y ago └─<b>Days left:</b> <code>{(datetime.fromtimestamp(user.expire or 0) - datetime.now()).days if user.expire else '-'}</code>"""
41 a47eb4c Dawsh 2y ago
42 01cdf2e Dawsh 2y ago return bot.reply_to(message, text, parse_mode="HTML")