add trial limits for create clusters_mode
This commit is contained in:
@@ -13,7 +13,7 @@ from aiogram.types import (
|
|||||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||||
|
|
||||||
from bot import bot
|
from bot import bot
|
||||||
from config import CONNECT_PHONE_BUTTON, SUPPORT_CHAT_URL
|
from config import CONNECT_PHONE_BUTTON, SUPPORT_CHAT_URL, TRIAL_CONFIG
|
||||||
from database import (
|
from database import (
|
||||||
get_key_details,
|
get_key_details,
|
||||||
get_tariff_by_id,
|
get_tariff_by_id,
|
||||||
@@ -73,8 +73,16 @@ async def key_cluster_mode(
|
|||||||
expiry_timestamp = int(expiry_time.timestamp() * 1000)
|
expiry_timestamp = int(expiry_time.timestamp() * 1000)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
data = await state.get_data() if state else {}
|
||||||
|
is_trial = data.get("is_trial", False)
|
||||||
|
|
||||||
device_limit = 0
|
device_limit = 0
|
||||||
if plan:
|
traffic_limit_bytes = None
|
||||||
|
|
||||||
|
if is_trial:
|
||||||
|
device_limit = TRIAL_CONFIG.get("hwid_limit", 1)
|
||||||
|
traffic_limit_bytes = int(TRIAL_CONFIG.get("traffic_limit_gb", 100) * 1024**3)
|
||||||
|
elif plan:
|
||||||
tariff = await get_tariff_by_id(session, plan)
|
tariff = await get_tariff_by_id(session, plan)
|
||||||
if tariff and tariff.get("device_limit") is not None:
|
if tariff and tariff.get("device_limit") is not None:
|
||||||
device_limit = int(tariff["device_limit"])
|
device_limit = int(tariff["device_limit"])
|
||||||
@@ -89,6 +97,7 @@ async def key_cluster_mode(
|
|||||||
plan=plan,
|
plan=plan,
|
||||||
session=session,
|
session=session,
|
||||||
hwid_limit=device_limit,
|
hwid_limit=device_limit,
|
||||||
|
traffic_limit_bytes=traffic_limit_bytes,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -103,9 +112,7 @@ async def key_cluster_mode(
|
|||||||
remnawave_link = key_record.get("remnawave_link")
|
remnawave_link = key_record.get("remnawave_link")
|
||||||
final_link = public_link or remnawave_link or ""
|
final_link = public_link or remnawave_link or ""
|
||||||
|
|
||||||
data = await state.get_data() if state else {}
|
if is_trial:
|
||||||
|
|
||||||
if data.get("is_trial"):
|
|
||||||
trial_status = await get_trial(session, tg_id)
|
trial_status = await get_trial(session, tg_id)
|
||||||
if trial_status in [0, -1]:
|
if trial_status in [0, -1]:
|
||||||
await update_trial(session, tg_id, 1)
|
await update_trial(session, tg_id, 1)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from aiogram.utils.keyboard import InlineKeyboardBuilder
|
|||||||
|
|
||||||
from config import (
|
from config import (
|
||||||
NOTIFY_EXTRA_DAYS,
|
NOTIFY_EXTRA_DAYS,
|
||||||
TRIAL_TIME,
|
TRIAL_CONFIG,
|
||||||
TRIAL_TIME_DISABLE,
|
TRIAL_TIME_DISABLE,
|
||||||
USE_COUNTRY_SELECTION,
|
USE_COUNTRY_SELECTION,
|
||||||
USE_NEW_PAYMENT_FLOW,
|
USE_NEW_PAYMENT_FLOW,
|
||||||
@@ -66,20 +66,23 @@ async def handle_key_creation(
|
|||||||
if not TRIAL_TIME_DISABLE:
|
if not TRIAL_TIME_DISABLE:
|
||||||
trial_status = await get_trial(session, tg_id)
|
trial_status = await get_trial(session, tg_id)
|
||||||
if trial_status in [0, -1]:
|
if trial_status in [0, -1]:
|
||||||
|
base_days = TRIAL_CONFIG["duration_days"]
|
||||||
extra_days = NOTIFY_EXTRA_DAYS if trial_status == -1 else 0
|
extra_days = NOTIFY_EXTRA_DAYS if trial_status == -1 else 0
|
||||||
expiry_time = current_time + timedelta(days=TRIAL_TIME + extra_days)
|
total_days = base_days + extra_days
|
||||||
logger.info(
|
expiry_time = current_time + timedelta(days=total_days)
|
||||||
f"Доступен {TRIAL_TIME + extra_days}-дневный пробный период пользователю {tg_id}."
|
|
||||||
)
|
logger.info(f"[Trial] Доступен {total_days}-дневный триал для пользователя {tg_id}")
|
||||||
|
|
||||||
await edit_or_send_message(
|
await edit_or_send_message(
|
||||||
target_message=(
|
target_message=(
|
||||||
message_or_query
|
message_or_query.message
|
||||||
if isinstance(message_or_query, Message)
|
if isinstance(message_or_query, CallbackQuery)
|
||||||
else message_or_query.message
|
else message_or_query
|
||||||
),
|
),
|
||||||
text=CREATING_CONNECTION_MSG,
|
text=CREATING_CONNECTION_MSG,
|
||||||
reply_markup=None,
|
reply_markup=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
await state.update_data(is_trial=True)
|
await state.update_data(is_trial=True)
|
||||||
await create_key(tg_id, expiry_time, state, session, message_or_query)
|
await create_key(tg_id, expiry_time, state, session, message_or_query)
|
||||||
return
|
return
|
||||||
@@ -90,9 +93,9 @@ async def handle_key_creation(
|
|||||||
if not tariffs:
|
if not tariffs:
|
||||||
await edit_or_send_message(
|
await edit_or_send_message(
|
||||||
target_message=(
|
target_message=(
|
||||||
message_or_query
|
message_or_query.message
|
||||||
if isinstance(message_or_query, Message)
|
if isinstance(message_or_query, CallbackQuery)
|
||||||
else message_or_query.message
|
else message_or_query
|
||||||
),
|
),
|
||||||
text="❌ Нет доступных тарифов для выбранного кластера.",
|
text="❌ Нет доступных тарифов для выбранного кластера.",
|
||||||
reply_markup=None,
|
reply_markup=None,
|
||||||
@@ -114,6 +117,7 @@ async def handle_key_creation(
|
|||||||
if isinstance(message_or_query, CallbackQuery)
|
if isinstance(message_or_query, CallbackQuery)
|
||||||
else message_or_query
|
else message_or_query
|
||||||
)
|
)
|
||||||
|
|
||||||
await edit_or_send_message(
|
await edit_or_send_message(
|
||||||
target_message=target_message,
|
target_message=target_message,
|
||||||
text=SELECT_TARIFF_PLAN_MSG,
|
text=SELECT_TARIFF_PLAN_MSG,
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ async def create_key_on_cluster(
|
|||||||
session: AsyncSession = None,
|
session: AsyncSession = None,
|
||||||
remnawave_link: str = None,
|
remnawave_link: str = None,
|
||||||
hwid_limit: int = None,
|
hwid_limit: int = None,
|
||||||
|
traffic_limit_bytes: int = None,
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
servers = await get_servers(session, include_enabled=True)
|
servers = await get_servers(session, include_enabled=True)
|
||||||
@@ -59,19 +60,15 @@ async def create_key_on_cluster(
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
traffic_limit_bytes = None
|
if plan is not None and traffic_limit_bytes is None:
|
||||||
if plan is not None:
|
|
||||||
tariff = await get_tariff_by_id(session, plan)
|
tariff = await get_tariff_by_id(session, plan)
|
||||||
if not tariff:
|
if not tariff:
|
||||||
raise ValueError(f"Тариф с id={plan} не найден.")
|
raise ValueError(f"Тариф с id={plan} не найден.")
|
||||||
traffic_limit_bytes = (
|
traffic_limit_bytes = (
|
||||||
int(tariff["traffic_limit"]) if tariff["traffic_limit"] else None
|
int(tariff["traffic_limit"]) if tariff["traffic_limit"] else None
|
||||||
)
|
)
|
||||||
hwid_limit = (
|
if hwid_limit is None and tariff.get("device_limit") is not None:
|
||||||
int(tariff["device_limit"])
|
hwid_limit = int(tariff["device_limit"])
|
||||||
if tariff["device_limit"] is not None
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
remnawave_servers = [
|
remnawave_servers = [
|
||||||
s
|
s
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from config import (
|
|||||||
NOTIFY_INACTIVE,
|
NOTIFY_INACTIVE,
|
||||||
NOTIFY_INACTIVE_TRAFFIC,
|
NOTIFY_INACTIVE_TRAFFIC,
|
||||||
SUPPORT_CHAT_URL,
|
SUPPORT_CHAT_URL,
|
||||||
TRIAL_TIME,
|
TRIAL_CONFIG,
|
||||||
)
|
)
|
||||||
from database import (
|
from database import (
|
||||||
add_notification,
|
add_notification,
|
||||||
@@ -40,6 +40,8 @@ async def notify_inactive_trial_users(bot: Bot, session: AsyncSession):
|
|||||||
logger.info(f"Найдено {len(users)} неактивных пользователей для уведомления.")
|
logger.info(f"Найдено {len(users)} неактивных пользователей для уведомления.")
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
|
trial_days = TRIAL_CONFIG["duration_days"]
|
||||||
|
|
||||||
for user in users:
|
for user in users:
|
||||||
tg_id = user["tg_id"]
|
tg_id = user["tg_id"]
|
||||||
username = user["username"]
|
username = user["username"]
|
||||||
@@ -59,7 +61,7 @@ async def notify_inactive_trial_users(bot: Bot, session: AsyncSession):
|
|||||||
trial_extended = user["last_notification_time"] is not None
|
trial_extended = user["last_notification_time"] is not None
|
||||||
|
|
||||||
if trial_extended:
|
if trial_extended:
|
||||||
total_days = NOTIFY_EXTRA_DAYS + TRIAL_TIME
|
total_days = NOTIFY_EXTRA_DAYS + trial_days
|
||||||
message = TRIAL_INACTIVE_BONUS_MSG.format(
|
message = TRIAL_INACTIVE_BONUS_MSG.format(
|
||||||
display_name=display_name,
|
display_name=display_name,
|
||||||
extra_days_formatted=format_days(NOTIFY_EXTRA_DAYS),
|
extra_days_formatted=format_days(NOTIFY_EXTRA_DAYS),
|
||||||
@@ -68,7 +70,8 @@ async def notify_inactive_trial_users(bot: Bot, session: AsyncSession):
|
|||||||
await mark_trial_extended(tg_id, session)
|
await mark_trial_extended(tg_id, session)
|
||||||
else:
|
else:
|
||||||
message = TRIAL_INACTIVE_FIRST_MSG.format(
|
message = TRIAL_INACTIVE_FIRST_MSG.format(
|
||||||
display_name=display_name, trial_time_formatted=format_days(TRIAL_TIME)
|
display_name=display_name,
|
||||||
|
trial_time_formatted=format_days(trial_days),
|
||||||
)
|
)
|
||||||
|
|
||||||
messages.append(
|
messages.append(
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from sqlalchemy import desc, func, select
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from bot import bot
|
from bot import bot
|
||||||
from config import ADMIN_ID, INLINE_MODE, TOP_REFERRAL_BUTTON, TRIAL_TIME, USERNAME_BOT
|
from config import ADMIN_ID, INLINE_MODE, TOP_REFERRAL_BUTTON, TRIAL_CONFIG, USERNAME_BOT
|
||||||
from database import (
|
from database import (
|
||||||
add_referral,
|
add_referral,
|
||||||
add_user,
|
add_user,
|
||||||
@@ -86,13 +86,16 @@ async def inline_referral_handler(inline_query: InlineQuery):
|
|||||||
referral_link = (
|
referral_link = (
|
||||||
f"https://t.me/{USERNAME_BOT}?start=referral_{inline_query.from_user.id}"
|
f"https://t.me/{USERNAME_BOT}?start=referral_{inline_query.from_user.id}"
|
||||||
)
|
)
|
||||||
trial_time_formatted = format_days(TRIAL_TIME)
|
trial_days = TRIAL_CONFIG["duration_days"]
|
||||||
|
trial_time_formatted = format_days(trial_days)
|
||||||
|
|
||||||
results: list[InlineQueryResultArticle] = []
|
results: list[InlineQueryResultArticle] = []
|
||||||
|
|
||||||
for index, offer in enumerate(REFERRAL_OFFERS):
|
for index, offer in enumerate(REFERRAL_OFFERS):
|
||||||
description = offer["description"][:64]
|
description = offer["description"][:64]
|
||||||
message_text = offer["message"].format(
|
message_text = offer["message"].format(
|
||||||
trial_time=TRIAL_TIME, trial_time_formatted=trial_time_formatted
|
trial_time=trial_days,
|
||||||
|
trial_time_formatted=trial_time_formatted
|
||||||
)[:4096]
|
)[:4096]
|
||||||
|
|
||||||
builder = InlineKeyboardBuilder()
|
builder = InlineKeyboardBuilder()
|
||||||
|
|||||||
Reference in New Issue
Block a user