File size: 12,144 Bytes
b70dda2 | 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 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
# --- KONFIGURATSIYA ---
TOKEN = "8623378940:AAFR_EcoSY64xCZAioWwzje6WnhOWF55cUU"
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, "5": 7, "6": 0}
}
if os.path.exists(DB_FILE):
with open(DB_FILE, "r", encoding="utf-8") as f:
return json.load(f)
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 BOTNI SOZLASH ---
# Hugging Face-da ulanish xatolarini oldini olish uchun AiohttpSession qo'shildi
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 HANDLERLAR ---
@dp.message(Command("start"))
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:
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())
@dp.callback_query(F.data == "check_subs")
async def check_subs(callback: types.CallbackQuery):
await callback.answer("Tekshirilmoqda...")
await start_handler(callback.message)
@dp.callback_query(F.data == "broadcast")
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)
@dp.message(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()
@dp.callback_query(F.data == "add_word")
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)
@dp.message(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."); await state.clear()
@dp.callback_query(F.data == "del_word_start")
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)
@dp.message(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."); await state.clear()
@dp.callback_query(F.data == "set_punish")
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())
@dp.callback_query(F.data.startswith("step_"))
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())
@dp.callback_query(F.data.startswith("pday_"))
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()
@dp.callback_query(F.data == "view_stat")
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}")
@dp.callback_query(F.data == "list_groups")
async def list_groups(callback: types.CallbackQuery):
text = "π₯ Bot qo'shilgan guruhlar:\n\n"
for gid in db["groups"]:
try:
chat = await bot.get_chat(gid)
text += f"πΉ {chat.title} (ID: {gid})\n"
except: text += f"πΉ Noma'lum guruh (ID: {gid})\n"
await callback.message.answer(text)
@dp.callback_query(F.data == "add_chan")
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)
@dp.message(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.")
await state.clear()
@dp.callback_query(F.data == "add_admin_id")
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)
@dp.message(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.")
await state.clear()
# --- MODERATSIYA MANTIQI ---
@dp.my_chat_member()
async def on_join(event: types.ChatMemberUpdated):
if event.new_chat_member.status in ["administrator", "member"]:
db["groups"][str(event.chat.id)] = {"stats": 0}; save_db(db)
@dp.message()
async def monitor(message: types.Message):
if not message.chat.type in ["group", "supergroup"]: return
try:
m = await message.chat.get_member(message.from_user.id)
if m.status in ["creator", "administrator"]: return
except: return
if message.new_chat_members or message.left_chat_member:
try: await message.delete()
except: pass
return
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, cid = str(message.from_user.id), str(message.chat.id)
db["users"][uid] = db["users"].get(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"]
await message.delete()
days = db["punish_levels"].get(str(warns), -1)
if days == -1:
msg = await message.answer(f"β οΈ {user_link}, reklama bermang! ({warns}-ogohlantirish)", parse_mode="HTML")
await asyncio.sleep(5); await msg.delete()
else:
until = datetime.now() + timedelta(days=days) if days > 0 else None
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")
# --- VEB SERVER ---
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()) |