-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathbot.py
More file actions
180 lines (153 loc) · 5.4 KB
/
bot.py
File metadata and controls
180 lines (153 loc) · 5.4 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
import sys
import glob
import importlib
import asyncio
import logging
import logging.config
import traceback
from pathlib import Path
from datetime import date, datetime
import pytz
from aiohttp import web
from pyrogram import idle, __version__
from pyrogram.raw.all import layer
import pyrogram.utils
from pyrogram.types import Message
from info import *
from utils import temp, ping_server
from Script import script
from web import web_server, check_expired_premium
from web.bot import WebavBot
from web.bot.clients import initialize_clients
from web.server.support_logger import setup_support_logger
logger = setup_support_logger()
# ===============================
# GLOBAL EXCEPTION CATCH
# ===============================
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logger.critical(
"UNHANDLED EXCEPTION",
exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = handle_exception
# Hack for Channel ID limits
pyrogram.utils.MIN_CHANNEL_ID = -1009147483647
# ===============================
# Logging Config
# ===============================
try:
logging.config.fileConfig("logging.conf")
except Exception:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
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)
# Plugin Path
ppath = "plugins/*.py"
files = glob.glob(ppath)
# ===============================
# MAIN START FUNCTION
# ===============================
async def Webav_start():
print("\n")
print("Credit - Telegram @BOT_OWNER26")
try:
# 1. Initialize additional clients
await initialize_clients()
# 2. Start the main bot
print("Starting WebavBot...")
await WebavBot.start()
bot_info = await WebavBot.get_me()
WebavBot.username = bot_info.username
# 3. Import Plugins
print("Importing Plugins...")
for name in files:
try:
patt = Path(name)
plugin_name = patt.stem
import_path = f"plugins.{plugin_name}"
spec = importlib.util.spec_from_file_location(import_path, patt)
if spec and spec.loader:
load = importlib.util.module_from_spec(spec)
spec.loader.exec_module(load)
sys.modules["plugins." + plugin_name] = load
print(f"✅ Imported => {plugin_name}")
except Exception:
logger.error("PLUGIN IMPORT FAILED", exc_info=True)
# 4. Heroku Ping
if ON_HEROKU:
asyncio.create_task(ping_server())
# 5. Set Global Info
me = await WebavBot.get_me()
temp.ME = me.id
temp.U_NAME = me.username
temp.B_NAME = me.first_name
temp.B_LINK = me.mention
WebavBot.username = "@" + me.username
logging.info(
f"{me.first_name} with Pyrogram v{__version__} (Layer {layer}) started on {me.username}."
)
# 6. Date / Time
tz = pytz.timezone("Asia/Kolkata")
today = date.today()
now = datetime.now(tz)
time = now.strftime("%H:%M:%S %p")
# 7. Background Tasks
asyncio.create_task(check_expired_premium(WebavBot))
# 8. Startup Messages
try:
await WebavBot.send_message(
chat_id=LOG_CHANNEL,
text=script.RESTART_TXT.format(today, time)
)
except Exception:
logger.error("LOG CHANNEL ERROR", exc_info=True)
if SUPPORT_GROUP:
try:
await WebavBot.send_message(
chat_id=SUPPORT_GROUP,
text=f"<b>{me.mention} ʀᴇsᴛᴀʀᴛᴇᴅ 🤖</b>"
)
except Exception:
logger.error("SUPPORT GROUP ERROR", exc_info=True)
if ADMINS:
try:
await WebavBot.send_message(
chat_id=ADMINS[0],
text="<b>ʙᴏᴛ ʀᴇsᴛᴀʀᴛᴇᴅ !!</b>"
)
except Exception:
pass
# 9. Start Web Server
app = web.AppRunner(await web_server())
await app.setup()
bind_address = "0.0.0.0"
await web.TCPSite(app, bind_address, PORT).start()
logging.info(f"Web Server Started on Port {PORT}")
# 10. Idle
await idle()
except Exception:
logger.critical("STARTUP SEQUENCE FAILED", exc_info=True)
sys.exit(1)
# ===============================
# ENTRY POINT
# ===============================
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(Webav_start())
except KeyboardInterrupt:
logging.info("Service Stopped Bye 👋")
except Exception:
logger.critical("CRITICAL RUNTIME ERROR", exc_info=True)