-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
148 lines (116 loc) · 4.67 KB
/
bot.py
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
from aiogram import Bot, Dispatcher
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart,Command
from aiogram import F
from aiogram.types import Message,InlineKeyboardButton
from data import config
import asyncio
import logging
import sys
from menucommands.set_bot_commands import set_default_commands
from baza.sqlite import Database
from filters.admin import IsBotAdminFilter
from filters.check_sub_channel import IsCheckSubChannels
from keyboard_buttons import admin_keyboard
from aiogram.fsm.context import FSMContext #new
from states.reklama import Adverts
from aiogram.utils.keyboard import InlineKeyboardBuilder
import time
from ism_manosi import ism_manosi_funksiyasi
ADMINS = config.ADMINS
TOKEN = config.BOT_TOKEN
CHANNELS = config.CHANNELS
dp = Dispatcher()
@dp.message(CommandStart())
async def start_command(message:Message):
full_name = message.from_user.full_name
telegram_id = message.from_user.id
try:
db.add_user(full_name=full_name,telegram_id=telegram_id) #foydalanuvchi bazaga qo'shildi
await message.answer(text="Assalomu alaykum, Bu bot ismingizni manosini topib beradi.\nBotdan foydalanish uchun ismingizni kiriting!")
except:
await message.answer(text="Assalomu alaykum")
#help commands
@dp.message(Command("help"))
async def help_commands(message:Message):
await message.answer("Sizga qanday yordam kerak")
#about commands
@dp.message(Command("about"))
async def about_commands(message:Message):
await message.answer("Bot ismlar ma'nosini topib berad!")
@dp.message(Command("admin"),IsBotAdminFilter(ADMINS))
async def is_admin(message:Message):
await message.answer(text="Admin menu",reply_markup=admin_keyboard.admin_button)
@dp.message(F.text=="Foydalanuvchilar soni",IsBotAdminFilter(ADMINS))
async def users_count(message:Message):
counts = db.count_users()
text = f"Botimizda {counts[0]} ta foydalanuvchi bor"
await message.answer(text=text)
@dp.message(F.text=="Reklama yuborish",IsBotAdminFilter(ADMINS))
async def advert_dp(message:Message,state:FSMContext):
await state.set_state(Adverts.adverts)
await message.answer(text="Reklama yuborishingiz mumkin !")
@dp.message(Adverts.adverts)
async def send_advert(message:Message,state:FSMContext):
message_id = message.message_id
from_chat_id = message.from_user.id
users = db.all_users_id()
count = 0
for user in users:
try:
await bot.copy_message(chat_id=user[0],from_chat_id=from_chat_id,message_id=message_id)
count += 1
except:
pass
time.sleep(0.01)
await message.answer(f"Reklama {count}ta foydalanuvchiga yuborildi")
await state.clear()
@dp.message(F.text)
async def ismmanosi(message:Message):
ism = message.text
ism = ism.replace("'","‘")
manosi = ism_manosi_funksiyasi(ism=ism)
text = f"✨ {ism} ma'nosi :\n{manosi}"
if manosi == False:
text = "Afsuski topilmadi 😢"
await message.answer(text=text)
@dp.message(IsCheckSubChannels())
async def kanalga_obuna(message:Message):
text = ""
inline_channel = InlineKeyboardBuilder()
for index,channel in enumerate(CHANNELS):
ChatInviteLink = await bot.create_chat_invite_link(channel)
inline_channel.add(InlineKeyboardButton(text=f"{index+1}-kanal",url=ChatInviteLink.invite_link))
inline_channel.adjust(1,repeat=True)
button = inline_channel.as_markup()
await message.answer(f"{text} kanallarga azo bo'ling",reply_markup=button)
@dp.startup()
async def on_startup_notify(bot: Bot):
for admin in ADMINS:
try:
await bot.send_message(chat_id=int(admin),text="Bot ishga tushdi")
except Exception as err:
logging.exception(err)
#bot ishga tushganini xabarini yuborish
@dp.shutdown()
async def off_startup_notify(bot: Bot):
for admin in ADMINS:
try:
await bot.send_message(chat_id=int(admin),text="Bot ishdan to'xtadi!")
except Exception as err:
logging.exception(err)
def setup_middlewares(dispatcher: Dispatcher, bot: Bot) -> None:
"""MIDDLEWARE"""
from middlewares.throttling import ThrottlingMiddleware
# Spamdan himoya qilish uchun klassik ichki o'rta dastur. So'rovlar orasidagi asosiy vaqtlar 0,5 soniya
dispatcher.message.middleware(ThrottlingMiddleware(slow_mode_delay=0.5))
async def main() -> None:
global bot,db
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
db = Database(path_to_db="main.db")
await set_default_commands(bot)
await dp.start_polling(bot)
setup_middlewares(dispatcher=dp, bot=bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())