Opera8 commited on
Commit
399b0ff
·
verified ·
1 Parent(s): 792260b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +95 -23
main.py CHANGED
@@ -18,9 +18,18 @@ from huggingface_hub import AsyncInferenceClient, HfApi, hf_hub_download
18
  from PIL import Image
19
  from pydub import AudioSegment
20
 
 
 
 
21
  # --- ثبت زمان روشن شدن ربات برای جلوگیری از پرتاب پیام‌های قدیمی ---
22
  BOT_STARTUP_TIME = time.time()
23
 
 
 
 
 
 
 
24
  # --- سیستم دیتابیس حساب کاربری متصل به دیتاست هاگینگ فیس ---
25
  DB_FILE = "users_db.json"
26
  DATASET_REPO = "Opera8/Karbaran-rayegan-tedad"
@@ -86,20 +95,21 @@ def get_user_credits(chat_id):
86
  today_str = datetime.date.today().isoformat()
87
 
88
  if str_chat_id not in user_credits_db:
89
- user_credits_db[str_chat_id] = {}
90
 
91
  user_data = user_credits_db[str_chat_id]
 
92
 
93
- # بازنشانی خودکار سهمیه روزانه
94
- if user_data.get("last_reset") != today_str:
95
  user_data["last_reset"] = today_str
96
- user_data["chat"] = 30
97
- user_data["image"] = 5
98
- user_data["edit_image"] = 3
99
- user_data["podcast"] = 1
100
- user_data["tts"] = 5
101
- user_data["file"] = 5
102
- user_data["stt"] = 5
103
  save_db(user_credits_db)
104
 
105
  return user_data
@@ -119,7 +129,7 @@ app = Flask(__name__)
119
 
120
  @app.route('/')
121
  def home():
122
- return "ربات یکپارچه آلفا (متصل به دیتاست + نسخه پرو + پادکست + ویرایش عکس) روشن است! 🚀"
123
 
124
  def run_flask():
125
  app.run(host="0.0.0.0", port=7860)
@@ -343,7 +353,7 @@ async def process_gemini(client, chat_id, prompt, file_bytes=None, file_name=Non
343
  str_chat_id = str(chat_id)
344
  creds = get_user_credits(str_chat_id)
345
  if creds["chat"] <= 0:
346
- return await send_with_keyboard(client, chat_id, "❌ اعتبار پیام‌های چت شما برای امروز تمام شده است.", False)
347
 
348
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای API جیمینای تنظیم نشده‌اند.", False)
349
 
@@ -440,7 +450,7 @@ async def process_image(client, chat_id, prompt):
440
  str_chat_id = str(chat_id)
441
  creds = get_user_credits(str_chat_id)
442
  if creds["image"] <= 0:
443
- return await send_with_keyboard(client, chat_id, "❌ اعتبار ساخت عکس روزانه شما تمام شده است.", False)
444
 
445
  if not HF_TOKENS: return await send_with_keyboard(client, chat_id, "❌ توکن‌های هاگینگ فیس تنظیم نشده‌اند.", False)
446
 
@@ -516,7 +526,7 @@ async def process_image_edit(client, chat_id, image_bytes, prompt):
516
  str_chat_id = str(chat_id)
517
  creds = get_user_credits(str_chat_id)
518
  if creds["edit_image"] <= 0:
519
- return await send_with_keyboard(client, chat_id, "❌ اعتبار ویرایش عکس روزانه شما تمام شده است.", False)
520
 
521
  if not HF_TOKENS:
522
  return await send_with_keyboard(client, chat_id, "❌ توکن‌های هاگینگ فیس تنظیم نشده‌اند.", False)
@@ -574,7 +584,7 @@ async def process_tts(client, chat_id, user_text, speaker_id, speaker_name):
574
  str_chat_id = str(chat_id)
575
  creds = get_user_credits(str_chat_id)
576
  if creds["tts"] <= 0:
577
- return await send_with_keyboard(client, chat_id, "❌ اعتبار تبدیل متن به صدای شما تمام شده است.", False)
578
 
579
  try:
580
  proc_msg = await send_with_keyboard(client, chat_id, f"⏳ در حال ساخت صدا با «{speaker_name}»...\n(لطفاً صبور باشید)", False)
@@ -625,7 +635,7 @@ async def process_podcast(client, chat_id, prompt):
625
  str_chat_id = str(chat_id)
626
  creds = get_user_credits(str_chat_id)
627
  if creds["podcast"] <= 0:
628
- return await send_with_keyboard(client, chat_id, "❌ اعتبار ساخت پادکست روزانه شما تمام شده است.", False)
629
 
630
  proc_msg = await send_with_keyboard(client, chat_id, "📻 در حال بررسی موضوع و نگارش سناریوی پادکست توسط هوش مصنوعی...\n(لطفاً صبور باشید)", False)
631
  available_speakers = []
@@ -715,7 +725,7 @@ async def process_stt(client, chat_id, audio_bytes, file_name):
715
  str_chat_id = str(chat_id)
716
  creds = get_user_credits(str_chat_id)
717
  if creds["stt"] <= 0:
718
- return await send_with_keyboard(client, chat_id, "❌ اعتبار تبدیل صدا به متن شما تمام شده است.", False)
719
 
720
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای جیمینای تنظیم نشده‌اند.", False)
721
 
@@ -762,7 +772,7 @@ async def process_file_analysis(client, chat_id, file_bytes, file_name, prompt):
762
  str_chat_id = str(chat_id)
763
  creds = get_user_credits(str_chat_id)
764
  if creds["file"] <= 0:
765
- return await send_with_keyboard(client, chat_id, "❌ اعتبار تحلیل فایل روزانه شما تمام شده است.", False)
766
 
767
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلید جیمینای تنظیم نیست.", False)
768
 
@@ -845,14 +855,62 @@ else:
845
 
846
  # 🛡 سیستم محدودکننده سرعت کاربر (Rate Limit) 🛡
847
  last_req_time = user_last_request_time.get(chat_id, 0)
848
- if current_time - last_req_time < 1.5:
849
  return
850
  user_last_request_time[chat_id] = current_time
851
 
852
  # تشخیص محتوای متنی
853
  user_text = getattr(update, "text", "") or getattr(msg_obj, "text", "")
854
  user_text_str = str(user_text).strip() if user_text else ""
 
 
 
 
 
855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
856
  # تشخیص فایل
857
  is_file = False
858
  file_name = "unknown_file.jpg"
@@ -885,10 +943,12 @@ else:
885
  # --- حساب کاربری پویا و متصل به دیتاست ---
886
  if user_text_str in ["/account", "حساب کاربری 👤"]:
887
  creds = get_user_credits(chat_id)
 
 
888
  account_profile = f"""👤 **اطلاعات حساب کاربری شما**
889
 
890
  🔹 **شناسه یکتا:** `{chat_id}`
891
- 🔹 **وضعیت اشتراک:** 🥉 نسخه رایگان (آزمایشی)
892
 
893
  📊 **سهمیه باقی‌مانده شما در امروز:**
894
  - 💬 چت هوشمند: {creds['chat']} پیام
@@ -897,9 +957,21 @@ else:
897
  - 🎙 ساخت پادکست: {creds['podcast']} برنامه
898
  - 🗣 تبدیل متن به صدا: {creds['tts']} فایل
899
  - 📁 تحلیل فایل: {creds['file']} فایل
900
- - 📝 تبدیل صدا به متن: {creds['stt']} فایل
 
 
 
 
 
 
901
 
902
- کته: اطلاعات شما به صورت امن در سرور ابری ذخیره می‌شود و اعتبار شما هر روز ساعت ۰۰:۰۰ مجدداً شارژ می‌گردد.* 🚀"""
 
 
 
 
 
 
903
  await send_with_keyboard(client, chat_id, account_profile, True)
904
  return
905
 
@@ -1052,5 +1124,5 @@ else:
1052
  if __name__ == "__main__":
1053
  threading.Thread(target=run_flask, daemon=True).start()
1054
  if bot_token:
1055
- print("ربات آلفا با موفقیت به همراه دیتابیس ابری (HF Datasets) روشن شد...")
1056
  bot.run()
 
18
  from PIL import Image
19
  from pydub import AudioSegment
20
 
21
+ # --- رمز عبور ادمین برای فعال‌سازی اشتراک‌ها ---
22
+ ADMIN_SECRET = "hmd744_admin"
23
+
24
  # --- ثبت زمان روشن شدن ربات برای جلوگیری از پرتاب پیام‌های قدیمی ---
25
  BOT_STARTUP_TIME = time.time()
26
 
27
+ # --- تنظیمات سهمیه اکانت‌ها ---
28
+ PLAN_LIMITS = {
29
+ "free": {"chat": 10, "image": 5, "edit_image": 1, "podcast": 1, "tts": 5, "file": 1, "stt": 5},
30
+ "premium": {"chat": 100, "image": 50, "edit_image": 10, "podcast": 50, "tts": 100, "file": 20, "stt": 50}
31
+ }
32
+
33
  # --- سیستم دیتابیس حساب کاربری متصل به دیتاست هاگینگ فیس ---
34
  DB_FILE = "users_db.json"
35
  DATASET_REPO = "Opera8/Karbaran-rayegan-tedad"
 
95
  today_str = datetime.date.today().isoformat()
96
 
97
  if str_chat_id not in user_credits_db:
98
+ user_credits_db[str_chat_id] = {"plan": "free"}
99
 
100
  user_data = user_credits_db[str_chat_id]
101
+ current_plan = user_data.get("plan", "free")
102
 
103
+ # بازنشانی خودکار سهمیه روزانه یا در صورت تغییر پلن
104
+ if user_data.get("last_reset") != today_str or user_data.get("last_plan_set") != current_plan:
105
  user_data["last_reset"] = today_str
106
+ user_data["last_plan_set"] = current_plan
107
+
108
+ # اعمال محدودیت‌ها بر اساس نوع اکانت
109
+ limits = PLAN_LIMITS.get(current_plan, PLAN_LIMITS["free"])
110
+ for key, val in limits.items():
111
+ user_data[key] = val
112
+
113
  save_db(user_credits_db)
114
 
115
  return user_data
 
129
 
130
  @app.route('/')
131
  def home():
132
+ return "ربات یکپارچه آلفا (متصل به دیتاست + اشتراک ویژه + پنل ادمین + پادکست + ویرایش عکس) روشن است! 🚀"
133
 
134
  def run_flask():
135
  app.run(host="0.0.0.0", port=7860)
 
353
  str_chat_id = str(chat_id)
354
  creds = get_user_credits(str_chat_id)
355
  if creds["chat"] <= 0:
356
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار پیام‌های چت شما برای امروز تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
357
 
358
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای API جیمینای تنظیم نشده‌اند.", False)
359
 
 
450
  str_chat_id = str(chat_id)
451
  creds = get_user_credits(str_chat_id)
452
  if creds["image"] <= 0:
453
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار ساخت عکس روزانه شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
454
 
455
  if not HF_TOKENS: return await send_with_keyboard(client, chat_id, "❌ توکن‌های هاگینگ فیس تنظیم نشده‌اند.", False)
456
 
 
526
  str_chat_id = str(chat_id)
527
  creds = get_user_credits(str_chat_id)
528
  if creds["edit_image"] <= 0:
529
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار ویرایش عکس روزانه شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
530
 
531
  if not HF_TOKENS:
532
  return await send_with_keyboard(client, chat_id, "❌ توکن‌های هاگینگ فیس تنظیم نشده‌اند.", False)
 
584
  str_chat_id = str(chat_id)
585
  creds = get_user_credits(str_chat_id)
586
  if creds["tts"] <= 0:
587
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار تبدیل متن به صدای شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
588
 
589
  try:
590
  proc_msg = await send_with_keyboard(client, chat_id, f"⏳ در حال ساخت صدا با «{speaker_name}»...\n(لطفاً صبور باشید)", False)
 
635
  str_chat_id = str(chat_id)
636
  creds = get_user_credits(str_chat_id)
637
  if creds["podcast"] <= 0:
638
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار ساخت پادکست روزانه شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
639
 
640
  proc_msg = await send_with_keyboard(client, chat_id, "📻 در حال بررسی موضوع و نگارش سناریوی پادکست توسط هوش مصنوعی...\n(لطفاً صبور باشید)", False)
641
  available_speakers = []
 
725
  str_chat_id = str(chat_id)
726
  creds = get_user_credits(str_chat_id)
727
  if creds["stt"] <= 0:
728
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار تبدیل صدا به متن شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
729
 
730
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلیدهای جیمینای تنظیم نشده‌اند.", False)
731
 
 
772
  str_chat_id = str(chat_id)
773
  creds = get_user_credits(str_chat_id)
774
  if creds["file"] <= 0:
775
+ return await send_with_keyboard(client, chat_id, "❌ اعتبار تحلیل فایل روزانه شما تمام شده است.\nبرای ارتقا به بخش «حساب کاربری 👤» مراجعه کنید.", False)
776
 
777
  if not GEMINI_KEYS: return await send_with_keyboard(client, chat_id, "❌ کلید جیمینای تنظیم نیست.", False)
778
 
 
855
 
856
  # 🛡 سیستم محدودکننده سرعت کاربر (Rate Limit) 🛡
857
  last_req_time = user_last_request_time.get(chat_id, 0)
858
+ if current_time - last_req_time < 1.0:
859
  return
860
  user_last_request_time[chat_id] = current_time
861
 
862
  # تشخیص محتوای متنی
863
  user_text = getattr(update, "text", "") or getattr(msg_obj, "text", "")
864
  user_text_str = str(user_text).strip() if user_text else ""
865
+ str_chat_id = str(chat_id)
866
+
867
+ # ==========================================
868
+ # 🔐 بخش ادمین و مدیریت اشتراک‌ها 🔐
869
+ # ==========================================
870
 
871
+ # ۱. گرفتن دسترسی ادمین با رمز
872
+ if user_text_str == f"/adminlogin {ADMIN_SECRET}":
873
+ if "_ADMINS_" not in user_credits_db:
874
+ user_credits_db["_ADMINS_"] = []
875
+ if str_chat_id not in user_credits_db["_ADMINS_"]:
876
+ user_credits_db["_ADMINS_"].append(str_chat_id)
877
+ save_db(user_credits_db)
878
+ await send_with_keyboard(client, chat_id, "✅ دسترسی مدیریت (پنل ادمین) برای شما فعال شد.\n\nدستورات مجاز:\n`/premium [شناسه_کاربر]` 👈 فعال‌سازی اشتراک\n`/free [شناسه_کاربر]` 👈 لغو اشتراک", True)
879
+ return
880
+
881
+ is_admin = str_chat_id in user_credits_db.get("_ADMINS_", [])
882
+
883
+ # ۲. دستور فعال‌سازی اشتراک توسط ادمین
884
+ if is_admin and user_text_str.startswith("/premium "):
885
+ target_id = user_text_str.replace("/premium ", "").strip()
886
+ if target_id not in user_credits_db:
887
+ user_credits_db[target_id] = {"plan": "free"}
888
+ user_credits_db[target_id]["plan"] = "premium"
889
+ user_credits_db[target_id]["last_reset"] = "" # ریست اجباری سهمیه‌ها برای اعمال آنی
890
+ save_db(user_credits_db)
891
+ await send_with_keyboard(client, chat_id, f"✅ اشتراک پرو برای کاربر {target_id} با موفقیت فعال شد.", True)
892
+ try:
893
+ await client.send_message(target_id, "🎉 **تبریک!**\nاشتراک ویژه (نسخه پرو) شما توسط مدیریت فعال شد. هم‌اکنون می‌توانید از تمام امکانات پیشرفته و نامحدود ربات لذت ببرید.")
894
+ except: pass
895
+ return
896
+
897
+ # ۳. دستور لغو اشتراک توسط ادمین
898
+ if is_admin and user_text_str.startswith("/free "):
899
+ target_id = user_text_str.replace("/free ", "").strip()
900
+ if target_id in user_credits_db:
901
+ user_credits_db[target_id]["plan"] = "free"
902
+ user_credits_db[target_id]["last_reset"] = ""
903
+ save_db(user_credits_db)
904
+ await send_with_keyboard(client, chat_id, f"✅ اکانت کاربر {target_id} به حالت رایگان برگشت داده شد.", True)
905
+ try:
906
+ await client.send_message(target_id, "⚠️ **توجه:**\nاشتراک ویژه شما به پایان رسید و حساب شما به نسخه رایگان (محدود) تغییر یافت.")
907
+ except: pass
908
+ return
909
+
910
+ # ==========================================
911
+ # پایان بخش ادمین
912
+ # ==========================================
913
+
914
  # تشخیص فایل
915
  is_file = False
916
  file_name = "unknown_file.jpg"
 
943
  # --- حساب کاربری پویا و متصل به دیتاست ---
944
  if user_text_str in ["/account", "حساب کاربری 👤"]:
945
  creds = get_user_credits(chat_id)
946
+ plan_status = "🌟 نسخه پرو (اشتراک ویژه فعال)" if creds.get("plan") == "premium" else "🥉 نسخه رایگان (آزمایشی)"
947
+
948
  account_profile = f"""👤 **اطلاعات حساب کاربری شما**
949
 
950
  🔹 **شناسه یکتا:** `{chat_id}`
951
+ 🔹 **وضعیت اشتراک:** {plan_status}
952
 
953
  📊 **سهمیه باقی‌مانده شما در امروز:**
954
  - 💬 چت هوشمند: {creds['chat']} پیام
 
957
  - 🎙 ساخت پادکست: {creds['podcast']} برنامه
958
  - 🗣 تبدیل متن به صدا: {creds['tts']} فایل
959
  - 📁 تحلیل فایل: {creds['file']} فایل
960
+ - 📝 تبدیل صدا به متن: {creds['stt']} فایل"""
961
+
962
+ if creds.get("plan") != "premium":
963
+ account_profile += """
964
+
965
+ 💎 **ارتقا به نسخه پرو (نامحدود):**
966
+ با ارتقا به نسخه پرو امکانات شما بی‌نهایت افزایش یافته و محدودیت‌ها برداشته می‌شود.
967
 
968
+ 💰 **هزینه اشتراک ماهانه:** 199 هزار تومان
969
+ 💳 **شماره کارت:** `6104338667771394`
970
+
971
+ جهت فعال‌سازی، لطفاً «رسید واریز» و «شناسه یکتا» خود را به پشتیبانی تلگرام/روبیکا ارسال کنید:
972
+ 👨‍💻 **پشتیبانی:** @H_a_m_e_d100
973
+ *(پس از ارسال رسید، اشتراک شما سریعاً فعال می‌گردد)*"""
974
+
975
  await send_with_keyboard(client, chat_id, account_profile, True)
976
  return
977
 
 
1124
  if __name__ == "__main__":
1125
  threading.Thread(target=run_flask, daemon=True).start()
1126
  if bot_token:
1127
+ print("ربات آلفا با موفقیت به همراه دیتابیس ابری (HF Datasets) و پنل ادمین روشن شد...")
1128
  bot.run()