Download bot.py from Maqsadov/mmmm: direct link, hf CLI and curl.
- Browser
- Download file 14 kB
-
https://huggingface.co/spaces/Maqsadov/mmmm/resolve/main/bot.py
- Command line
-
hf download hf://spaces/Maqsadov/mmmm/bot.py
-
curl -L -o bot.py https://huggingface.co/spaces/Maqsadov/mmmm/resolve/main/bot.py
14 kB
| import asyncio | |
| import json | |
| import os | |
| import re | |
| from datetime import datetime, timedelta | |
| from aiohttp import web | |
| from aiogram import Bot, Dispatcher, types, F | |
| from aiogram.filters import Command | |
| from aiogram.types import ChatPermissions, InlineKeyboardButton | |
| from aiogram.utils.keyboard import InlineKeyboardBuilder | |
| from aiogram.fsm.state import State, StatesGroup | |
| from aiogram.fsm.context import FSMContext | |
| from aiogram.utils.markdown import hlink | |
| from aiogram.client.session.aiohttp import AiohttpSession # Server xatosini to'g'rilash uchun | |
| # --- KONFIGURATSIYA --- | |
| TOKEN = "8792076655:AAGzgsYt2HVRaZfIevHC-zEpzo4Y6viJBSM" | |
| OWNER_ID = 5745296238 | |
| DB_FILE = "database.json" | |
| def load_db(): | |
| default = { | |
| "groups": {}, | |
| "users": {}, | |
| "channels": [], | |
| "extra_admins": [], | |
| "global_words": ["kazino", "bonus", "stavka", "sex", "qizlar"], | |
| "punish_levels": {"4": 1.0, "5": 7.0, "6": 0.0} | |
| } | |
| if os.path.exists(DB_FILE): | |
| try: | |
| with open(DB_FILE, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| # Bazada yetishmayotgan kalitlarni to'ldirish | |
| for k, v in default.items(): | |
| if k not in data: data[k] = v | |
| return data | |
| except: return default | |
| return default | |
| def save_db(data): | |
| with open(DB_FILE, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=4, ensure_ascii=False) | |
| db = load_db() | |
| # --- SESSIYA VA BOT --- | |
| session = AiohttpSession() | |
| bot = Bot(token=TOKEN, session=session) | |
| dp = Dispatcher() | |
| class AdminState(StatesGroup): | |
| waiting_for_word = State() | |
| waiting_for_del_word = State() | |
| waiting_for_broadcast = State() | |
| waiting_for_channel = State() | |
| waiting_for_admin_id = State() | |
| waiting_for_punish_step = State() | |
| waiting_for_punish_days = State() | |
| def is_admin(user_id): | |
| return user_id == OWNER_ID or user_id in db["extra_admins"] | |
| def get_admin_keyboard(): | |
| builder = InlineKeyboardBuilder() | |
| builder.row(InlineKeyboardButton(text="β So'z qo'shish", callback_data="add_word"), | |
| InlineKeyboardButton(text="β So'z o'chirish", callback_data="del_word_start")) | |
| builder.row(InlineKeyboardButton(text="π So'zlar", callback_data="list_words"), | |
| InlineKeyboardButton(text="π Stat", callback_data="view_stat")) | |
| builder.row(InlineKeyboardButton(text="π’ Xabar yuborish", callback_data="broadcast"), | |
| InlineKeyboardButton(text="π₯ Guruhlar", callback_data="list_groups")) | |
| builder.row(InlineKeyboardButton(text="β Kanal qo'shish", callback_data="add_chan"), | |
| InlineKeyboardButton(text="βοΈ Jazo sozlash", callback_data="set_punish")) | |
| builder.row(InlineKeyboardButton(text="π€ Admin qo'shish", callback_data="add_admin_id")) | |
| return builder.as_markup() | |
| # --- ADMIN VA START HANDLERLAR --- | |
| async def start_handler(message: types.Message): | |
| if message.chat.type != "private": return | |
| if is_admin(message.from_user.id): | |
| await message.answer("π» **Admin Panelga xush kelibsiz!**", reply_markup=get_admin_keyboard(), parse_mode="Markdown") | |
| else: | |
| # Majburiy obuna tekshiruvi | |
| not_joined = [] | |
| for channel in db["channels"]: | |
| try: | |
| member = await bot.get_chat_member(channel, message.from_user.id) | |
| if member.status in ["left", "kicked"]: not_joined.append(channel) | |
| except: pass | |
| if not_joined: | |
| kb = InlineKeyboardBuilder() | |
| for ch in not_joined: | |
| kb.row(InlineKeyboardButton(text=f"Obuna bo'lish", url=f"https://t.me/{ch.replace('@', '')}")) | |
| kb.row(InlineKeyboardButton(text="Tekshirish β ", callback_data="check_subs")) | |
| return await message.answer("Botdan foydalanish uchun kanallarga a'zo bo'ling:", reply_markup=kb.as_markup()) | |
| bot_info = await bot.get_me() | |
| add_url = f"https://t.me/{bot_info.username}?startgroup=true" | |
| kb = InlineKeyboardBuilder() | |
| kb.row(InlineKeyboardButton(text="β Guruhga qo'shish", url=add_url)) | |
| await message.answer("Salom! Men guruhlarni reklamadan himoya qilaman.", reply_markup=kb.as_markup()) | |
| async def check_subs(callback: types.CallbackQuery): | |
| await callback.answer("Tekshirilmoqda...") | |
| await start_handler(callback.message) | |
| # --- ADMIN FUNKSIYALARI --- | |
| async def broadcast_start(callback: types.CallbackQuery, state: FSMContext): | |
| await callback.message.answer("Guruhlarga yuboriladigan xabarni yozing:") | |
| await state.set_state(AdminState.waiting_for_broadcast) | |
| async def process_broadcast(message: types.Message, state: FSMContext): | |
| count = 0 | |
| for gid in list(db["groups"].keys()): | |
| try: | |
| await bot.send_message(chat_id=int(gid), text=message.text) | |
| count += 1 | |
| await asyncio.sleep(0.1) | |
| except: continue | |
| await message.answer(f"β Xabar {count} ta guruhga yuborildi.") | |
| await state.clear() | |
| async def add_word(callback: types.CallbackQuery, state: FSMContext): | |
| await callback.message.answer("Taqiqlangan so'zni yuboring:") | |
| await state.set_state(AdminState.waiting_for_word) | |
| async def proc_word(message: types.Message, state: FSMContext): | |
| word = message.text.lower().strip() | |
| if word not in db["global_words"]: | |
| db["global_words"].append(word) | |
| save_db(db) | |
| await message.answer(f"β '{word}' qo'shildi.") | |
| else: | |
| await message.answer("β οΈ Bu so'z allaqachon bor.") | |
| await state.clear() | |
| async def del_word(callback: types.CallbackQuery, state: FSMContext): | |
| await callback.message.answer("O'chirmoqchi bo'lgan so'zni yozing:") | |
| await state.set_state(AdminState.waiting_for_del_word) | |
| async def proc_del(message: types.Message, state: FSMContext): | |
| word = message.text.lower().strip() | |
| if word in db["global_words"]: | |
| db["global_words"].remove(word) | |
| save_db(db) | |
| await message.answer(f"π '{word}' o'chirildi.") | |
| else: | |
| await message.answer("β οΈ Bunday so'z topilmadi.") | |
| await state.clear() | |
| async def list_words_cb(callback: types.CallbackQuery): | |
| words = ", ".join(db["global_words"]) | |
| await callback.message.answer(f"π Taqiqlangan so'zlar:\n\n{words}") | |
| async def punish_start(callback: types.CallbackQuery): | |
| builder = InlineKeyboardBuilder() | |
| for i in range(1, 11): | |
| builder.add(InlineKeyboardButton(text=str(i), callback_data=f"step_{i}")) | |
| builder.adjust(5) | |
| await callback.message.edit_text("Nechanchi ogohlantirish jazosini o'zgartiramiz?", reply_markup=builder.as_markup()) | |
| async def proc_p_step(callback: types.CallbackQuery, state: FSMContext): | |
| step = callback.data.split("_")[1] | |
| await state.update_data(step=step) | |
| builder = InlineKeyboardBuilder() | |
| builder.row(InlineKeyboardButton(text="π« Faqat ogohlantirish", callback_data="pday_-1")) | |
| builder.row(InlineKeyboardButton(text="β³ 1 kun mute", callback_data="pday_1")) | |
| builder.row(InlineKeyboardButton(text="β³ 3 kun mute", callback_data="pday_3")) | |
| builder.row(InlineKeyboardButton(text="β³ 7 kun mute", callback_data="pday_7")) | |
| builder.row(InlineKeyboardButton(text="π· Abadiy blok", callback_data="pday_0")) | |
| await callback.message.edit_text(f"{step}-ogohlantirish uchun jazo turini tanlang:", reply_markup=builder.as_markup()) | |
| async def proc_p_days(callback: types.CallbackQuery, state: FSMContext): | |
| day_val = float(callback.data.split("_")[1]) | |
| data = await state.get_data() | |
| db["punish_levels"][str(data['step'])] = day_val | |
| save_db(db) | |
| await callback.message.edit_text(f"β Saqlandi: {data['step']}-ogohlantirish uchun jazo yangilandi.") | |
| await state.clear() | |
| async def view_stat(callback: types.CallbackQuery): | |
| total_g = len(db["groups"]) | |
| total_s = sum(g.get("stats", 0) for g in db["groups"].values()) | |
| await callback.message.answer(f"π Statistika:\n- Guruhlar: {total_g}\n- O'chirilgan reklamalar: {total_s}") | |
| async def list_groups(callback: types.CallbackQuery): | |
| if not db["groups"]: | |
| return await callback.message.answer("β οΈ Bot hali hech qanday guruhga qo'shilmagan.") | |
| text = "π₯ Bot qo'shilgan guruhlar:\n\n" | |
| for gid in db["groups"]: | |
| try: | |
| chat = await bot.get_chat(int(gid)) | |
| text += f"πΉ {chat.title} (ID: {gid})\n" | |
| except: | |
| text += f"πΉ Noma'lum guruh (ID: {gid})\n" | |
| await callback.message.answer(text) | |
| async def add_chan_start(callback: types.CallbackQuery, state: FSMContext): | |
| await callback.message.answer("Kanal userneymini yuboring (@ bilan):") | |
| await state.set_state(AdminState.waiting_for_channel) | |
| async def proc_chan(message: types.Message, state: FSMContext): | |
| if message.text.startswith("@"): | |
| db["channels"].append(message.text) | |
| save_db(db) | |
| await message.answer("β Kanal qo'shildi.") | |
| else: | |
| await message.answer("β οΈ Format noto'g'ri. @ bilan yuboring.") | |
| await state.clear() | |
| async def add_admin_start(callback: types.CallbackQuery, state: FSMContext): | |
| await callback.message.answer("Yangi admin ID sini yuboring:") | |
| await state.set_state(AdminState.waiting_for_admin_id) | |
| async def proc_admin(message: types.Message, state: FSMContext): | |
| if message.text.isdigit(): | |
| db["extra_admins"].append(int(message.text)) | |
| save_db(db) | |
| await message.answer("β Admin qo'shildi.") | |
| else: | |
| await message.answer("β οΈ ID faqat raqamlardan iborat bo'lishi kerak.") | |
| await state.clear() | |
| # --- MODERATSIYA MANTIQI --- | |
| async def on_join(event: types.ChatMemberUpdated): | |
| if event.new_chat_member.status in ["administrator", "member"]: | |
| cid = str(event.chat.id) | |
| if cid not in db["groups"]: | |
| db["groups"][cid] = {"stats": 0} | |
| save_db(db) | |
| async def monitor(message: types.Message): | |
| if message.chat.type not in ["group", "supergroup"]: return | |
| # Guruh bazada bo'lmasa, qo'shib qo'yamiz (list_groups ishlashi uchun) | |
| cid = str(message.chat.id) | |
| if cid not in db["groups"]: | |
| db["groups"][cid] = {"stats": 0} | |
| save_db(db) | |
| # Adminlarni e'tiborsiz qoldirish | |
| try: | |
| m = await message.chat.get_member(message.from_user.id) | |
| if m.status in ["creator", "administrator"]: return | |
| except: return | |
| # "Kirdi-chiqdi" xabarlarni o'chirish | |
| if message.new_chat_members or message.left_chat_member: | |
| try: await message.delete() | |
| except: pass | |
| return | |
| # Reklama/So'z tekshiruvi | |
| text = message.text or message.caption or "" | |
| is_spam = any(w in text.lower() for w in db["global_words"]) or re.search(r'(https?://|t\.me/|@[\w_]+)', text) | |
| if is_spam: | |
| uid = str(message.from_user.id) | |
| # User va guruh statistikasini yuritish | |
| if uid not in db["users"]: db["users"][uid] = {"warns": 0} | |
| db["users"][uid]["warns"] += 1 | |
| db["groups"][cid]["stats"] = db["groups"].get(cid, {}).get("stats", 0) + 1 | |
| save_db(db) | |
| user_link = f"@{message.from_user.username}" if message.from_user.username else hlink(message.from_user.full_name, f"tg://user?id={message.from_user.id}") | |
| warns = db["users"][uid]["warns"] | |
| try: await message.delete() | |
| except: pass | |
| # Jazo turini aniqlash va jazolash | |
| days = db["punish_levels"].get(str(warns), -1.0) | |
| if days == -1.0: | |
| msg = await message.answer(f"β οΈ {user_link}, reklama bermang! ({warns}-ogohlantirish)", parse_mode="HTML") | |
| await asyncio.sleep(5) # Xabarni 5 soniyadan keyin o'chirish | |
| try: await msg.delete() | |
| except: pass | |
| else: | |
| until = datetime.now() + timedelta(days=days) if days > 0 else None | |
| try: | |
| await message.chat.restrict(uid, permissions=ChatPermissions(can_send_messages=False), until_date=until) | |
| status = f"{days} kunga" if days > 0 else "abadiy" | |
| await message.answer(f"π {user_link} {status} bloklandi (Ogohlantirish: {warns}).", parse_mode="HTML") | |
| except: | |
| pass # Botda admin huquqi bo'lmasa xato tashlamaydi | |
| # --- VEB SERVER VA ISHGA TUSHIRISH --- | |
| async def handle(request): | |
| return web.Response(text="Bot 24/7 ishlamoqda!") | |
| async def web_server(): | |
| app = web.Application() | |
| app.router.add_get('/', handle) | |
| runner = web.AppRunner(app) | |
| await runner.setup() | |
| port = int(os.environ.get("PORT", 10000)) | |
| site = web.TCPSite(runner, '0.0.0.0', port) | |
| await site.start() | |
| async def main(): | |
| print("Bot 24/7 rejimda ishga tushdi...") | |
| asyncio.create_task(web_server()) | |
| await dp.start_polling(bot) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |