-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathbot.py
More file actions
182 lines (173 loc) · 7.11 KB
/
bot.py
File metadata and controls
182 lines (173 loc) · 7.11 KB
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
import sys
import time
import traceback
from datetime import datetime
import asyncio
from asyncio import sleep
from pyrogram import idle, __version__
from pyrogram.raw.all import layer
from pyrogram.errors import FloodWait
from pytz import timezone
from pathlib import Path
import importlib.util
from aiohttp import web
from PIL import Image
Image.MAX_IMAGE_PIXELS = 500_000_000
from database.ia_filterdb import Media, Media2
from database.users_chats_db import db
from info import *
from utils import temp
from Script import script
import plugins.cover
from plugins.route import routes
from web import techifybots
from web.clients import initialize_clients
import logging
import logging.config
logging.basicConfig(level=logging.INFO)
logging.config.fileConfig('logging.conf')
logging.getLogger().setLevel(logging.INFO)
logging.getLogger("pyrogram").setLevel(logging.ERROR)
logging.getLogger("imdbpy").setLevel(logging.ERROR)
logging.getLogger("aiohttp").setLevel(logging.ERROR)
logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
logging.getLogger("pymongo").setLevel(logging.WARNING)
botStartTime = time.time()
async def web_server():
web_app = web.Application(client_max_size=30000000)
web_app.add_routes(routes)
return web_app
async def check_expired_premium(client):
while 1:
data = await db.get_expired(datetime.now())
for user in data:
user_id = user["id"]
await db.remove_premium_access(user_id)
try:
user = await client.get_users(user_id)
await client.send_message(
chat_id=user_id,
text=f"<b>{user.mention},\n\nʏᴏᴜʀ ᴘʀᴇᴍɪᴜᴍ ᴀᴄᴄᴇss ʜᴀs ᴇxᴘɪʀᴇᴅ.\nᴄʜᴇᴄᴋ /plan ᴛᴏ ᴠɪᴇᴡ ᴏᴜʀ ᴄᴜʀʀᴇɴᴛ ᴘʟᴀɴs.</b>"
)
await client.send_message(PREMIUM_LOGS, text=f"<b>#Premium_Expire\n\nUser name: {user.mention}\nUser id: <code>{user_id}</code>")
except Exception as e:
print(e)
await sleep(0.5)
await sleep(1)
async def keep_alive():
async with aiohttp.ClientSession() as session:
while True:
await asyncio.sleep(298)
try:
async with session.get(URL) as resp:
if resp.status != 200:
logging.warning(f"⚠️ Ping Error! Status: {resp.status}")
except Exception as e:
logging.error(f"❌ Ping Failed: {e}")
async def ping_server():
sleep_time = PING_INTERVAL
while True:
await asyncio.sleep(sleep_time)
try:
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=10)
) as session:
async with session.get(URL) as resp:
logging.info("Pinged server with response: {}".format(resp.status))
except TimeoutError:
logging.warning("Couldn't connect to the site URL..!")
except Exception:
traceback.print_exc()
def techifybots_plugins_handler(app, plugins_dir: str | Path = "plugins", package_name: str = "plugins") -> list[str]:
plugins_dir = Path(plugins_dir)
loaded_plugins: list[str] = []
if not plugins_dir.exists():
logging.warning("Plugins Directory '%s' Does Not Exist.", plugins_dir)
return loaded_plugins
for file in sorted(plugins_dir.rglob("*.py")):
if file.name == "__init__.py":
continue
rel_path = file.relative_to(plugins_dir).with_suffix("")
import_path = package_name + ".".join([""] + list(rel_path.parts))
try:
spec = importlib.util.spec_from_file_location(import_path, file)
if spec is None or spec.loader is None:
logging.warning("Skipping %s (No Spec/Loader).", file)
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules[import_path] = module
loaded_plugins.append(import_path)
short_name = import_path.removeprefix(f"{package_name}.")
logging.info("🔌 Loaded plugin: %s", short_name)
except Exception:
logging.exception("Failed To Import Plugin: %s", import_path)
disp = getattr(app, "dispatcher", None)
if disp is None:
logging.warning("App Has No Dispatcher; Skipping Handler Regroup.")
return loaded_plugins
if 0 in disp.groups:
all_handlers = list(disp.groups[0])
for i, handler in enumerate(all_handlers):
disp.remove_handler(handler, group=0)
disp.add_handler(handler, group=i)
else:
logging.info("No Handlers In Group 0; Nothing To Regroup.")
return loaded_plugins
async def techifybots_start():
print('\n\nInitalizing ')
await techifybots.start()
bot_info = await techifybots.get_me()
techifybots.username = bot_info.username
await initialize_clients()
loaded_plugins = techifybots_plugins_handler(techifybots)
if loaded_plugins:
logging.info("✅ Plugins Loaded: %d", len(loaded_plugins))
else:
logging.info("⚠️ No Plugins Loaded.")
if ON_HEROKU:
asyncio.create_task(ping_server())
b_users, b_chats = await db.get_banned()
temp.BANNED_USERS = b_users
temp.BANNED_CHATS = b_chats
await Media.ensure_indexes()
if MULTIPLE_DB:
await Media2.ensure_indexes()
print("Multiple Database Mode On. Now Files Will Be Save In Second DB If First DB Is Full")
else:
print("Single DB Mode On ! Files Will Be Save In First Database")
me = await techifybots.get_me()
temp.ME = me.id
temp.U_NAME = me.username
temp.B_NAME = me.first_name
temp.B_LINK = me.mention
techifybots.username = '@' + me.username
techifybots.loop.create_task(check_expired_premium(techifybots))
logging.info(f"{me.first_name} with Pyrogram v{__version__} (Layer {layer}) started on {me.username}.")
logging.info(LOG_STR)
logging.info(script.LOGO)
now = datetime.now(timezone("Asia/Kolkata"))
await techifybots.send_message(chat_id=LOG_CHANNEL, text=f"**{temp.B_LINK} is restarted!**\n\n📅 Date : `{now.strftime('%d %B, %Y')}`\n⏰ Time : `{now.strftime('%I:%M:%S %p')}`\n🌐 Timezone : `Asia/Kolkata`")
for admin_id in ADMINS:
try:
await techifybots.send_message(chat_id=int(admin_id), text=f"🤖 {temp.B_NAME} Restarted Successfully ✅")
except Exception as e:
logging.warning(f"Couldn't send restart message to admin {admin_id}: {e}")
app = web.AppRunner(await web_server())
await app.setup()
bind_address = "0.0.0.0"
await web.TCPSite(app, bind_address, PORT).start()
techifybots.loop.create_task(keep_alive())
await idle()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
while True:
try:
loop.run_until_complete(techifybots_start())
break
except FloodWait as e:
print(f"FloodWait! Sleeping for {e.value} seconds.")
time.sleep(e.value)
except KeyboardInterrupt:
logging.info('Service Stopped Bye 👋')
break