Spaces:
Running on Zero
Running on Zero
File size: 7,173 Bytes
f09e5c9 1ef6a70 f9fb3c4 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f9fb3c4 1ef6a70 f09e5c9 1ef6a70 f9fb3c4 f09e5c9 1ef6a70 f9fb3c4 1ef6a70 f9fb3c4 f09e5c9 1ef6a70 f9fb3c4 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 f9fb3c4 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f9fb3c4 f09e5c9 f9fb3c4 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 1ef6a70 f09e5c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | import os
from datetime import datetime, timedelta, timezone
from supabase import create_client, Client
from config import SUPABASE_URL, SUPABASE_KEY, API_ID, API_HASH
class Database:
def __init__(self):
self.supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
def add_user(self, user_id: int, name: str):
try:
res = self.supabase.table("users").select("user_id").eq("user_id", user_id).execute()
if not res.data:
self.supabase.table("users").insert({
"user_id": user_id,
"name": name,
"session": None,
"api_id": None,
"api_hash": None,
"plan_expiry": None,
"lang": "hi"
}).execute()
except Exception as e:
print(f"Database add_user error: {e}")
def get_user(self, user_id: int):
try:
res = self.supabase.table("users").select("*").eq("user_id", user_id).execute()
return res.data[0] if res.data else None
except Exception as e:
print(f"Database get_user error: {e}")
return None
def get_all_users(self):
try:
res = self.supabase.table("users").select("*").execute()
return res.data or []
except Exception as e:
print(f"Database get_all_users error: {e}")
return []
def set_session(self, user_id: int, session: str):
try:
self.supabase.table("users").update({"session": session}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database set_session error: {e}")
def get_session(self, user_id: int):
user = self.get_user(user_id)
if user and user.get("session"):
sess = str(user.get("session")).strip()
return sess if len(sess) > 10 else None
return None
def remove_session(self, user_id: int):
try:
self.supabase.table("users").update({"session": None, "api_id": None, "api_hash": None}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database remove_session error: {e}")
def set_api_id(self, user_id: int, api_id: str):
try:
self.supabase.table("users").update({"api_id": str(api_id)}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database set_api_id error: {e}")
def get_api_id(self, user_id: int):
user = self.get_user(user_id)
val = user.get("api_id") if user else None
if val and str(val).strip().isdigit():
return int(str(val).strip())
return API_ID
def set_api_hash(self, user_id: int, api_hash: str):
try:
self.supabase.table("users").update({"api_hash": str(api_hash).strip()}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database set_api_hash error: {e}")
def get_api_hash(self, user_id: int):
user = self.get_user(user_id)
val = user.get("api_hash") if user else None
if val and len(str(val).strip()) >= 10:
return str(val).strip()
return API_HASH
def set_lang(self, user_id: int, lang: str):
try:
self.supabase.table("users").update({"lang": lang}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database set_lang error: {e}")
def get_lang(self, user_id: int):
user = self.get_user(user_id)
return user.get("lang") if (user and user.get("lang")) else "hi"
def add_vip_user(self, user_id: int, days: int):
try:
user = self.get_user(user_id)
now = datetime.now(timezone.utc)
if user and user.get("plan_expiry"):
try:
exp_clean = user["plan_expiry"].replace("Z", "+00:00")
current_exp = datetime.fromisoformat(exp_clean)
if current_exp > now:
new_exp = current_exp + timedelta(days=days)
else:
new_exp = now + timedelta(days=days)
except Exception:
new_exp = now + timedelta(days=days)
else:
new_exp = now + timedelta(days=days)
exp_str = new_exp.isoformat()
if user:
self.supabase.table("users").update({"plan_expiry": exp_str}).eq("user_id", user_id).execute()
else:
self.supabase.table("users").insert({
"user_id": user_id,
"name": "User",
"plan_expiry": exp_str,
"lang": "hi"
}).execute()
except Exception as e:
print(f"Database add_vip_user error: {e}")
def remove_vip_user(self, user_id: int):
try:
self.supabase.table("users").update({"plan_expiry": None}).eq("user_id", user_id).execute()
except Exception as e:
print(f"Database remove_vip_user error: {e}")
def is_user_vip(self, user_id: int, admin_ids):
# एडमिन हमेशा लाइफटाइम वीआईपी हैं
if isinstance(admin_ids, list):
if int(user_id) in [int(x) for x in admin_ids]:
return True
elif int(user_id) == int(admin_ids):
return True
user = self.get_user(user_id)
if not user or not user.get("plan_expiry"):
return False
try:
exp_clean = str(user["plan_expiry"]).replace("Z", "+00:00")
exp = datetime.fromisoformat(exp_clean)
if exp.tzinfo is None:
exp = exp.replace(tzinfo=timezone.utc)
return datetime.now(timezone.utc) < exp
except Exception as e:
print(f"VIP verification error for user {user_id}: {e}")
return False
def get_all_plans(self):
default_plans = {
"1day": "20",
"5days": "50",
"15days": "120",
"30days": "199"
}
try:
res = self.supabase.table("bot_settings").select("key, value").execute()
if res.data:
for row in res.data:
k = row.get("key")
v = row.get("value")
if k in default_plans and v:
default_plans[k] = str(v)
except Exception as e:
print(f"Database get_all_plans error: {e}")
return default_plans
def set_plan_price(self, plan_key: str, price: str):
try:
res = self.supabase.table("bot_settings").select("key").eq("key", plan_key).execute()
if res.data:
self.supabase.table("bot_settings").update({"value": str(price)}).eq("key", plan_key).execute()
else:
self.supabase.table("bot_settings").insert({"key": plan_key, "value": str(price)}).execute()
except Exception as e:
print(f"Database set_plan_price error: {e}")
db = Database()
|