gitscope
https://dev.kronoz.win/r/marzneshin.git
app/telegram/handlers/admin.py · v0.3.0-alpha
Source
1 a47eb4c Dawsh 2y ago import io
2 a47eb4c Dawsh 2y ago import math
3 a47eb4c Dawsh 2y ago import re
4 a47eb4c Dawsh 2y ago import random
5 a47eb4c Dawsh 2y ago import string
6 a47eb4c Dawsh 2y ago import os
7 a47eb4c Dawsh 2y ago from datetime import datetime
8 a47eb4c Dawsh 2y ago
9 a47eb4c Dawsh 2y ago import qrcode
10 a47eb4c Dawsh 2y ago import sqlalchemy
11 a47eb4c Dawsh 2y ago from dateutil.relativedelta import relativedelta
12 a47eb4c Dawsh 2y ago from telebot import types
13 a47eb4c Dawsh 2y ago from telebot.util import user_link
14 a47eb4c Dawsh 2y ago from telebot.asyncio_handler_backends import State, StatesGroup
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago from app.db import GetDB, crud
17 01cdf2e Dawsh 2y ago from app.models.user import (
18 01cdf2e Dawsh 2y ago UserCreate,
19 01cdf2e Dawsh 2y ago UserModify,
20 01cdf2e Dawsh 2y ago UserResponse,
21 01cdf2e Dawsh 2y ago UserStatus,
22 01cdf2e Dawsh 2y ago UserStatusModify,
23 01cdf2e Dawsh 2y ago )
24 a47eb4c Dawsh 2y ago from app.models.proxy import ProxyTypes
25 a47eb4c Dawsh 2y ago from app.telegram import bot
26 01cdf2e Dawsh 2y ago from app.telegram.utils.custom_filters import query_equals, query_startswith
27 a47eb4c Dawsh 2y ago from app.telegram.utils.keyboard import BotKeyboard
28 a47eb4c Dawsh 2y ago from app.utils.store import MemoryStorage
29 a47eb4c Dawsh 2y ago from app.utils.system import cpu_usage, memory_usage, readable_size
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago try:
32 a47eb4c Dawsh 2y ago from app.utils.system import realtime_bandwith as realtime_bandwidth
33 a47eb4c Dawsh 2y ago except ImportError:
34 a47eb4c Dawsh 2y ago from app.utils.system import realtime_bandwidth
35 a47eb4c Dawsh 2y ago
36 a47eb4c Dawsh 2y ago from config import TELEGRAM_LOGGER_CHANNEL_ID, TELEGRAM_DEFAULT_VLESS_FLOW
37 a47eb4c Dawsh 2y ago
38 a47eb4c Dawsh 2y ago mem_store = MemoryStorage()
39 a47eb4c Dawsh 2y ago
40 01cdf2e Dawsh 2y ago
41 a47eb4c Dawsh 2y ago class botStates(StatesGroup):
42 a47eb4c Dawsh 2y ago start = State()
43 01cdf2e Dawsh 2y ago add_data = State() # statesgroup should contain states
44 a47eb4c Dawsh 2y ago add_time = State()
45 a47eb4c Dawsh 2y ago edit_user_data_limit = State()
46 a47eb4c Dawsh 2y ago edit_user_expire = State()
47 a47eb4c Dawsh 2y ago edit_note = State()
48 9bfa731 Dawsh 2y ago # add_user_from_template = State()
49 a47eb4c Dawsh 2y ago add_user_username = State()
50 a47eb4c Dawsh 2y ago add_user_data_limit = State()
51 a47eb4c Dawsh 2y ago add_user_expire = State()
52 a47eb4c Dawsh 2y ago
53 a47eb4c Dawsh 2y ago
54 a47eb4c Dawsh 2y ago def get_system_info():
55 a47eb4c Dawsh 2y ago mem = memory_usage()
56 a47eb4c Dawsh 2y ago cpu = cpu_usage()
57 a47eb4c Dawsh 2y ago with GetDB() as db:
58 a47eb4c Dawsh 2y ago bandwidth = crud.get_system_usage(db)
59 a47eb4c Dawsh 2y ago total_users = crud.get_users_count(db)
60 a47eb4c Dawsh 2y ago users_active = crud.get_users_count(db, UserStatus.active)
61 a47eb4c Dawsh 2y ago return """\
62 a47eb4c Dawsh 2y ago 🎛 *CPU Cores*: `{cpu_cores}`
63 a47eb4c Dawsh 2y ago 🖥 *CPU Usage*: `{cpu_percent}%`
64 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖
65 a47eb4c Dawsh 2y ago 📊 *Total Memory*: `{total_memory}`
66 a47eb4c Dawsh 2y ago 📈 *In Use Memory*: `{used_memory}`
67 a47eb4c Dawsh 2y ago 📉 *Free Memory*: `{free_memory}`
68 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖
69 a47eb4c Dawsh 2y ago ⬇️ *Download Usage*: `{down_bandwidth}`
70 a47eb4c Dawsh 2y ago ⬆️ *Upload Usage*: `{up_bandwidth}`
71 a47eb4c Dawsh 2y ago ↕️ *Total Usage*: `{total_bandwidth}`
72 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖
73 a47eb4c Dawsh 2y ago 👥 *Total Users*: `{total_users}`
74 a47eb4c Dawsh 2y ago 🟢 *Active Users*: `{active_users}`
75 a47eb4c Dawsh 2y ago 🔴 *Deactivate Users*: `{deactivate_users}`
76 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖
77 a47eb4c Dawsh 2y ago ⏫ *Upload Speed*: `{up_speed}`
78 a47eb4c Dawsh 2y ago ⏬ *Download Speed*: `{down_speed}`
79 a47eb4c Dawsh 2y ago """.format(
80 a47eb4c Dawsh 2y ago cpu_cores=cpu.cores,
81 a47eb4c Dawsh 2y ago cpu_percent=cpu.percent,
82 a47eb4c Dawsh 2y ago total_memory=readable_size(mem.total),
83 a47eb4c Dawsh 2y ago used_memory=readable_size(mem.used),
84 a47eb4c Dawsh 2y ago free_memory=readable_size(mem.free),
85 a47eb4c Dawsh 2y ago total_bandwidth=readable_size(bandwidth.uplink + bandwidth.downlink),
86 a47eb4c Dawsh 2y ago up_bandwidth=readable_size(bandwidth.uplink),
87 a47eb4c Dawsh 2y ago down_bandwidth=readable_size(bandwidth.downlink),
88 a47eb4c Dawsh 2y ago total_users=total_users,
89 a47eb4c Dawsh 2y ago active_users=users_active,
90 a47eb4c Dawsh 2y ago deactivate_users=total_users - users_active,
91 a47eb4c Dawsh 2y ago up_speed=readable_size(realtime_bandwidth().outgoing_bytes),
92 01cdf2e Dawsh 2y ago down_speed=readable_size(realtime_bandwidth().incoming_bytes),
93 a47eb4c Dawsh 2y ago )
94 a47eb4c Dawsh 2y ago
95 a47eb4c Dawsh 2y ago
96 a47eb4c Dawsh 2y ago def schedule_delete_message(chat_id, *message_ids: int) -> None:
97 a47eb4c Dawsh 2y ago messages: list[int] = mem_store.get(f"{chat_id}:messages_to_delete", [])
98 a47eb4c Dawsh 2y ago for mid in message_ids:
99 a47eb4c Dawsh 2y ago messages.append(mid)
100 a47eb4c Dawsh 2y ago mem_store.set(f"{chat_id}:messages_to_delete", messages)
101 a47eb4c Dawsh 2y ago
102 a47eb4c Dawsh 2y ago
103 a47eb4c Dawsh 2y ago async def cleanup_messages(chat_id: int) -> None:
104 a47eb4c Dawsh 2y ago messages: list[int] = mem_store.get(f"{chat_id}:messages_to_delete", [])
105 a47eb4c Dawsh 2y ago for message_id in messages:
106 01cdf2e Dawsh 2y ago try:
107 01cdf2e Dawsh 2y ago await bot.delete_message(chat_id, message_id)
108 01cdf2e Dawsh 2y ago except:
109 01cdf2e Dawsh 2y ago pass
110 a47eb4c Dawsh 2y ago mem_store.set(f"{chat_id}:messages_to_delete", [])
111 a47eb4c Dawsh 2y ago
112 a47eb4c Dawsh 2y ago
113 01cdf2e Dawsh 2y ago @bot.message_handler(commands=["start", "help"], is_admin=True)
114 a47eb4c Dawsh 2y ago async def help_command(message: types.Message):
115 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
116 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(message.chat.id)
117 a47eb4c Dawsh 2y ago await bot.delete_state(message.from_user.id, message.chat.id)
118 01cdf2e Dawsh 2y ago return await bot.reply_to(
119 01cdf2e Dawsh 2y ago message,
120 01cdf2e Dawsh 2y ago """
121 a47eb4c Dawsh 2y ago {user_link} Welcome to Marzban Telegram-Bot Admin Panel.
122 a47eb4c Dawsh 2y ago Here you can manage your users and proxies.
123 a47eb4c Dawsh 2y ago To get started, use the buttons below.
124 a47eb4c Dawsh 2y ago """.format(
125 01cdf2e Dawsh 2y ago user_link=user_link(message.from_user)
126 01cdf2e Dawsh 2y ago ),
127 01cdf2e Dawsh 2y ago parse_mode="html",
128 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
129 01cdf2e Dawsh 2y ago )
130 a47eb4c Dawsh 2y ago
131 a47eb4c Dawsh 2y ago
132 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("system"), is_admin=True)
133 a47eb4c Dawsh 2y ago async def system_command(call: types.CallbackQuery):
134 a47eb4c Dawsh 2y ago return await bot.edit_message_text(
135 a47eb4c Dawsh 2y ago get_system_info(),
136 a47eb4c Dawsh 2y ago call.message.chat.id,
137 a47eb4c Dawsh 2y ago call.message.message_id,
138 a47eb4c Dawsh 2y ago parse_mode="MarkdownV2",
139 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
140 a47eb4c Dawsh 2y ago )
141 a47eb4c Dawsh 2y ago
142 a47eb4c Dawsh 2y ago
143 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("restart"), is_admin=True)
144 a47eb4c Dawsh 2y ago async def restart_command(call: types.CallbackQuery):
145 a47eb4c Dawsh 2y ago await bot.edit_message_text(
146 01cdf2e Dawsh 2y ago "⚠️ Are you sure? This will restart Xray core.",
147 a47eb4c Dawsh 2y ago call.message.chat.id,
148 a47eb4c Dawsh 2y ago call.message.message_id,
149 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_restart"),
150 a47eb4c Dawsh 2y ago )
151 a47eb4c Dawsh 2y ago
152 a47eb4c Dawsh 2y ago
153 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("delete:"), is_admin=True)
154 a47eb4c Dawsh 2y ago async def delete_user_command(call: types.CallbackQuery):
155 01cdf2e Dawsh 2y ago username = call.data.split(":")[1]
156 a47eb4c Dawsh 2y ago await bot.edit_message_text(
157 01cdf2e Dawsh 2y ago f"⚠️ Are you sure? This will delete user `{username}`.",
158 a47eb4c Dawsh 2y ago call.message.chat.id,
159 a47eb4c Dawsh 2y ago call.message.message_id,
160 a47eb4c Dawsh 2y ago parse_mode="markdown",
161 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_delete", username=username),
162 a47eb4c Dawsh 2y ago )
163 a47eb4c Dawsh 2y ago
164 a47eb4c Dawsh 2y ago
165 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("suspend:"), is_admin=True)
166 a47eb4c Dawsh 2y ago async def suspend_user_command(call: types.CallbackQuery):
167 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
168 a47eb4c Dawsh 2y ago await bot.edit_message_text(
169 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will suspend user `{username}`.",
170 a47eb4c Dawsh 2y ago call.message.chat.id,
171 a47eb4c Dawsh 2y ago call.message.message_id,
172 a47eb4c Dawsh 2y ago parse_mode="markdown",
173 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_suspend", username=username),
174 a47eb4c Dawsh 2y ago )
175 a47eb4c Dawsh 2y ago
176 a47eb4c Dawsh 2y ago
177 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("activate:"), is_admin=True)
178 a47eb4c Dawsh 2y ago async def activate_user_command(call: types.CallbackQuery):
179 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
180 a47eb4c Dawsh 2y ago await bot.edit_message_text(
181 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will activate user `{username}`.",
182 a47eb4c Dawsh 2y ago call.message.chat.id,
183 a47eb4c Dawsh 2y ago call.message.message_id,
184 a47eb4c Dawsh 2y ago parse_mode="markdown",
185 a47eb4c Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(
186 01cdf2e Dawsh 2y ago action="do_activate", username=username
187 01cdf2e Dawsh 2y ago ),
188 a47eb4c Dawsh 2y ago )
189 a47eb4c Dawsh 2y ago
190 a47eb4c Dawsh 2y ago
191 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("reset_usage:"), is_admin=True)
192 a47eb4c Dawsh 2y ago async def reset_usage_user_command(call: types.CallbackQuery):
193 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
194 a47eb4c Dawsh 2y ago await bot.edit_message_text(
195 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will Reset Usage of user `{username}`.",
196 a47eb4c Dawsh 2y ago call.message.chat.id,
197 a47eb4c Dawsh 2y ago call.message.message_id,
198 a47eb4c Dawsh 2y ago parse_mode="markdown",
199 a47eb4c Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(
200 01cdf2e Dawsh 2y ago action="do_reset_usage", username=username
201 01cdf2e Dawsh 2y ago ),
202 a47eb4c Dawsh 2y ago )
203 a47eb4c Dawsh 2y ago
204 a47eb4c Dawsh 2y ago
205 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("edit_all"), is_admin=True)
206 a47eb4c Dawsh 2y ago async def edit_all_command(call: types.CallbackQuery):
207 a47eb4c Dawsh 2y ago with GetDB() as db:
208 a47eb4c Dawsh 2y ago total_users = crud.get_users_count(db)
209 a47eb4c Dawsh 2y ago active_users = crud.get_users_count(db, UserStatus.active)
210 a47eb4c Dawsh 2y ago disabled_users = crud.get_users_count(db, UserStatus.disabled)
211 a47eb4c Dawsh 2y ago expired_users = crud.get_users_count(db, UserStatus.expired)
212 a47eb4c Dawsh 2y ago limited_users = crud.get_users_count(db, UserStatus.limited)
213 01cdf2e Dawsh 2y ago text = f"""
214 a47eb4c Dawsh 2y ago 👥 *Total Users*: `{total_users}`
215 a47eb4c Dawsh 2y ago ✅ *Active Users*: `{active_users}`
216 a47eb4c Dawsh 2y ago ❌ *Disabled Users*: `{disabled_users}`
217 a47eb4c Dawsh 2y ago 🕰 *Expired Users*: `{expired_users}`
218 01cdf2e Dawsh 2y ago 🪫 *Limited Users*: `{limited_users}`"""
219 a47eb4c Dawsh 2y ago return await bot.edit_message_text(
220 a47eb4c Dawsh 2y ago text,
221 a47eb4c Dawsh 2y ago call.message.chat.id,
222 a47eb4c Dawsh 2y ago call.message.message_id,
223 a47eb4c Dawsh 2y ago parse_mode="markdown",
224 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.edit_all_menu(),
225 a47eb4c Dawsh 2y ago )
226 a47eb4c Dawsh 2y ago
227 a47eb4c Dawsh 2y ago
228 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("delete_expired"), is_admin=True)
229 a47eb4c Dawsh 2y ago async def delete_expired_command(call: types.CallbackQuery):
230 a47eb4c Dawsh 2y ago await bot.edit_message_text(
231 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will *DELETE All Expired Users*‼️",
232 a47eb4c Dawsh 2y ago call.message.chat.id,
233 a47eb4c Dawsh 2y ago call.message.message_id,
234 a47eb4c Dawsh 2y ago parse_mode="markdown",
235 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_delete_expired"),
236 01cdf2e Dawsh 2y ago )
237 a47eb4c Dawsh 2y ago
238 a47eb4c Dawsh 2y ago
239 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("delete_limited"), is_admin=True)
240 a47eb4c Dawsh 2y ago async def delete_limited_command(call: types.CallbackQuery):
241 a47eb4c Dawsh 2y ago await bot.edit_message_text(
242 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will *DELETE All Limited Users*‼️",
243 a47eb4c Dawsh 2y ago call.message.chat.id,
244 a47eb4c Dawsh 2y ago call.message.message_id,
245 a47eb4c Dawsh 2y ago parse_mode="markdown",
246 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_delete_limited"),
247 01cdf2e Dawsh 2y ago )
248 a47eb4c Dawsh 2y ago
249 a47eb4c Dawsh 2y ago
250 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("add_data"), is_admin=True)
251 a47eb4c Dawsh 2y ago async def add_data_command(call: types.CallbackQuery):
252 a47eb4c Dawsh 2y ago msg = await bot.edit_message_text(
253 a47eb4c Dawsh 2y ago f"🔋 Enter Data Limit to increase or decrease (GB):",
254 a47eb4c Dawsh 2y ago call.message.chat.id,
255 a47eb4c Dawsh 2y ago call.message.message_id,
256 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(),
257 01cdf2e Dawsh 2y ago )
258 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, call.message.id)
259 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.id)
260 a47eb4c Dawsh 2y ago # return bot.register_next_step_handler(call.message, add_data_step)
261 a47eb4c Dawsh 2y ago await bot.set_state(call.from_user.id, botStates.add_data, call.message.chat.id)
262 a47eb4c Dawsh 2y ago
263 a47eb4c Dawsh 2y ago
264 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.add_data)
265 a47eb4c Dawsh 2y ago async def add_data_step(message):
266 a47eb4c Dawsh 2y ago try:
267 a47eb4c Dawsh 2y ago data_limit = float(message.text)
268 a47eb4c Dawsh 2y ago if not data_limit:
269 a47eb4c Dawsh 2y ago raise ValueError
270 a47eb4c Dawsh 2y ago except ValueError:
271 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
272 01cdf2e Dawsh 2y ago message.chat.id, "❌ Data limit must be a number and not zero."
273 01cdf2e Dawsh 2y ago )
274 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
275 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_data_step)
276 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.message_id)
277 a47eb4c Dawsh 2y ago msg = await bot.send_message(
278 a47eb4c Dawsh 2y ago message.chat.id,
279 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? this will change Data limit of all users according to <b>"
280 a47eb4c Dawsh 2y ago f"{'+' if data_limit > 0 else '-'}{readable_size(abs(data_limit *1024*1024*1024))}</b>",
281 a47eb4c Dawsh 2y ago parse_mode="html",
282 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action("do_add_data", data_limit),
283 01cdf2e Dawsh 2y ago )
284 a47eb4c Dawsh 2y ago await bot.delete_state(message.from_user.id, message.chat.id)
285 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
286 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, msg.id)
287 a47eb4c Dawsh 2y ago
288 a47eb4c Dawsh 2y ago
289 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("add_time"), is_admin=True)
290 a47eb4c Dawsh 2y ago async def add_time_command(call: types.CallbackQuery):
291 a47eb4c Dawsh 2y ago msg = await bot.edit_message_text(
292 a47eb4c Dawsh 2y ago f"📅 Enter Days to increase or decrease expiry:",
293 a47eb4c Dawsh 2y ago call.message.chat.id,
294 a47eb4c Dawsh 2y ago call.message.message_id,
295 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(),
296 01cdf2e Dawsh 2y ago )
297 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, call.message.id)
298 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.id)
299 a47eb4c Dawsh 2y ago # return bot.register_next_step_handler(call.message, add_time_step)
300 a47eb4c Dawsh 2y ago await bot.set_state(call.from_user.id, botStates.add_time, call.message.chat.id)
301 a47eb4c Dawsh 2y ago
302 a47eb4c Dawsh 2y ago
303 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.add_time)
304 a47eb4c Dawsh 2y ago async def add_time_step(message):
305 a47eb4c Dawsh 2y ago try:
306 a47eb4c Dawsh 2y ago days = int(message.text)
307 a47eb4c Dawsh 2y ago if not days:
308 a47eb4c Dawsh 2y ago raise ValueError
309 a47eb4c Dawsh 2y ago except ValueError:
310 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
311 01cdf2e Dawsh 2y ago message.chat.id, "❌ Days must be as a number and not zero."
312 01cdf2e Dawsh 2y ago )
313 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
314 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_time_step)
315 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.message_id)
316 a47eb4c Dawsh 2y ago msg = await bot.send_message(
317 a47eb4c Dawsh 2y ago message.chat.id,
318 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? this will change Expiry Time of all users according to <b>{days} Days</b>",
319 a47eb4c Dawsh 2y ago parse_mode="html",
320 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action("do_add_time", days),
321 01cdf2e Dawsh 2y ago )
322 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
323 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, msg.id)
324 a47eb4c Dawsh 2y ago
325 a47eb4c Dawsh 2y ago
326 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("inbound"), is_admin=True)
327 a47eb4c Dawsh 2y ago async def inbound_command(call: types.CallbackQuery):
328 a47eb4c Dawsh 2y ago await bot.edit_message_text(
329 a47eb4c Dawsh 2y ago f"Select inbound to *{call.data[8:].title()}* from all users",
330 a47eb4c Dawsh 2y ago call.message.chat.id,
331 a47eb4c Dawsh 2y ago call.message.message_id,
332 a47eb4c Dawsh 2y ago parse_mode="markdown",
333 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inbounds_menu(call.data, xray.config.inbounds_by_tag),
334 01cdf2e Dawsh 2y ago )
335 a47eb4c Dawsh 2y ago
336 a47eb4c Dawsh 2y ago
337 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("confirm_inbound"), is_admin=True)
338 a47eb4c Dawsh 2y ago async def delete_expired_confirm_command(call: types.CallbackQuery):
339 a47eb4c Dawsh 2y ago await bot.edit_message_text(
340 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will *{call.data[16:].replace(':', ' ')} for All Users*‼️",
341 a47eb4c Dawsh 2y ago call.message.chat.id,
342 a47eb4c Dawsh 2y ago call.message.message_id,
343 a47eb4c Dawsh 2y ago parse_mode="markdown",
344 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action=call.data[8:]),
345 01cdf2e Dawsh 2y ago )
346 a47eb4c Dawsh 2y ago
347 a47eb4c Dawsh 2y ago
348 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("edit:"), is_admin=True)
349 a47eb4c Dawsh 2y ago async def edit_command(call: types.CallbackQuery):
350 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
351 a47eb4c Dawsh 2y ago await bot.delete_state(call.from_user.id, call.message.chat.id)
352 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
353 a47eb4c Dawsh 2y ago with GetDB() as db:
354 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
355 a47eb4c Dawsh 2y ago if not db_user:
356 a47eb4c Dawsh 2y ago return await bot.answer_callback_query(
357 01cdf2e Dawsh 2y ago call.id, "❌ User not found.", show_alert=True
358 a47eb4c Dawsh 2y ago )
359 9bfa731 Dawsh 2y ago services = crud.get_services(db)
360 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
361 01cdf2e Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:username", username)
362 01cdf2e Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:data_limit", db_user.data_limit)
363 01cdf2e Dawsh 2y ago mem_store.set(
364 01cdf2e Dawsh 2y ago f"{call.message.chat.id}:expire_date",
365 01cdf2e Dawsh 2y ago datetime.fromtimestamp(db_user.expire) if db_user.expire else None,
366 01cdf2e Dawsh 2y ago )
367 01cdf2e Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:services", [s.id for s in db_user.services])
368 a47eb4c Dawsh 2y ago await bot.edit_message_text(
369 a47eb4c Dawsh 2y ago f"📝 Editing user `{username}`",
370 a47eb4c Dawsh 2y ago call.message.chat.id,
371 a47eb4c Dawsh 2y ago call.message.message_id,
372 a47eb4c Dawsh 2y ago parse_mode="markdown",
373 9bfa731 Dawsh 2y ago reply_markup=BotKeyboard.select_services(
374 9bfa731 Dawsh 2y ago services,
375 9bfa731 Dawsh 2y ago [s.id for s in user.services],
376 a47eb4c Dawsh 2y ago "edit",
377 a47eb4c Dawsh 2y ago username=username,
378 a47eb4c Dawsh 2y ago data_limit=db_user.data_limit,
379 a47eb4c Dawsh 2y ago expire_date=mem_store.get(f"{call.message.chat.id}:expire_date"),
380 01cdf2e Dawsh 2y ago ),
381 a47eb4c Dawsh 2y ago )
382 a47eb4c Dawsh 2y ago
383 a47eb4c Dawsh 2y ago
384 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("help_edit"), is_admin=True)
385 a47eb4c Dawsh 2y ago async def help_edit_command(call: types.CallbackQuery):
386 a47eb4c Dawsh 2y ago await bot.answer_callback_query(
387 01cdf2e Dawsh 2y ago call.id, text="Press the (✏️ Edit) button to edit", show_alert=True
388 a47eb4c Dawsh 2y ago )
389 a47eb4c Dawsh 2y ago
390 a47eb4c Dawsh 2y ago
391 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("cancel"), is_admin=True)
392 a47eb4c Dawsh 2y ago async def cancel_command(call: types.CallbackQuery):
393 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
394 a47eb4c Dawsh 2y ago await bot.delete_state(call.from_user.id, call.message.chat.id)
395 a47eb4c Dawsh 2y ago return await bot.edit_message_text(
396 a47eb4c Dawsh 2y ago get_system_info(),
397 a47eb4c Dawsh 2y ago call.message.chat.id,
398 a47eb4c Dawsh 2y ago call.message.message_id,
399 a47eb4c Dawsh 2y ago parse_mode="MarkdownV2",
400 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
401 a47eb4c Dawsh 2y ago )
402 a47eb4c Dawsh 2y ago
403 a47eb4c Dawsh 2y ago
404 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("edit_user:"), is_admin=True)
405 a47eb4c Dawsh 2y ago async def edit_user_command(call: types.CallbackQuery):
406 a47eb4c Dawsh 2y ago _, username, action = call.data.split(":")
407 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, call.message.id)
408 a47eb4c Dawsh 2y ago await cleanup_messages(call.message.chat.id)
409 a47eb4c Dawsh 2y ago if action == "data":
410 a47eb4c Dawsh 2y ago msg = await bot.send_message(
411 a47eb4c Dawsh 2y ago call.message.chat.id,
412 01cdf2e Dawsh 2y ago "⬆️ Enter Data Limit (GB):\n⚠️ Send 0 for unlimited.",
413 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(f"user:{username}"),
414 a47eb4c Dawsh 2y ago )
415 a47eb4c Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:edit_msg_text", call.message.text)
416 a47eb4c Dawsh 2y ago await bot.delete_state(call.from_user.id, call.message.chat.id)
417 01cdf2e Dawsh 2y ago await bot.set_state(
418 01cdf2e Dawsh 2y ago call.from_user.id, botStates.edit_user_data_limit, call.message.chat.id
419 01cdf2e Dawsh 2y ago )
420 a47eb4c Dawsh 2y ago async with bot.retrieve_data(call.from_user.id, call.message.chat.id) as data:
421 01cdf2e Dawsh 2y ago data["username"] = username
422 01cdf2e Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
423 01cdf2e Dawsh 2y ago # bot.register_next_step_handler(
424 a47eb4c Dawsh 2y ago # call.message, edit_user_data_limit_step, username)
425 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.message_id)
426 a47eb4c Dawsh 2y ago elif action == "expire":
427 a47eb4c Dawsh 2y ago msg = await bot.send_message(
428 a47eb4c Dawsh 2y ago call.message.chat.id,
429 01cdf2e Dawsh 2y ago "⬆️ Enter Expire Date (YYYY-MM-DD)\nOr You Can Use Regex Symbol: ^[0-9]{1,3}(M|D) :\n⚠️ Send 0 for never expire.",
430 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(f"user:{username}"),
431 01cdf2e Dawsh 2y ago )
432 a47eb4c Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:edit_msg_text", call.message.text)
433 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
434 01cdf2e Dawsh 2y ago await bot.set_state(
435 01cdf2e Dawsh 2y ago call.from_user.id, botStates.edit_user_expire, call.message.chat.id
436 01cdf2e Dawsh 2y ago )
437 a47eb4c Dawsh 2y ago async with bot.retrieve_data(call.from_user.id, call.message.chat.id) as data:
438 01cdf2e Dawsh 2y ago data["username"] = username
439 01cdf2e Dawsh 2y ago # bot.register_next_step_handler(
440 a47eb4c Dawsh 2y ago # call.message, edit_user_expire_step, username=username)
441 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.message_id)
442 a47eb4c Dawsh 2y ago
443 a47eb4c Dawsh 2y ago
444 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.edit_user_data_limit)
445 a47eb4c Dawsh 2y ago async def edit_user_data_limit_step(message: types.Message):
446 a47eb4c Dawsh 2y ago try:
447 a47eb4c Dawsh 2y ago if float(message.text) < 0:
448 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
449 01cdf2e Dawsh 2y ago message.chat.id, "❌ Data limit must be greater or equal to 0."
450 01cdf2e Dawsh 2y ago )
451 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
452 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, edit_user_data_limit_step, username=username)
453 a47eb4c Dawsh 2y ago data_limit = float(message.text) * 1024**3
454 a47eb4c Dawsh 2y ago except ValueError:
455 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
456 01cdf2e Dawsh 2y ago message.chat.id, "❌ Data limit must be a number."
457 01cdf2e Dawsh 2y ago )
458 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
459 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, edit_user_data_limit_step, username=username)
460 01cdf2e Dawsh 2y ago mem_store.set(f"{message.chat.id}:data_limit", data_limit)
461 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.message_id)
462 a47eb4c Dawsh 2y ago text = mem_store.get(f"{message.chat.id}:edit_msg_text")
463 a47eb4c Dawsh 2y ago mem_store.delete(f"{message.chat.id}:edit_msg_text")
464 a47eb4c Dawsh 2y ago async with bot.retrieve_data(call.from_user.id, call.message.chat.id) as data:
465 a47eb4c Dawsh 2y ago username = data["username"]
466 a47eb4c Dawsh 2y ago del data["username"]
467 a47eb4c Dawsh 2y ago await bot.send_message(
468 a47eb4c Dawsh 2y ago message.chat.id,
469 a47eb4c Dawsh 2y ago text or f"📝 Editing user <code>{username}</code>",
470 a47eb4c Dawsh 2y ago parse_mode="html",
471 a47eb4c Dawsh 2y ago reply_markup=BotKeyboard.select_protocols(
472 01cdf2e Dawsh 2y ago mem_store.get(f"{message.chat.id}:protocols"),
473 01cdf2e Dawsh 2y ago "edit",
474 01cdf2e Dawsh 2y ago username=username,
475 01cdf2e Dawsh 2y ago data_limit=data_limit,
476 01cdf2e Dawsh 2y ago expire_date=mem_store.get(f"{message.chat.id}:expire_date"),
477 01cdf2e Dawsh 2y ago ),
478 01cdf2e Dawsh 2y ago )
479 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
480 a47eb4c Dawsh 2y ago
481 a47eb4c Dawsh 2y ago
482 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.edit_user_expire)
483 a47eb4c Dawsh 2y ago async def edit_user_expire_step(message: types.Message):
484 a47eb4c Dawsh 2y ago try:
485 a47eb4c Dawsh 2y ago now = datetime.now()
486 a47eb4c Dawsh 2y ago today = datetime(
487 01cdf2e Dawsh 2y ago year=now.year, month=now.month, day=now.day, hour=23, minute=59, second=59
488 a47eb4c Dawsh 2y ago )
489 01cdf2e Dawsh 2y ago if re.match(r"^[0-9]{1,3}(M|m|D|d)$", message.text):
490 a47eb4c Dawsh 2y ago expire_date = today
491 01cdf2e Dawsh 2y ago number_pattern = r"^[0-9]{1,3}"
492 a47eb4c Dawsh 2y ago number = int(re.findall(number_pattern, message.text)[0])
493 01cdf2e Dawsh 2y ago symbol_pattern = r"(M|m|D|d)$"
494 a47eb4c Dawsh 2y ago symbol = re.findall(symbol_pattern, message.text)[0].upper()
495 01cdf2e Dawsh 2y ago if symbol == "M":
496 a47eb4c Dawsh 2y ago expire_date = today + relativedelta(months=number)
497 01cdf2e Dawsh 2y ago elif symbol == "D":
498 a47eb4c Dawsh 2y ago expire_date = today + relativedelta(days=number)
499 01cdf2e Dawsh 2y ago elif message.text != "0":
500 a47eb4c Dawsh 2y ago expire_date = datetime.strptime(message.text, "%Y-%m-%d")
501 a47eb4c Dawsh 2y ago else:
502 a47eb4c Dawsh 2y ago expire_date = None
503 a47eb4c Dawsh 2y ago if expire_date and expire_date < today:
504 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
505 01cdf2e Dawsh 2y ago message.chat.id, "❌ Expire date must be greater than today."
506 01cdf2e Dawsh 2y ago )
507 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
508 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, edit_user_expire_step, username=username)
509 a47eb4c Dawsh 2y ago except ValueError:
510 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
511 01cdf2e Dawsh 2y ago message.chat.id,
512 01cdf2e Dawsh 2y ago "❌ Expire date must be in YYYY-MM-DD format.\nOr You Can Use Regex Symbol: ^[0-9]{1,3}(M|D)",
513 01cdf2e Dawsh 2y ago )
514 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.message_id)
515 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, edit_user_expire_step, username=username)
516 a47eb4c Dawsh 2y ago
517 01cdf2e Dawsh 2y ago mem_store.set(f"{message.chat.id}:expire_date", expire_date)
518 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.message_id)
519 a47eb4c Dawsh 2y ago text = mem_store.get(f"{message.chat.id}:edit_msg_text")
520 a47eb4c Dawsh 2y ago mem_store.delete(f"{message.chat.id}:edit_msg_text")
521 a47eb4c Dawsh 2y ago async with bot.retrieve_data(call.from_user.id, call.message.chat.id) as data:
522 a47eb4c Dawsh 2y ago username = data["username"]
523 a47eb4c Dawsh 2y ago del data["username"]
524 a47eb4c Dawsh 2y ago await bot.send_message(
525 a47eb4c Dawsh 2y ago message.chat.id,
526 a47eb4c Dawsh 2y ago text or f"📝 Editing user <code>{username}</code>",
527 a47eb4c Dawsh 2y ago parse_mode="html",
528 a47eb4c Dawsh 2y ago reply_markup=BotKeyboard.select_protocols(
529 01cdf2e Dawsh 2y ago mem_store.get(f"{message.chat.id}:protocols"),
530 01cdf2e Dawsh 2y ago "edit",
531 01cdf2e Dawsh 2y ago username=username,
532 01cdf2e Dawsh 2y ago data_limit=mem_store.get(f"{message.chat.id}:data_limit"),
533 01cdf2e Dawsh 2y ago expire_date=expire_date,
534 01cdf2e Dawsh 2y ago ),
535 01cdf2e Dawsh 2y ago )
536 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
537 a47eb4c Dawsh 2y ago
538 a47eb4c Dawsh 2y ago
539 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("users:"), is_admin=True)
540 a47eb4c Dawsh 2y ago async def users_command(call: types.CallbackQuery):
541 01cdf2e Dawsh 2y ago page = int(call.data.split(":")[1]) if len(call.data.split(":")) > 1 else 1
542 a47eb4c Dawsh 2y ago with GetDB() as db:
543 a47eb4c Dawsh 2y ago total_pages = math.ceil(crud.get_users_count(db) / 10)
544 01cdf2e Dawsh 2y ago users = crud.get_users(
545 01cdf2e Dawsh 2y ago db,
546 01cdf2e Dawsh 2y ago offset=(page - 1) * 10,
547 01cdf2e Dawsh 2y ago limit=10,
548 01cdf2e Dawsh 2y ago sort=[crud.UsersSortingOptions["-created_at"]],
549 01cdf2e Dawsh 2y ago )
550 a47eb4c Dawsh 2y ago text = """👥 Users: (Page {page}/{total_pages})
551 a47eb4c Dawsh 2y ago ✅ Active
552 a47eb4c Dawsh 2y ago ❌ Disabled
553 a47eb4c Dawsh 2y ago 🕰 Expired
554 01cdf2e Dawsh 2y ago 🪫 Limited""".format(
555 01cdf2e Dawsh 2y ago page=page, total_pages=total_pages
556 01cdf2e Dawsh 2y ago )
557 a47eb4c Dawsh 2y ago
558 a47eb4c Dawsh 2y ago await bot.edit_message_text(
559 a47eb4c Dawsh 2y ago text,
560 a47eb4c Dawsh 2y ago call.message.chat.id,
561 a47eb4c Dawsh 2y ago call.message.message_id,
562 a47eb4c Dawsh 2y ago parse_mode="HTML",
563 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_list(users, page, total_pages=total_pages),
564 a47eb4c Dawsh 2y ago )
565 a47eb4c Dawsh 2y ago
566 a47eb4c Dawsh 2y ago
567 a47eb4c Dawsh 2y ago def get_user_info_text(
568 01cdf2e Dawsh 2y ago status: str,
569 01cdf2e Dawsh 2y ago username: str,
570 01cdf2e Dawsh 2y ago sub_url: str,
571 01cdf2e Dawsh 2y ago data_limit: int = None,
572 01cdf2e Dawsh 2y ago usage: int = None,
573 01cdf2e Dawsh 2y ago expire: int = None,
574 01cdf2e Dawsh 2y ago note: str = None,
575 01cdf2e Dawsh 2y ago ) -> str:
576 01cdf2e Dawsh 2y ago statuses = {"active": "✅", "expired": "🕰", "limited": "🪫", "disabled": "❌"}
577 a47eb4c Dawsh 2y ago text = f"""\
578 a47eb4c Dawsh 2y ago ┌─{statuses[status]} <b>Status:</b> <code>{status.title()}</code>
579 a47eb4c Dawsh 2y ago │ └─<b>Username:</b> <code>{username}</code>
580 a47eb4c Dawsh 2y ago │
581 a47eb4c Dawsh 2y ago ├─🔋 <b>Data limit:</b> <code>{readable_size(data_limit) if data_limit else 'Unlimited'}</code>
582 a47eb4c Dawsh 2y ago │ └─<b>Data Used:</b> <code>{readable_size(usage) if usage else "-"}</code>
583 a47eb4c Dawsh 2y ago │
584 a47eb4c Dawsh 2y ago ├─📅 <b>Expiry Date:</b> <code>{datetime.fromtimestamp(expire).date() if expire else 'Never'}</code>
585 a47eb4c Dawsh 2y ago │ └─<b>Days left:</b> <code>{(datetime.fromtimestamp(expire or 0) - datetime.now()).days if expire else '-'}</code>
586 a47eb4c Dawsh 2y ago │
587 a47eb4c Dawsh 2y ago """
588 a47eb4c Dawsh 2y ago if note:
589 01cdf2e Dawsh 2y ago text += f"├─📝 <b>Note:</b> <code>{note}</code>\n│\n"
590 a47eb4c Dawsh 2y ago text += f'└─🚀 <b><a href="{sub_url}">Subscription</a>:</b> <code>{sub_url}</code>'
591 a47eb4c Dawsh 2y ago return text
592 a47eb4c Dawsh 2y ago
593 a47eb4c Dawsh 2y ago
594 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("edit_note:"), is_admin=True)
595 a47eb4c Dawsh 2y ago async def edit_note_command(call: types.CallbackQuery):
596 01cdf2e Dawsh 2y ago username = call.data.split(":")[1]
597 a47eb4c Dawsh 2y ago with GetDB() as db:
598 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
599 a47eb4c Dawsh 2y ago if not db_user:
600 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
601 01cdf2e Dawsh 2y ago call.id, "❌ User not found.", show_alert=True
602 01cdf2e Dawsh 2y ago )
603 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, call.message.id)
604 a47eb4c Dawsh 2y ago await cleanup_messages(call.message.chat.id)
605 a47eb4c Dawsh 2y ago msg = await bot.send_message(
606 a47eb4c Dawsh 2y ago call.message.chat.id,
607 01cdf2e Dawsh 2y ago f"<b>📝 Current Note:</b> <code>{db_user.note}</code>\n\nSend new Note for <code>{username}</code>",
608 a47eb4c Dawsh 2y ago parse_mode="HTML",
609 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(f"user:{username}"),
610 01cdf2e Dawsh 2y ago )
611 01cdf2e Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:username", username)
612 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.id)
613 a47eb4c Dawsh 2y ago await bot.set_state(call.from_user.id, botStates.edit_note, call.message.chat.id)
614 a47eb4c Dawsh 2y ago # bot.register_next_step_handler(msg, edit_note_step)
615 a47eb4c Dawsh 2y ago
616 01cdf2e Dawsh 2y ago
617 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.edit_note)
618 a47eb4c Dawsh 2y ago async def edit_note_step(message: types.Message):
619 01cdf2e Dawsh 2y ago note = message.text or ""
620 a47eb4c Dawsh 2y ago if len(note) > 500:
621 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
622 01cdf2e Dawsh 2y ago message.chat.id, "❌ Note can not be more than 500 characters."
623 01cdf2e Dawsh 2y ago )
624 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
625 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
626 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, edit_note_step)
627 01cdf2e Dawsh 2y ago username = mem_store.get(f"{message.chat.id}:username")
628 a47eb4c Dawsh 2y ago if not username:
629 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
630 01cdf2e Dawsh 2y ago await bot.reply_to(message, "❌ Something went wrong!\n restart bot /start")
631 a47eb4c Dawsh 2y ago with GetDB() as db:
632 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
633 a47eb4c Dawsh 2y ago last_note = db_user.note
634 01cdf2e Dawsh 2y ago db_user = crud.update_user(
635 01cdf2e Dawsh 2y ago db, db_user, UserModify(username=username, note=note)
636 01cdf2e Dawsh 2y ago )
637 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
638 a47eb4c Dawsh 2y ago text = get_user_info_text(
639 a47eb4c Dawsh 2y ago status=user.status,
640 a47eb4c Dawsh 2y ago username=user.username,
641 a47eb4c Dawsh 2y ago sub_url=user.subscription_url,
642 a47eb4c Dawsh 2y ago expire=user.expire,
643 a47eb4c Dawsh 2y ago data_limit=user.data_limit,
644 a47eb4c Dawsh 2y ago usage=user.used_traffic,
645 01cdf2e Dawsh 2y ago note=user.note,
646 01cdf2e Dawsh 2y ago )
647 01cdf2e Dawsh 2y ago await bot.reply_to(
648 01cdf2e Dawsh 2y ago message,
649 01cdf2e Dawsh 2y ago text,
650 01cdf2e Dawsh 2y ago parse_mode="html",
651 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
652 01cdf2e Dawsh 2y ago user_info={"status": user.status, "username": user.username}, note=True
653 01cdf2e Dawsh 2y ago ),
654 01cdf2e Dawsh 2y ago )
655 a47eb4c Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
656 01cdf2e Dawsh 2y ago text = f"""\
657 a47eb4c Dawsh 2y ago 📝 <b>#Edit_Note #From_Bot</b>
658 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
659 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{username}</code>
660 a47eb4c Dawsh 2y ago <b>Last Note :</b> <code>{last_note}</code>
661 a47eb4c Dawsh 2y ago <b>New Note :</b> <code>{note}</code>
662 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
663 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={message.chat.id}">{message.from_user.full_name}</a>"""
664 a47eb4c Dawsh 2y ago try:
665 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
666 a47eb4c Dawsh 2y ago except:
667 a47eb4c Dawsh 2y ago pass
668 a47eb4c Dawsh 2y ago
669 a47eb4c Dawsh 2y ago
670 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("user:"), is_admin=True)
671 a47eb4c Dawsh 2y ago async def user_command(call: types.CallbackQuery):
672 a47eb4c Dawsh 2y ago await bot.delete_state(call.from_user.id, call.message.chat.id)
673 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
674 01cdf2e Dawsh 2y ago username = call.data.split(":")[1]
675 01cdf2e Dawsh 2y ago page = int(call.data.split(":")[2]) if len(call.data.split(":")) > 2 else 1
676 a47eb4c Dawsh 2y ago with GetDB() as db:
677 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
678 a47eb4c Dawsh 2y ago if not db_user:
679 a47eb4c Dawsh 2y ago return await bot.answer_callback_query(
680 01cdf2e Dawsh 2y ago call.id, "❌ User not found.", show_alert=True
681 a47eb4c Dawsh 2y ago )
682 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
683 a47eb4c Dawsh 2y ago text = get_user_info_text(
684 01cdf2e Dawsh 2y ago status=user.status,
685 01cdf2e Dawsh 2y ago username=username,
686 01cdf2e Dawsh 2y ago sub_url=user.subscription_url,
687 01cdf2e Dawsh 2y ago data_limit=user.data_limit,
688 01cdf2e Dawsh 2y ago usage=user.used_traffic,
689 01cdf2e Dawsh 2y ago expire=user.expire,
690 01cdf2e Dawsh 2y ago note=user.note,
691 01cdf2e Dawsh 2y ago )
692 a47eb4c Dawsh 2y ago await bot.edit_message_text(
693 a47eb4c Dawsh 2y ago text,
694 01cdf2e Dawsh 2y ago call.message.chat.id,
695 01cdf2e Dawsh 2y ago call.message.message_id,
696 01cdf2e Dawsh 2y ago parse_mode="HTML",
697 a47eb4c Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
698 01cdf2e Dawsh 2y ago {"username": user.username, "status": user.status}, page=page, note=True
699 01cdf2e Dawsh 2y ago ),
700 01cdf2e Dawsh 2y ago )
701 a47eb4c Dawsh 2y ago
702 a47eb4c Dawsh 2y ago
703 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("revoke_sub:"), is_admin=True)
704 a47eb4c Dawsh 2y ago async def revoke_sub_command(call: types.CallbackQuery):
705 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
706 a47eb4c Dawsh 2y ago await bot.edit_message_text(
707 a47eb4c Dawsh 2y ago f"⚠️ Are you sure? This will *Revoke Subscription* link for `{username}`‼️",
708 a47eb4c Dawsh 2y ago call.message.chat.id,
709 a47eb4c Dawsh 2y ago call.message.message_id,
710 a47eb4c Dawsh 2y ago parse_mode="markdown",
711 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.confirm_action(action="do_revoke", username=username),
712 01cdf2e Dawsh 2y ago )
713 a47eb4c Dawsh 2y ago
714 a47eb4c Dawsh 2y ago
715 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("links:"), is_admin=True)
716 a47eb4c Dawsh 2y ago async def links_command(call: types.CallbackQuery):
717 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
718 a47eb4c Dawsh 2y ago
719 a47eb4c Dawsh 2y ago with GetDB() as db:
720 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
721 a47eb4c Dawsh 2y ago if not db_user:
722 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
723 01cdf2e Dawsh 2y ago call.id, "User not found!", show_alert=True
724 01cdf2e Dawsh 2y ago )
725 a47eb4c Dawsh 2y ago
726 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
727 a47eb4c Dawsh 2y ago
728 a47eb4c Dawsh 2y ago text = f"<code>{user.subscription_url}</code>\n\n\n"
729 a47eb4c Dawsh 2y ago for link in user.links:
730 a47eb4c Dawsh 2y ago text += f"<code>{link}</code>\n\n"
731 a47eb4c Dawsh 2y ago
732 a47eb4c Dawsh 2y ago await bot.edit_message_text(
733 a47eb4c Dawsh 2y ago text,
734 a47eb4c Dawsh 2y ago call.message.chat.id,
735 a47eb4c Dawsh 2y ago call.message.message_id,
736 a47eb4c Dawsh 2y ago parse_mode="HTML",
737 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.show_links(username),
738 a47eb4c Dawsh 2y ago )
739 a47eb4c Dawsh 2y ago
740 a47eb4c Dawsh 2y ago
741 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("genqr:"), is_admin=True)
742 a47eb4c Dawsh 2y ago async def genqr_command(call: types.CallbackQuery):
743 a47eb4c Dawsh 2y ago username = call.data.split(":")[1]
744 a47eb4c Dawsh 2y ago
745 a47eb4c Dawsh 2y ago with GetDB() as db:
746 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, username)
747 a47eb4c Dawsh 2y ago if not db_user:
748 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
749 01cdf2e Dawsh 2y ago call.id, "User not found!", show_alert=True
750 01cdf2e Dawsh 2y ago )
751 a47eb4c Dawsh 2y ago
752 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
753 a47eb4c Dawsh 2y ago
754 a47eb4c Dawsh 2y ago await bot.answer_callback_query(call.id, "Generating QR code...")
755 a47eb4c Dawsh 2y ago
756 a47eb4c Dawsh 2y ago for link in user.links:
757 a47eb4c Dawsh 2y ago f = io.BytesIO()
758 a47eb4c Dawsh 2y ago qr = qrcode.QRCode(border=6)
759 a47eb4c Dawsh 2y ago qr.add_data(link)
760 a47eb4c Dawsh 2y ago qr.make_image().save(f)
761 a47eb4c Dawsh 2y ago f.seek(0)
762 a47eb4c Dawsh 2y ago await bot.send_photo(
763 a47eb4c Dawsh 2y ago call.message.chat.id,
764 a47eb4c Dawsh 2y ago photo=f,
765 a47eb4c Dawsh 2y ago caption=f"<code>{link}</code>",
766 01cdf2e Dawsh 2y ago parse_mode="HTML",
767 a47eb4c Dawsh 2y ago )
768 a47eb4c Dawsh 2y ago with io.BytesIO() as f:
769 a47eb4c Dawsh 2y ago qr = qrcode.QRCode(border=6)
770 a47eb4c Dawsh 2y ago qr.add_data(user.subscription_url)
771 a47eb4c Dawsh 2y ago qr.make_image().save(f)
772 a47eb4c Dawsh 2y ago f.seek(0)
773 a47eb4c Dawsh 2y ago await bot.send_photo(
774 a47eb4c Dawsh 2y ago call.message.chat.id,
775 a47eb4c Dawsh 2y ago photo=f,
776 a47eb4c Dawsh 2y ago caption=get_user_info_text(
777 a47eb4c Dawsh 2y ago status=user.status,
778 a47eb4c Dawsh 2y ago username=user.username,
779 a47eb4c Dawsh 2y ago sub_url=user.subscription_url,
780 a47eb4c Dawsh 2y ago data_limit=user.data_limit,
781 a47eb4c Dawsh 2y ago usage=user.used_traffic,
782 01cdf2e Dawsh 2y ago expire=user.expire,
783 a47eb4c Dawsh 2y ago ),
784 a47eb4c Dawsh 2y ago parse_mode="HTML",
785 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.subscription_page(user.subscription_url),
786 a47eb4c Dawsh 2y ago )
787 a47eb4c Dawsh 2y ago try:
788 a47eb4c Dawsh 2y ago await bot.delete_message(call.message.chat.id, call.message.message_id)
789 a47eb4c Dawsh 2y ago except:
790 a47eb4c Dawsh 2y ago pass
791 a47eb4c Dawsh 2y ago
792 a47eb4c Dawsh 2y ago text = f"<code>{user.subscription_url}</code>\n\n\n"
793 a47eb4c Dawsh 2y ago for link in user.links:
794 a47eb4c Dawsh 2y ago text += f"<code>{link}</code>\n\n"
795 a47eb4c Dawsh 2y ago
796 a47eb4c Dawsh 2y ago await bot.send_message(
797 a47eb4c Dawsh 2y ago call.message.chat.id,
798 a47eb4c Dawsh 2y ago text,
799 a47eb4c Dawsh 2y ago "HTML",
800 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.show_links(username),
801 a47eb4c Dawsh 2y ago )
802 a47eb4c Dawsh 2y ago
803 01cdf2e Dawsh 2y ago
804 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("random"), is_admin=True)
805 a47eb4c Dawsh 2y ago async def random_username(call: types.CallbackQuery):
806 a47eb4c Dawsh 2y ago # bot.clear_step_handler_by_chat_id(call.message.chat.id)
807 a47eb4c Dawsh 2y ago await bot.delete_state(call.from_user.id, call.message.chat.id)
808 a47eb4c Dawsh 2y ago template_id = int(call.data.split(":")[1] or 0)
809 01cdf2e Dawsh 2y ago mem_store.delete(f"{call.message.chat.id}:template_id")
810 a47eb4c Dawsh 2y ago
811 a47eb4c Dawsh 2y ago characters = string.ascii_letters + string.digits
812 01cdf2e Dawsh 2y ago username = (
813 01cdf2e Dawsh 2y ago random.choice(characters)
814 01cdf2e Dawsh 2y ago + "".join(random.choices(characters, k=4))
815 01cdf2e Dawsh 2y ago + "_"
816 01cdf2e Dawsh 2y ago + "".join(random.choices(characters, k=4))
817 01cdf2e Dawsh 2y ago )
818 a47eb4c Dawsh 2y ago
819 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, call.message.id)
820 a47eb4c Dawsh 2y ago await cleanup_messages(call.message.chat.id)
821 a47eb4c Dawsh 2y ago
822 01cdf2e Dawsh 2y ago msg = await bot.send_message(
823 01cdf2e Dawsh 2y ago call.message.chat.id,
824 01cdf2e Dawsh 2y ago "⬆️ Enter Data Limit (GB):\n⚠️ Send 0 for unlimited.",
825 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(),
826 01cdf2e Dawsh 2y ago )
827 9bfa731 Dawsh 2y ago schedule_delete_message(call.message.chat.id, msg.id)
828 01cdf2e Dawsh 2y ago await bot.set_state(
829 01cdf2e Dawsh 2y ago call.from_user.id, botStates.add_user_data_limit, call.message.chat.id
830 01cdf2e Dawsh 2y ago )
831 a47eb4c Dawsh 2y ago async with bot.retrieve_data(call.from_user.id, call.message.chat.id) as data:
832 9bfa731 Dawsh 2y ago data["username"] = username
833 01cdf2e Dawsh 2y ago return
834 a47eb4c Dawsh 2y ago
835 a47eb4c Dawsh 2y ago
836 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_equals("add_user"), is_admin=True)
837 a47eb4c Dawsh 2y ago async def add_user_command(call: types.CallbackQuery):
838 a47eb4c Dawsh 2y ago try:
839 a47eb4c Dawsh 2y ago await bot.delete_message(call.message.chat.id, call.message.message_id)
840 a47eb4c Dawsh 2y ago except: # noqa
841 a47eb4c Dawsh 2y ago pass
842 a47eb4c Dawsh 2y ago username_msg = await bot.send_message(
843 a47eb4c Dawsh 2y ago call.message.chat.id,
844 01cdf2e Dawsh 2y ago "👤 Enter username:\n⚠️Username only can be 3 to 32 characters and contain a-z, A-Z 0-9, and underscores in "
845 01cdf2e Dawsh 2y ago "between.",
846 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.random_username(),
847 01cdf2e Dawsh 2y ago )
848 a47eb4c Dawsh 2y ago schedule_delete_message(call.message.chat.id, username_msg.id)
849 01cdf2e Dawsh 2y ago await bot.set_state(
850 01cdf2e Dawsh 2y ago call.from_user.id, botStates.add_user_username, call.message.chat.id
851 01cdf2e Dawsh 2y ago )
852 a47eb4c Dawsh 2y ago # bot.register_next_step_handler(username_msg, add_user_username_step)
853 a47eb4c Dawsh 2y ago
854 01cdf2e Dawsh 2y ago
855 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.add_user_username)
856 a47eb4c Dawsh 2y ago async def add_user_username_step(message: types.Message):
857 a47eb4c Dawsh 2y ago username = message.text
858 a47eb4c Dawsh 2y ago if not username:
859 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
860 01cdf2e Dawsh 2y ago message.chat.id, "❌ Username can not be empty."
861 01cdf2e Dawsh 2y ago )
862 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
863 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
864 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_username_step)
865 01cdf2e Dawsh 2y ago if not re.match(r"^(?!.*__)(?!.*_$)\w{2,31}[a-zA-Z\d]$", username):
866 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
867 01cdf2e Dawsh 2y ago message.chat.id,
868 01cdf2e Dawsh 2y ago "❌ Username only can be 3 to 32 characters and contain a-z, A-Z, 0-9, and underscores in between.",
869 01cdf2e Dawsh 2y ago )
870 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
871 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
872 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_username_step)
873 a47eb4c Dawsh 2y ago with GetDB() as db:
874 a47eb4c Dawsh 2y ago if crud.get_user(db, username):
875 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
876 01cdf2e Dawsh 2y ago message.chat.id, "❌ Username already exists."
877 01cdf2e Dawsh 2y ago )
878 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
879 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
880 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_username_step)
881 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
882 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
883 01cdf2e Dawsh 2y ago msg = await bot.send_message(
884 01cdf2e Dawsh 2y ago message.chat.id,
885 01cdf2e Dawsh 2y ago "⬆️ Enter Data Limit (GB):\n⚠️ Send 0 for unlimited.",
886 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(),
887 01cdf2e Dawsh 2y ago )
888 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, msg.id)
889 01cdf2e Dawsh 2y ago await bot.set_state(
890 01cdf2e Dawsh 2y ago message.from_user.id, botStates.add_user_data_limit, message.chat.id
891 01cdf2e Dawsh 2y ago )
892 a47eb4c Dawsh 2y ago async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
893 01cdf2e Dawsh 2y ago data["username"] = username
894 a47eb4c Dawsh 2y ago # bot.register_next_step_handler(msg, add_user_data_limit_step, username=username)
895 a47eb4c Dawsh 2y ago
896 a47eb4c Dawsh 2y ago
897 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.add_user_data_limit)
898 a47eb4c Dawsh 2y ago async def add_user_data_limit_step(message: types.Message):
899 a47eb4c Dawsh 2y ago try:
900 a47eb4c Dawsh 2y ago if float(message.text) < 0:
901 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
902 01cdf2e Dawsh 2y ago message.chat.id, "❌ Data limit must be greater or equal to 0."
903 01cdf2e Dawsh 2y ago )
904 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
905 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
906 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_data_limit_step, username=username)
907 a47eb4c Dawsh 2y ago data_limit = float(message.text) * 1024 * 1024 * 1024
908 a47eb4c Dawsh 2y ago except ValueError:
909 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
910 01cdf2e Dawsh 2y ago message.chat.id, "❌ Data limit must be a number."
911 01cdf2e Dawsh 2y ago )
912 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
913 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
914 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_data_limit_step, username=username)
915 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
916 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
917 01cdf2e Dawsh 2y ago msg = await bot.send_message(
918 01cdf2e Dawsh 2y ago message.chat.id,
919 01cdf2e Dawsh 2y ago "⬆️ Enter Expire Date (YYYY-MM-DD)\nOr You Can Use Regex Symbol: ^[0-9]{1,3}(M|D) :\n⚠️ Send 0 for never expire.",
920 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.inline_cancel_action(),
921 01cdf2e Dawsh 2y ago )
922 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, msg.id)
923 01cdf2e Dawsh 2y ago await bot.set_state(
924 01cdf2e Dawsh 2y ago message.from_user.id, botStates.add_user_expire, message.chat.id
925 01cdf2e Dawsh 2y ago )
926 a47eb4c Dawsh 2y ago async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
927 a47eb4c Dawsh 2y ago data["data_limit"] = data_limit
928 01cdf2e Dawsh 2y ago
929 a47eb4c Dawsh 2y ago # bot.register_next_step_handler(msg, add_user_expire_step, username=username, data_limit=data_limit)
930 a47eb4c Dawsh 2y ago
931 01cdf2e Dawsh 2y ago
932 a47eb4c Dawsh 2y ago @bot.message_handler(state=botStates.add_user_expire)
933 a47eb4c Dawsh 2y ago async def add_user_expire_step(message: types.Message):
934 a47eb4c Dawsh 2y ago try:
935 a47eb4c Dawsh 2y ago now = datetime.now()
936 a47eb4c Dawsh 2y ago today = datetime(
937 01cdf2e Dawsh 2y ago year=now.year, month=now.month, day=now.day, hour=23, minute=59, second=59
938 a47eb4c Dawsh 2y ago )
939 01cdf2e Dawsh 2y ago if re.match(r"^[0-9]{1,3}(M|m|D|d)$", message.text):
940 a47eb4c Dawsh 2y ago expire_date = today
941 01cdf2e Dawsh 2y ago number_pattern = r"^[0-9]{1,3}"
942 a47eb4c Dawsh 2y ago number = int(re.findall(number_pattern, message.text)[0])
943 01cdf2e Dawsh 2y ago symbol_pattern = r"(M|m|D|d)$"
944 a47eb4c Dawsh 2y ago symbol = re.findall(symbol_pattern, message.text)[0].upper()
945 01cdf2e Dawsh 2y ago if symbol == "M":
946 a47eb4c Dawsh 2y ago expire_date = today + relativedelta(months=number)
947 01cdf2e Dawsh 2y ago elif symbol == "D":
948 a47eb4c Dawsh 2y ago expire_date = today + relativedelta(days=number)
949 01cdf2e Dawsh 2y ago elif message.text != "0":
950 a47eb4c Dawsh 2y ago expire_date = datetime.strptime(message.text, "%Y-%m-%d")
951 a47eb4c Dawsh 2y ago else:
952 a47eb4c Dawsh 2y ago expire_date = None
953 a47eb4c Dawsh 2y ago if expire_date and expire_date < today:
954 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
955 01cdf2e Dawsh 2y ago message.chat.id, "❌ Expire date must be greater than today."
956 01cdf2e Dawsh 2y ago )
957 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
958 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
959 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_expire_step, username=username, data_limit=data_limit)
960 a47eb4c Dawsh 2y ago except ValueError:
961 01cdf2e Dawsh 2y ago wait_msg = await bot.send_message(
962 01cdf2e Dawsh 2y ago message.chat.id,
963 01cdf2e Dawsh 2y ago "❌ Expire date must be in YYYY-MM-DD format.\nOr You Can Use Regex Symbol: ^[0-9]{1,3}(M|D)",
964 01cdf2e Dawsh 2y ago )
965 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, wait_msg.id)
966 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
967 01cdf2e Dawsh 2y ago return # bot.register_next_step_handler(wait_msg, add_user_expire_step, username=username, data_limit=data_limit)
968 a47eb4c Dawsh 2y ago async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
969 a47eb4c Dawsh 2y ago username, data_limit = data["username"], data["data_limit"]
970 01cdf2e Dawsh 2y ago mem_store.set(f"{message.chat.id}:username", username)
971 01cdf2e Dawsh 2y ago mem_store.set(f"{message.chat.id}:data_limit", data_limit)
972 01cdf2e Dawsh 2y ago mem_store.set(f"{message.chat.id}:expire_date", expire_date)
973 a47eb4c Dawsh 2y ago
974 9bfa731 Dawsh 2y ago with GetDB() as db:
975 9bfa731 Dawsh 2y ago services = crud.get_services(db)
976 a47eb4c Dawsh 2y ago schedule_delete_message(message.chat.id, message.id)
977 a47eb4c Dawsh 2y ago await cleanup_messages(message.chat.id)
978 a47eb4c Dawsh 2y ago await bot.send_message(
979 a47eb4c Dawsh 2y ago message.chat.id,
980 01cdf2e Dawsh 2y ago "Select Services:\nUsernames: {}\nData Limit: {}\nExpiry Date {}".format(
981 01cdf2e Dawsh 2y ago mem_store.get(f"{message.chat.id}:username"),
982 01cdf2e Dawsh 2y ago (
983 01cdf2e Dawsh 2y ago readable_size(mem_store.get(f"{message.chat.id}:data_limit"))
984 01cdf2e Dawsh 2y ago if mem_store.get(f"{message.chat.id}:data_limit")
985 01cdf2e Dawsh 2y ago else "Unlimited"
986 01cdf2e Dawsh 2y ago ),
987 01cdf2e Dawsh 2y ago (
988 01cdf2e Dawsh 2y ago mem_store.get(f"{message.chat.id}:expire_date").strftime("%Y-%m-%d")
989 01cdf2e Dawsh 2y ago if mem_store.get(f"{message.chat.id}:expire_date")
990 01cdf2e Dawsh 2y ago else "Never"
991 01cdf2e Dawsh 2y ago ),
992 a47eb4c Dawsh 2y ago ),
993 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.select_services(services, [], action="create"),
994 a47eb4c Dawsh 2y ago )
995 a47eb4c Dawsh 2y ago
996 a47eb4c Dawsh 2y ago
997 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("switch_service:"), is_admin=True)
998 9bfa731 Dawsh 2y ago async def switch_service(call: types.CallbackQuery):
999 01cdf2e Dawsh 2y ago if not (username := mem_store.get(f"{call.message.chat.id}:username")):
1000 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
1001 01cdf2e Dawsh 2y ago call.id, "❌ No user selected.", show_alert=True
1002 01cdf2e Dawsh 2y ago )
1003 01cdf2e Dawsh 2y ago selected_services: List[int] = mem_store.get(f"{call.message.chat.id}:services", [])
1004 01cdf2e Dawsh 2y ago _, service, action = call.data.split(":")
1005 9bfa731 Dawsh 2y ago service = int(service)
1006 a47eb4c Dawsh 2y ago
1007 9bfa731 Dawsh 2y ago with GetDB() as db:
1008 9bfa731 Dawsh 2y ago services = crud.get_services(db)
1009 9bfa731 Dawsh 2y ago if service in selected_services:
1010 9bfa731 Dawsh 2y ago selected_services.remove(service)
1011 a47eb4c Dawsh 2y ago else:
1012 9bfa731 Dawsh 2y ago selected_services.append(service)
1013 9bfa731 Dawsh 2y ago
1014 01cdf2e Dawsh 2y ago mem_store.set(f"{call.message.chat.id}:services", selected_services)
1015 a47eb4c Dawsh 2y ago
1016 9bfa731 Dawsh 2y ago if action in ["edit"]:
1017 a47eb4c Dawsh 2y ago return await bot.edit_message_text(
1018 a47eb4c Dawsh 2y ago call.message.text,
1019 a47eb4c Dawsh 2y ago call.message.chat.id,
1020 a47eb4c Dawsh 2y ago call.message.message_id,
1021 9bfa731 Dawsh 2y ago reply_markup=BotKeyboard.select_services(
1022 9bfa731 Dawsh 2y ago services,
1023 9bfa731 Dawsh 2y ago selected_services,
1024 a47eb4c Dawsh 2y ago "edit",
1025 a47eb4c Dawsh 2y ago username=username,
1026 a47eb4c Dawsh 2y ago data_limit=mem_store.get(f"{call.message.chat.id}:data_limit"),
1027 01cdf2e Dawsh 2y ago expire_date=mem_store.get(f"{call.message.chat.id}:expire_date"),
1028 01cdf2e Dawsh 2y ago ),
1029 a47eb4c Dawsh 2y ago )
1030 a47eb4c Dawsh 2y ago await bot.edit_message_text(
1031 a47eb4c Dawsh 2y ago call.message.text,
1032 a47eb4c Dawsh 2y ago call.message.chat.id,
1033 a47eb4c Dawsh 2y ago call.message.message_id,
1034 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.select_services(services, selected_services, "create"),
1035 a47eb4c Dawsh 2y ago )
1036 a47eb4c Dawsh 2y ago
1037 a47eb4c Dawsh 2y ago
1038 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("confirm:"), is_admin=True)
1039 a47eb4c Dawsh 2y ago async def confirm_user_command(call: types.CallbackQuery):
1040 01cdf2e Dawsh 2y ago data = call.data.split(":")[1]
1041 a47eb4c Dawsh 2y ago chat_id = call.from_user.id
1042 a47eb4c Dawsh 2y ago full_name = call.from_user.full_name
1043 a47eb4c Dawsh 2y ago now = datetime.now()
1044 a47eb4c Dawsh 2y ago today = datetime(
1045 01cdf2e Dawsh 2y ago year=now.year, month=now.month, day=now.day, hour=23, minute=59, second=59
1046 01cdf2e Dawsh 2y ago )
1047 01cdf2e Dawsh 2y ago
1048 a47eb4c Dawsh 2y ago
1049 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_delete"), is_admin=True)
1050 9bfa731 Dawsh 2y ago async def delete_user(call: types.CallbackQuery):
1051 01cdf2e Dawsh 2y ago username = call.data.split(":")[1]
1052 9bfa731 Dawsh 2y ago with GetDB() as db:
1053 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1054 9bfa731 Dawsh 2y ago crud.remove_user(db, db_user)
1055 9bfa731 Dawsh 2y ago await xray.operations.remove_user(db_user)
1056 9bfa731 Dawsh 2y ago
1057 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1058 01cdf2e Dawsh 2y ago "✅ User deleted.",
1059 9bfa731 Dawsh 2y ago call.message.chat.id,
1060 9bfa731 Dawsh 2y ago call.message.message_id,
1061 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1062 9bfa731 Dawsh 2y ago )
1063 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1064 01cdf2e Dawsh 2y ago text = f"""\
1065 a47eb4c Dawsh 2y ago 🗑 <b>#Deleted #From_Bot</b>
1066 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1067 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{db_user.username}</code>
1068 a47eb4c Dawsh 2y ago <b>Traffic Limit :</b> <code>{readable_size(db_user.data_limit) if db_user.data_limit else "Unlimited"}</code>
1069 a47eb4c Dawsh 2y ago <b>Expire Date :</b> <code>\
1070 a47eb4c Dawsh 2y ago {datetime.fromtimestamp(db_user.expire).strftime('%H:%M:%S %Y-%m-%d') if db_user.expire else "Never"}</code>
1071 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1072 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1073 9bfa731 Dawsh 2y ago try:
1074 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1075 9bfa731 Dawsh 2y ago except:
1076 9bfa731 Dawsh 2y ago pass
1077 9bfa731 Dawsh 2y ago
1078 9bfa731 Dawsh 2y ago
1079 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_suspend"), is_admin=True)
1080 9bfa731 Dawsh 2y ago async def disable_user(call: types.CallbackQuery):
1081 9bfa731 Dawsh 2y ago username = call.data.split(":")[1]
1082 9bfa731 Dawsh 2y ago with GetDB() as db:
1083 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1084 01cdf2e Dawsh 2y ago crud.update_user(
1085 01cdf2e Dawsh 2y ago db, db_user, UserModify(username=username, status=UserStatusModify.disabled)
1086 01cdf2e Dawsh 2y ago )
1087 9bfa731 Dawsh 2y ago await xray.operations.remove_user(db_user)
1088 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1089 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1090 9bfa731 Dawsh 2y ago get_user_info_text(
1091 01cdf2e Dawsh 2y ago status="disabled",
1092 9bfa731 Dawsh 2y ago username=username,
1093 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1094 9bfa731 Dawsh 2y ago data_limit=db_user.data_limit,
1095 9bfa731 Dawsh 2y ago usage=db_user.used_traffic,
1096 9bfa731 Dawsh 2y ago expire=db_user.expire,
1097 01cdf2e Dawsh 2y ago note=user.note,
1098 9bfa731 Dawsh 2y ago ),
1099 9bfa731 Dawsh 2y ago call.message.chat.id,
1100 9bfa731 Dawsh 2y ago call.message.message_id,
1101 01cdf2e Dawsh 2y ago parse_mode="HTML",
1102 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1103 01cdf2e Dawsh 2y ago user_info={"status": "disabled", "username": db_user.username}, note=True
1104 01cdf2e Dawsh 2y ago ),
1105 01cdf2e Dawsh 2y ago )
1106 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1107 01cdf2e Dawsh 2y ago text = f"""\
1108 a47eb4c Dawsh 2y ago ❌ <b>#Disabled #From_Bot</b>
1109 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1110 a47eb4c Dawsh 2y ago <b>Username</b> : <code>{username}</code>
1111 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1112 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1113 9bfa731 Dawsh 2y ago try:
1114 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1115 9bfa731 Dawsh 2y ago except:
1116 9bfa731 Dawsh 2y ago pass
1117 9bfa731 Dawsh 2y ago
1118 01cdf2e Dawsh 2y ago
1119 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_activate"), is_admin=True)
1120 9bfa731 Dawsh 2y ago async def activate_user(call: types.CallbackQuery):
1121 9bfa731 Dawsh 2y ago username = call.data.split(":")[1]
1122 9bfa731 Dawsh 2y ago with GetDB() as db:
1123 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1124 01cdf2e Dawsh 2y ago crud.update_user(
1125 01cdf2e Dawsh 2y ago db, db_user, UserModify(username=username, status=UserStatusModify.active)
1126 01cdf2e Dawsh 2y ago )
1127 9bfa731 Dawsh 2y ago await xray.operations.add_user(db_user)
1128 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1129 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1130 9bfa731 Dawsh 2y ago get_user_info_text(
1131 01cdf2e Dawsh 2y ago status="active",
1132 9bfa731 Dawsh 2y ago username=username,
1133 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1134 9bfa731 Dawsh 2y ago data_limit=db_user.data_limit,
1135 9bfa731 Dawsh 2y ago usage=db_user.used_traffic,
1136 9bfa731 Dawsh 2y ago expire=db_user.expire,
1137 01cdf2e Dawsh 2y ago note=user.note,
1138 9bfa731 Dawsh 2y ago ),
1139 9bfa731 Dawsh 2y ago call.message.chat.id,
1140 9bfa731 Dawsh 2y ago call.message.message_id,
1141 01cdf2e Dawsh 2y ago parse_mode="HTML",
1142 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1143 01cdf2e Dawsh 2y ago user_info={"status": "active", "username": db_user.username}, note=True
1144 01cdf2e Dawsh 2y ago ),
1145 01cdf2e Dawsh 2y ago )
1146 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1147 01cdf2e Dawsh 2y ago text = f"""\
1148 a47eb4c Dawsh 2y ago ✅ <b>#Activated #From_Bot</b>
1149 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1150 a47eb4c Dawsh 2y ago <b>Username</b> : <code>{username}</code>
1151 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1152 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1153 9bfa731 Dawsh 2y ago try:
1154 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1155 9bfa731 Dawsh 2y ago except:
1156 9bfa731 Dawsh 2y ago pass
1157 9bfa731 Dawsh 2y ago
1158 01cdf2e Dawsh 2y ago
1159 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_reset_usage"), is_admin=True)
1160 9bfa731 Dawsh 2y ago async def reset_all_usages(call: types.CallbackQuery):
1161 9bfa731 Dawsh 2y ago username = call.data.split(":")[1]
1162 9bfa731 Dawsh 2y ago with GetDB() as db:
1163 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1164 9bfa731 Dawsh 2y ago crud.reset_user_data_usage(db, db_user)
1165 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1166 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1167 9bfa731 Dawsh 2y ago get_user_info_text(
1168 9bfa731 Dawsh 2y ago status=user.status,
1169 9bfa731 Dawsh 2y ago username=username,
1170 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1171 9bfa731 Dawsh 2y ago data_limit=user.data_limit,
1172 9bfa731 Dawsh 2y ago usage=user.used_traffic,
1173 9bfa731 Dawsh 2y ago expire=user.expire,
1174 01cdf2e Dawsh 2y ago note=user.note,
1175 9bfa731 Dawsh 2y ago ),
1176 9bfa731 Dawsh 2y ago call.message.chat.id,
1177 9bfa731 Dawsh 2y ago call.message.message_id,
1178 01cdf2e Dawsh 2y ago parse_mode="HTML",
1179 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1180 01cdf2e Dawsh 2y ago user_info={"status": user.status, "username": user.username}, note=True
1181 01cdf2e Dawsh 2y ago ),
1182 01cdf2e Dawsh 2y ago )
1183 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1184 01cdf2e Dawsh 2y ago text = f"""\
1185 9bfa731 Dawsh 2y ago 🔁 <b>#Reset_usage #From_Bot</b>
1186 9bfa731 Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1187 a47eb4c Dawsh 2y ago <b>Username</b> : <code>{username}</code>
1188 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1189 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1190 9bfa731 Dawsh 2y ago try:
1191 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1192 9bfa731 Dawsh 2y ago except:
1193 9bfa731 Dawsh 2y ago pass
1194 9bfa731 Dawsh 2y ago
1195 9bfa731 Dawsh 2y ago
1196 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_equals("do_restart"), is_admin=True)
1197 9bfa731 Dawsh 2y ago async def restart_xray(call: types.CallbackQuery):
1198 9bfa731 Dawsh 2y ago m = await bot.edit_message_text(
1199 01cdf2e Dawsh 2y ago "🔄 Restarting XRay core...", call.message.chat.id, call.message.message_id
1200 01cdf2e Dawsh 2y ago )
1201 9bfa731 Dawsh 2y ago await xray.core.restart(xray.config.include_db_users())
1202 9bfa731 Dawsh 2y ago for node_id, node in list(xray.nodes.items()):
1203 9bfa731 Dawsh 2y ago if await node.is_healthy():
1204 9bfa731 Dawsh 2y ago await xray.operations.restart_node(node_id, xray.config.include_db_users())
1205 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1206 01cdf2e Dawsh 2y ago "✅ XRay core restarted successfully.",
1207 01cdf2e Dawsh 2y ago m.chat.id,
1208 01cdf2e Dawsh 2y ago m.message_id,
1209 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1210 9bfa731 Dawsh 2y ago )
1211 9bfa731 Dawsh 2y ago
1212 01cdf2e Dawsh 2y ago
1213 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_equals("edit_user_done"), is_admin=True)
1214 9bfa731 Dawsh 2y ago async def edit_user(call: types.CallbackQuery):
1215 01cdf2e Dawsh 2y ago if (username := mem_store.get(f"{call.message.chat.id}:username")) is None:
1216 9bfa731 Dawsh 2y ago try:
1217 01cdf2e Dawsh 2y ago await bot.delete_message(call.message.chat.id, call.message.message_id)
1218 9bfa731 Dawsh 2y ago except Exception:
1219 9bfa731 Dawsh 2y ago pass
1220 9bfa731 Dawsh 2y ago return await bot.send_message(
1221 9bfa731 Dawsh 2y ago call.message.chat.id,
1222 01cdf2e Dawsh 2y ago "❌ Bot reload detected. Please start over.",
1223 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1224 a47eb4c Dawsh 2y ago )
1225 a47eb4c Dawsh 2y ago
1226 01cdf2e Dawsh 2y ago if not mem_store.get(f"{call.message.chat.id}:services"):
1227 9bfa731 Dawsh 2y ago return await bot.answer_callback_query(
1228 01cdf2e Dawsh 2y ago call.id, "❌ No Service selected.", show_alert=True
1229 9bfa731 Dawsh 2y ago )
1230 a47eb4c Dawsh 2y ago
1231 01cdf2e Dawsh 2y ago # inbounds: dict[str, list[str]] = {
1232 9bfa731 Dawsh 2y ago # k: v for k, v in mem_store.get(f'{call.message.chat.id}:protocols').items() if v}
1233 9bfa731 Dawsh 2y ago services: List[int] = mem_store.get(f"{call.message.chat.id}:services")
1234 a47eb4c Dawsh 2y ago
1235 9bfa731 Dawsh 2y ago with GetDB() as db:
1236 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1237 9bfa731 Dawsh 2y ago if not db_user:
1238 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
1239 01cdf2e Dawsh 2y ago call.id, text=f"User not found!", show_alert=True
1240 01cdf2e Dawsh 2y ago )
1241 a47eb4c Dawsh 2y ago
1242 9bfa731 Dawsh 2y ago # proxies = {p.type.value: p.settings for p in db_user.proxies}
1243 a47eb4c Dawsh 2y ago
1244 9bfa731 Dawsh 2y ago """for protocol in xray.config.inbounds_by_protocol:
1245 9bfa731 Dawsh 2y ago if protocol in inbounds and protocol not in db_user.inbounds:
1246 9bfa731 Dawsh 2y ago proxies.update({protocol: {'flow': TELEGRAM_DEFAULT_VLESS_FLOW} if
1247 9bfa731 Dawsh 2y ago TELEGRAM_DEFAULT_VLESS_FLOW and protocol == ProxyTypes.VLESS else {}})
1248 9bfa731 Dawsh 2y ago elif protocol in db_user.inbounds and protocol not in inbounds:
1249 9bfa731 Dawsh 2y ago del proxies[protocol]"""
1250 a47eb4c Dawsh 2y ago
1251 9bfa731 Dawsh 2y ago modify = UserModify(
1252 9bfa731 Dawsh 2y ago username=username,
1253 9bfa731 Dawsh 2y ago # status=db_user.status,
1254 01cdf2e Dawsh 2y ago expire=(
1255 01cdf2e Dawsh 2y ago int(mem_store.get(f"{call.message.chat.id}:expire_date").timestamp())
1256 01cdf2e Dawsh 2y ago if mem_store.get(f"{call.message.chat.id}:expire_date")
1257 01cdf2e Dawsh 2y ago else 0
1258 01cdf2e Dawsh 2y ago ),
1259 9bfa731 Dawsh 2y ago data_limit=mem_store.get(f"{call.message.chat.id}:data_limit"),
1260 01cdf2e Dawsh 2y ago services=services,
1261 01cdf2e Dawsh 2y ago )
1262 9bfa731 Dawsh 2y ago last_user = UserResponse.model_validate(db_user)
1263 9bfa731 Dawsh 2y ago db_user = crud.update_user(db, db_user, modify)
1264 a47eb4c Dawsh 2y ago
1265 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1266 a47eb4c Dawsh 2y ago
1267 9bfa731 Dawsh 2y ago if user.status == UserStatus.active:
1268 9bfa731 Dawsh 2y ago await xray.operations.update_user(db_user)
1269 9bfa731 Dawsh 2y ago else:
1270 9bfa731 Dawsh 2y ago await xray.operations.remove_user(db_user)
1271 a47eb4c Dawsh 2y ago
1272 9bfa731 Dawsh 2y ago await bot.answer_callback_query(call.id, "✅ User updated successfully.")
1273 a47eb4c Dawsh 2y ago
1274 9bfa731 Dawsh 2y ago text = get_user_info_text(
1275 9bfa731 Dawsh 2y ago status=user.status,
1276 9bfa731 Dawsh 2y ago username=user.username,
1277 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1278 9bfa731 Dawsh 2y ago data_limit=user.data_limit,
1279 9bfa731 Dawsh 2y ago usage=user.used_traffic,
1280 9bfa731 Dawsh 2y ago expire=user.expire,
1281 01cdf2e Dawsh 2y ago note=user.note,
1282 9bfa731 Dawsh 2y ago )
1283 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1284 9bfa731 Dawsh 2y ago text,
1285 9bfa731 Dawsh 2y ago call.message.chat.id,
1286 9bfa731 Dawsh 2y ago call.message.message_id,
1287 9bfa731 Dawsh 2y ago parse_mode="HTML",
1288 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1289 01cdf2e Dawsh 2y ago {"username": db_user.username, "status": db_user.status}, note=True
1290 01cdf2e Dawsh 2y ago ),
1291 9bfa731 Dawsh 2y ago )
1292 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1293 9bfa731 Dawsh 2y ago tag = f'\n➖➖➖➖➖➖➖➖➖ \n<b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>'
1294 9bfa731 Dawsh 2y ago if last_user.data_limit != user.data_limit:
1295 01cdf2e Dawsh 2y ago text = f"""\
1296 a47eb4c Dawsh 2y ago 📶 <b>#Traffic_Change #From_Bot</b>
1297 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1298 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{user.username}</code>
1299 a47eb4c Dawsh 2y ago <b>Last Traffic Limit :</b> <code>{readable_size(last_user.data_limit) if last_user.data_limit else "Unlimited"}</code>
1300 01cdf2e Dawsh 2y ago <b>New Traffic Limit :</b> <code>{readable_size(user.data_limit) if user.data_limit else "Unlimited"}</code>{tag}"""
1301 9bfa731 Dawsh 2y ago try:
1302 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1303 9bfa731 Dawsh 2y ago except:
1304 9bfa731 Dawsh 2y ago pass
1305 9bfa731 Dawsh 2y ago if last_user.expire != user.expire:
1306 01cdf2e Dawsh 2y ago text = f"""\
1307 a47eb4c Dawsh 2y ago 📅 <b>#Expiry_Change #From_Bot</b>
1308 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1309 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{user.username}</code>
1310 a47eb4c Dawsh 2y ago <b>Last Expire Date :</b> <code>\
1311 a47eb4c Dawsh 2y ago {datetime.fromtimestamp(last_user.expire).strftime('%H:%M:%S %Y-%m-%d') if last_user.expire else "Never"}</code>
1312 a47eb4c Dawsh 2y ago <b>New Expire Date :</b> <code>\
1313 01cdf2e Dawsh 2y ago {datetime.fromtimestamp(user.expire).strftime('%H:%M:%S %Y-%m-%d') if user.expire else "Never"}</code>{tag}"""
1314 9bfa731 Dawsh 2y ago try:
1315 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1316 9bfa731 Dawsh 2y ago except:
1317 9bfa731 Dawsh 2y ago pass
1318 9bfa731 Dawsh 2y ago if list(last_user.inbounds.values())[0] != list(user.inbounds.values())[0]:
1319 01cdf2e Dawsh 2y ago text = f"""\
1320 a47eb4c Dawsh 2y ago ⚙️ <b>#Inbounds_Change #From_Bot</b>
1321 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1322 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{user.username}</code>
1323 a47eb4c Dawsh 2y ago <b>Last Proxies :</b> <code>{", ".join(list(last_user.inbounds.values())[0])}</code>
1324 01cdf2e Dawsh 2y ago <b>New Proxies :</b> <code>{", ".join(list(user.inbounds.values())[0])}</code>{tag}"""
1325 a47eb4c Dawsh 2y ago try:
1326 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1327 9bfa731 Dawsh 2y ago except:
1328 a47eb4c Dawsh 2y ago pass
1329 a47eb4c Dawsh 2y ago
1330 a47eb4c Dawsh 2y ago
1331 9bfa731 Dawsh 2y ago @bot.callback_query_handler(query_startswith("add_user_done"), is_admin=True)
1332 9bfa731 Dawsh 2y ago async def delete_limpired(call: types.CallbackQuery):
1333 01cdf2e Dawsh 2y ago if mem_store.get(f"{call.message.chat.id}:username") is None:
1334 a47eb4c Dawsh 2y ago try:
1335 01cdf2e Dawsh 2y ago await bot.delete_message(call.message.chat.id, call.message.message_id)
1336 9bfa731 Dawsh 2y ago except Exception:
1337 9bfa731 Dawsh 2y ago pass
1338 9bfa731 Dawsh 2y ago return await bot.send_message(
1339 9bfa731 Dawsh 2y ago call.message.chat.id,
1340 01cdf2e Dawsh 2y ago "❌ Bot reload detected. Please start over.",
1341 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1342 9bfa731 Dawsh 2y ago )
1343 a47eb4c Dawsh 2y ago
1344 01cdf2e Dawsh 2y ago if not mem_store.get(f"{call.message.chat.id}:services"):
1345 9bfa731 Dawsh 2y ago return await bot.answer_callback_query(
1346 01cdf2e Dawsh 2y ago call.id, "❌ No services selected.", show_alert=True
1347 9bfa731 Dawsh 2y ago )
1348 a47eb4c Dawsh 2y ago
1349 01cdf2e Dawsh 2y ago services: List[int] = mem_store.get(f"{call.message.chat.id}:services", [])
1350 01cdf2e Dawsh 2y ago
1351 9bfa731 Dawsh 2y ago new_user = UserCreate(
1352 01cdf2e Dawsh 2y ago username=mem_store.get(f"{call.message.chat.id}:username"),
1353 01cdf2e Dawsh 2y ago expire=(
1354 01cdf2e Dawsh 2y ago int(mem_store.get(f"{call.message.chat.id}:expire_date").timestamp())
1355 01cdf2e Dawsh 2y ago if mem_store.get(f"{call.message.chat.id}:expire_date")
1356 01cdf2e Dawsh 2y ago else None
1357 01cdf2e Dawsh 2y ago ),
1358 01cdf2e Dawsh 2y ago data_limit=(
1359 01cdf2e Dawsh 2y ago mem_store.get(f"{call.message.chat.id}:data_limit")
1360 01cdf2e Dawsh 2y ago if mem_store.get(f"{call.message.chat.id}:data_limit")
1361 01cdf2e Dawsh 2y ago else None
1362 01cdf2e Dawsh 2y ago ),
1363 01cdf2e Dawsh 2y ago services=services,
1364 01cdf2e Dawsh 2y ago )
1365 9bfa731 Dawsh 2y ago try:
1366 9bfa731 Dawsh 2y ago with GetDB() as db:
1367 9bfa731 Dawsh 2y ago db_user = crud.create_user(db, new_user)
1368 9bfa731 Dawsh 2y ago services = db_user.services
1369 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1370 9bfa731 Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
1371 9bfa731 Dawsh 2y ago db.rollback()
1372 9bfa731 Dawsh 2y ago return await bot.answer_callback_query(
1373 01cdf2e Dawsh 2y ago call.id, "❌ Username already exists.", show_alert=True
1374 a47eb4c Dawsh 2y ago )
1375 a47eb4c Dawsh 2y ago
1376 9bfa731 Dawsh 2y ago await xray.operations.add_user(db_user)
1377 9bfa731 Dawsh 2y ago
1378 9bfa731 Dawsh 2y ago text = get_user_info_text(
1379 9bfa731 Dawsh 2y ago status=user.status,
1380 9bfa731 Dawsh 2y ago username=user.username,
1381 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1382 9bfa731 Dawsh 2y ago data_limit=user.data_limit,
1383 9bfa731 Dawsh 2y ago usage=user.used_traffic,
1384 9bfa731 Dawsh 2y ago expire=user.expire,
1385 01cdf2e Dawsh 2y ago note=user.note,
1386 9bfa731 Dawsh 2y ago )
1387 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1388 9bfa731 Dawsh 2y ago text,
1389 9bfa731 Dawsh 2y ago call.message.chat.id,
1390 9bfa731 Dawsh 2y ago call.message.message_id,
1391 9bfa731 Dawsh 2y ago parse_mode="HTML",
1392 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1393 01cdf2e Dawsh 2y ago user_info={"status": user.status, "username": user.username}, note=True
1394 01cdf2e Dawsh 2y ago ),
1395 01cdf2e Dawsh 2y ago )
1396 9bfa731 Dawsh 2y ago
1397 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1398 01cdf2e Dawsh 2y ago text = f"""\
1399 a47eb4c Dawsh 2y ago 🆕 <b>#Created #From_Bot</b>
1400 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1401 a47eb4c Dawsh 2y ago <b>Username :</b> <code>{user.username}</code>
1402 a47eb4c Dawsh 2y ago <b>Traffic Limit :</b> <code>{readable_size(user.data_limit) if user.data_limit else "Unlimited"}</code>
1403 a47eb4c Dawsh 2y ago <b>Expire Date :</b> <code>\
1404 a47eb4c Dawsh 2y ago {datetime.fromtimestamp(user.expire).strftime('%H:%M:%S %Y-%m-%d') if user.expire else "Never"}</code>
1405 9bfa731 Dawsh 2y ago <b>Services :</b> <code>{"" if not services else ", ".join([s.name for s in services])}</code>
1406 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1407 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1408 9bfa731 Dawsh 2y ago try:
1409 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1410 9bfa731 Dawsh 2y ago except:
1411 9bfa731 Dawsh 2y ago pass
1412 a47eb4c Dawsh 2y ago
1413 9bfa731 Dawsh 2y ago
1414 01cdf2e Dawsh 2y ago @bot.callback_query_handler(
1415 01cdf2e Dawsh 2y ago query_startswith(["do_delete_expired", "do_delete_limited"]), is_admin=True
1416 01cdf2e Dawsh 2y ago )
1417 9bfa731 Dawsh 2y ago async def delete_limpired(call: types.CallbackQuery):
1418 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1419 01cdf2e Dawsh 2y ago "⏳ <b>In Progress...</b>",
1420 9bfa731 Dawsh 2y ago call.message.chat.id,
1421 9bfa731 Dawsh 2y ago call.message.message_id,
1422 01cdf2e Dawsh 2y ago parse_mode="HTML",
1423 01cdf2e Dawsh 2y ago )
1424 9bfa731 Dawsh 2y ago with GetDB() as db:
1425 9bfa731 Dawsh 2y ago depleted_users = crud.get_users(
1426 01cdf2e Dawsh 2y ago db,
1427 01cdf2e Dawsh 2y ago status=[
1428 01cdf2e Dawsh 2y ago UserStatus.limited if data == "delete_limited" else UserStatus.expired
1429 01cdf2e Dawsh 2y ago ],
1430 01cdf2e Dawsh 2y ago )
1431 01cdf2e Dawsh 2y ago file_name = f"{data[8:]}_users_{int(now.timestamp()*1000)}.txt"
1432 01cdf2e Dawsh 2y ago with open(file_name, "w") as f:
1433 01cdf2e Dawsh 2y ago f.write("USERNAME\tEXIPRY\tUSAGE/LIMIT\tSTATUS\n")
1434 9bfa731 Dawsh 2y ago deleted = 0
1435 9bfa731 Dawsh 2y ago for user in depleted_users:
1436 9bfa731 Dawsh 2y ago try:
1437 9bfa731 Dawsh 2y ago crud.remove_user(db, user)
1438 9bfa731 Dawsh 2y ago await xray.operations.remove_user(user)
1439 01cdf2e Dawsh 2y ago deleted += 1
1440 01cdf2e Dawsh 2y ago f.write(
1441 01cdf2e Dawsh 2y ago f'{user.username}\
1442 a47eb4c Dawsh 2y ago \t{datetime.fromtimestamp(user.expire) if user.expire else "never"}\
1443 a47eb4c Dawsh 2y ago \t{readable_size(user.used_traffic) if user.used_traffic else 0}\
1444 a47eb4c Dawsh 2y ago /{readable_size(user.data_limit) if user.data_limit else "Unlimited"}\
1445 01cdf2e Dawsh 2y ago \t{user.status}\n'
1446 01cdf2e Dawsh 2y ago )
1447 9bfa731 Dawsh 2y ago except:
1448 9bfa731 Dawsh 2y ago db.rollback()
1449 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1450 01cdf2e Dawsh 2y ago f"✅ <code>{deleted}</code>/<code>{len(depleted_users)}</code> <b>{data[7:].title()} Users Deleted</b>",
1451 9bfa731 Dawsh 2y ago call.message.chat.id,
1452 9bfa731 Dawsh 2y ago call.message.message_id,
1453 9bfa731 Dawsh 2y ago parse_mode="HTML",
1454 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1455 01cdf2e Dawsh 2y ago )
1456 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1457 01cdf2e Dawsh 2y ago text = f"""\
1458 a47eb4c Dawsh 2y ago 🗑 <b>#Delete #{data[7:].title()} #From_Bot</b>
1459 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1460 a47eb4c Dawsh 2y ago <b>Count:</b> <code>{deleted}</code>
1461 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1462 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1463 9bfa731 Dawsh 2y ago try:
1464 01cdf2e Dawsh 2y ago await bot.send_document(
1465 01cdf2e Dawsh 2y ago TELEGRAM_LOGGER_CHANNEL_ID,
1466 01cdf2e Dawsh 2y ago open(file_name, "rb"),
1467 01cdf2e Dawsh 2y ago caption=text,
1468 01cdf2e Dawsh 2y ago parse_mode="HTML",
1469 01cdf2e Dawsh 2y ago )
1470 9bfa731 Dawsh 2y ago os.remove(file_name)
1471 9bfa731 Dawsh 2y ago except:
1472 9bfa731 Dawsh 2y ago pass
1473 9bfa731 Dawsh 2y ago
1474 01cdf2e Dawsh 2y ago
1475 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_add_data:"), is_admin=True)
1476 9bfa731 Dawsh 2y ago async def add_data_to_all(call: types.CallbackQuery):
1477 9bfa731 Dawsh 2y ago schedule_delete_message(
1478 9bfa731 Dawsh 2y ago call.message.chat.id,
1479 01cdf2e Dawsh 2y ago (await bot.send_message(chat_id, "⏳ <b>In Progress...</b>", "HTML")).id,
1480 01cdf2e Dawsh 2y ago )
1481 9bfa731 Dawsh 2y ago data_limit = float(call.data.split(":")[1]) * 1024 * 1024 * 1024
1482 9bfa731 Dawsh 2y ago with GetDB() as db:
1483 9bfa731 Dawsh 2y ago users = crud.get_users(db)
1484 9bfa731 Dawsh 2y ago counter = 0
1485 01cdf2e Dawsh 2y ago file_name = f"new_data_limit_users_{int(now.timestamp()*1000)}.txt"
1486 01cdf2e Dawsh 2y ago with open(file_name, "w") as f:
1487 01cdf2e Dawsh 2y ago f.write("USERNAME\tEXIPRY\tUSAGE/LIMIT\tSTATUS\n")
1488 9bfa731 Dawsh 2y ago for user in users:
1489 a47eb4c Dawsh 2y ago try:
1490 01cdf2e Dawsh 2y ago if user.data_limit and user.status not in [
1491 01cdf2e Dawsh 2y ago UserStatus.limited,
1492 01cdf2e Dawsh 2y ago UserStatus.expired,
1493 01cdf2e Dawsh 2y ago ]:
1494 01cdf2e Dawsh 2y ago user = crud.update_user(
1495 01cdf2e Dawsh 2y ago db,
1496 01cdf2e Dawsh 2y ago user,
1497 01cdf2e Dawsh 2y ago UserModify(
1498 01cdf2e Dawsh 2y ago username=user.username,
1499 01cdf2e Dawsh 2y ago data_limit=(user.data_limit + data_limit),
1500 01cdf2e Dawsh 2y ago ),
1501 01cdf2e Dawsh 2y ago )
1502 9bfa731 Dawsh 2y ago counter += 1
1503 9bfa731 Dawsh 2y ago f.write(
1504 9bfa731 Dawsh 2y ago f'{user.username}\
1505 a47eb4c Dawsh 2y ago \t{datetime.fromtimestamp(user.expire) if user.expire else "never"}\
1506 a47eb4c Dawsh 2y ago \t{readable_size(user.used_traffic) if user.used_traffic else 0}\
1507 a47eb4c Dawsh 2y ago /{readable_size(user.data_limit) if user.data_limit else "Unlimited"}\
1508 01cdf2e Dawsh 2y ago \t{user.status}\n'
1509 01cdf2e Dawsh 2y ago )
1510 9bfa731 Dawsh 2y ago except:
1511 9bfa731 Dawsh 2y ago db.rollback()
1512 9bfa731 Dawsh 2y ago await cleanup_messages(chat_id)
1513 9bfa731 Dawsh 2y ago await bot.send_message(
1514 9bfa731 Dawsh 2y ago chat_id,
1515 9bfa731 Dawsh 2y ago f'✅ <b>{counter}/{len(users)} Users</b> Data Limit according to <code>{"+" if data_limit > 0 else "-"}{readable_size(abs(data_limit))}</code>',
1516 01cdf2e Dawsh 2y ago "HTML",
1517 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1518 01cdf2e Dawsh 2y ago )
1519 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1520 01cdf2e Dawsh 2y ago text = f"""\
1521 a47eb4c Dawsh 2y ago 📶 <b>#Traffic_Change #From_Bot</b>
1522 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1523 a47eb4c Dawsh 2y ago <b>According to:</b> <code>{"+" if data_limit > 0 else "-"}{readable_size(abs(data_limit))}</code>
1524 a47eb4c Dawsh 2y ago <b>Count:</b> <code>{counter}</code>
1525 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1526 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1527 9bfa731 Dawsh 2y ago try:
1528 01cdf2e Dawsh 2y ago await bot.send_document(
1529 01cdf2e Dawsh 2y ago TELEGRAM_LOGGER_CHANNEL_ID,
1530 01cdf2e Dawsh 2y ago open(file_name, "rb"),
1531 01cdf2e Dawsh 2y ago caption=text,
1532 01cdf2e Dawsh 2y ago parse_mode="HTML",
1533 01cdf2e Dawsh 2y ago )
1534 9bfa731 Dawsh 2y ago os.remove(file_name)
1535 9bfa731 Dawsh 2y ago except:
1536 9bfa731 Dawsh 2y ago pass
1537 a47eb4c Dawsh 2y ago
1538 01cdf2e Dawsh 2y ago
1539 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_add_time:"), is_admin=True)
1540 9bfa731 Dawsh 2y ago async def add_time_to_all(call: types.CallbackQuery):
1541 9bfa731 Dawsh 2y ago schedule_delete_message(
1542 9bfa731 Dawsh 2y ago call.message.chat.id,
1543 01cdf2e Dawsh 2y ago (await bot.send_message(chat_id, "⏳ <b>In Progress...</b>", "HTML")).id,
1544 01cdf2e Dawsh 2y ago )
1545 9bfa731 Dawsh 2y ago days = int(call.data.split(":")[1])
1546 9bfa731 Dawsh 2y ago with GetDB() as db:
1547 9bfa731 Dawsh 2y ago users = crud.get_users(db)
1548 9bfa731 Dawsh 2y ago counter = 0
1549 01cdf2e Dawsh 2y ago file_name = f"new_expiry_users_{int(now.timestamp()*1000)}.txt"
1550 01cdf2e Dawsh 2y ago with open(file_name, "w") as f:
1551 01cdf2e Dawsh 2y ago f.write("USERNAME\tEXIPRY\tUSAGE/LIMIT\tSTATUS\n")
1552 9bfa731 Dawsh 2y ago for user in users:
1553 9bfa731 Dawsh 2y ago try:
1554 01cdf2e Dawsh 2y ago if user.expire and user.status not in [
1555 01cdf2e Dawsh 2y ago UserStatus.limited,
1556 01cdf2e Dawsh 2y ago UserStatus.expired,
1557 01cdf2e Dawsh 2y ago ]:
1558 9bfa731 Dawsh 2y ago user = crud.update_user(
1559 01cdf2e Dawsh 2y ago db,
1560 01cdf2e Dawsh 2y ago user,
1561 9bfa731 Dawsh 2y ago UserModify(
1562 9bfa731 Dawsh 2y ago username=user.username,
1563 9bfa731 Dawsh 2y ago expire=int(
1564 01cdf2e Dawsh 2y ago (
1565 01cdf2e Dawsh 2y ago datetime.fromtimestamp(user.expire)
1566 01cdf2e Dawsh 2y ago + relativedelta(days=days)
1567 01cdf2e Dawsh 2y ago ).timestamp()
1568 01cdf2e Dawsh 2y ago ),
1569 01cdf2e Dawsh 2y ago ),
1570 01cdf2e Dawsh 2y ago )
1571 9bfa731 Dawsh 2y ago counter += 1
1572 9bfa731 Dawsh 2y ago f.write(
1573 9bfa731 Dawsh 2y ago f'{user.username}\
1574 a47eb4c Dawsh 2y ago \t{datetime.fromtimestamp(user.expire) if user.expire else "never"}\
1575 a47eb4c Dawsh 2y ago \t{readable_size(user.used_traffic) if user.used_traffic else 0}\
1576 a47eb4c Dawsh 2y ago /{readable_size(user.data_limit) if user.data_limit else "Unlimited"}\
1577 01cdf2e Dawsh 2y ago \t{user.status}\n'
1578 01cdf2e Dawsh 2y ago )
1579 9bfa731 Dawsh 2y ago except:
1580 9bfa731 Dawsh 2y ago db.rollback()
1581 9bfa731 Dawsh 2y ago await cleanup_messages(chat_id)
1582 9bfa731 Dawsh 2y ago await bot.send_message(
1583 9bfa731 Dawsh 2y ago chat_id,
1584 01cdf2e Dawsh 2y ago f"✅ <b>{counter}/{len(users)} Users</b> Expiry Changes according to {days} Days",
1585 01cdf2e Dawsh 2y ago "HTML",
1586 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.main_menu(),
1587 01cdf2e Dawsh 2y ago )
1588 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1589 01cdf2e Dawsh 2y ago text = f"""\
1590 a47eb4c Dawsh 2y ago 📅 <b>#Expiry_Change #From_Bot</b>
1591 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1592 a47eb4c Dawsh 2y ago <b>According to:</b> <code>{days} Days</code>
1593 a47eb4c Dawsh 2y ago <b>Count:</b> <code>{counter}</code>
1594 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1595 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1596 9bfa731 Dawsh 2y ago try:
1597 01cdf2e Dawsh 2y ago await bot.send_document(
1598 01cdf2e Dawsh 2y ago TELEGRAM_LOGGER_CHANNEL_ID,
1599 01cdf2e Dawsh 2y ago open(file_name, "rb"),
1600 01cdf2e Dawsh 2y ago caption=text,
1601 01cdf2e Dawsh 2y ago parse_mode="HTML",
1602 01cdf2e Dawsh 2y ago )
1603 9bfa731 Dawsh 2y ago os.remove(file_name)
1604 9bfa731 Dawsh 2y ago except:
1605 9bfa731 Dawsh 2y ago pass
1606 9bfa731 Dawsh 2y ago
1607 01cdf2e Dawsh 2y ago
1608 01cdf2e Dawsh 2y ago @bot.callback_query_handler(query_startswith("do_revoke:"), is_admin=True)
1609 9bfa731 Dawsh 2y ago async def revoke_sub(call: types.CallbackQuery):
1610 9bfa731 Dawsh 2y ago username = call.data.split(":")[1]
1611 9bfa731 Dawsh 2y ago with GetDB() as db:
1612 9bfa731 Dawsh 2y ago db_user = crud.get_user(db, username)
1613 9bfa731 Dawsh 2y ago if not db_user:
1614 01cdf2e Dawsh 2y ago return await bot.answer_callback_query(
1615 01cdf2e Dawsh 2y ago call.id, text=f"User not found!", show_alert=True
1616 01cdf2e Dawsh 2y ago )
1617 9bfa731 Dawsh 2y ago db_user = crud.revoke_user_sub(db, db_user)
1618 9bfa731 Dawsh 2y ago user = UserResponse.model_validate(db_user)
1619 9bfa731 Dawsh 2y ago text = get_user_info_text(
1620 9bfa731 Dawsh 2y ago status=user.status,
1621 9bfa731 Dawsh 2y ago username=user.username,
1622 9bfa731 Dawsh 2y ago sub_url=user.subscription_url,
1623 9bfa731 Dawsh 2y ago expire=user.expire,
1624 9bfa731 Dawsh 2y ago data_limit=user.data_limit,
1625 9bfa731 Dawsh 2y ago usage=user.used_traffic,
1626 01cdf2e Dawsh 2y ago note=user.note,
1627 01cdf2e Dawsh 2y ago )
1628 9bfa731 Dawsh 2y ago await bot.edit_message_text(
1629 01cdf2e Dawsh 2y ago f"✅ Subscription Successfully Revoked!\n\n{text}",
1630 01cdf2e Dawsh 2y ago call.message.chat.id,
1631 01cdf2e Dawsh 2y ago call.message.message_id,
1632 01cdf2e Dawsh 2y ago parse_mode="HTML",
1633 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1634 01cdf2e Dawsh 2y ago user_info={"status": user.status, "username": user.username}, note=True
1635 01cdf2e Dawsh 2y ago ),
1636 01cdf2e Dawsh 2y ago )
1637 a47eb4c Dawsh 2y ago
1638 9bfa731 Dawsh 2y ago if TELEGRAM_LOGGER_CHANNEL_ID:
1639 01cdf2e Dawsh 2y ago text = f"""\
1640 a47eb4c Dawsh 2y ago 🚫 <b>#Revoke_sub #From_Bot</b>
1641 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1642 a47eb4c Dawsh 2y ago <b>Username:</b> <code>{username}</code>
1643 a47eb4c Dawsh 2y ago ➖➖➖➖➖➖➖➖➖
1644 01cdf2e Dawsh 2y ago <b>By :</b> <a href="tg://user?id={call.from_user.id}">{call.from_user.full_name}</a>"""
1645 9bfa731 Dawsh 2y ago try:
1646 01cdf2e Dawsh 2y ago await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, text, "HTML")
1647 9bfa731 Dawsh 2y ago except:
1648 9bfa731 Dawsh 2y ago pass
1649 a47eb4c Dawsh 2y ago
1650 a47eb4c Dawsh 2y ago
1651 a47eb4c Dawsh 2y ago @bot.message_handler(func=lambda message: True, is_admin=True)
1652 a47eb4c Dawsh 2y ago async def search(message: types.Message):
1653 a47eb4c Dawsh 2y ago with GetDB() as db:
1654 a47eb4c Dawsh 2y ago db_user = crud.get_user(db, message.text)
1655 a47eb4c Dawsh 2y ago if not db_user:
1656 01cdf2e Dawsh 2y ago return await bot.reply_to(message, "❌ User not found.")
1657 a47eb4c Dawsh 2y ago user = UserResponse.model_validate(db_user)
1658 a47eb4c Dawsh 2y ago text = get_user_info_text(
1659 a47eb4c Dawsh 2y ago status=user.status,
1660 a47eb4c Dawsh 2y ago username=user.username,
1661 a47eb4c Dawsh 2y ago sub_url=user.subscription_url,
1662 a47eb4c Dawsh 2y ago expire=user.expire,
1663 a47eb4c Dawsh 2y ago data_limit=user.data_limit,
1664 a47eb4c Dawsh 2y ago usage=user.used_traffic,
1665 01cdf2e Dawsh 2y ago note=user.note,
1666 01cdf2e Dawsh 2y ago )
1667 01cdf2e Dawsh 2y ago return await bot.reply_to(
1668 01cdf2e Dawsh 2y ago message,
1669 01cdf2e Dawsh 2y ago text,
1670 01cdf2e Dawsh 2y ago parse_mode="html",
1671 01cdf2e Dawsh 2y ago reply_markup=BotKeyboard.user_menu(
1672 01cdf2e Dawsh 2y ago user_info={"status": user.status, "username": user.username}, note=True
1673 01cdf2e Dawsh 2y ago ),
1674 01cdf2e Dawsh 2y ago )