forked from SilentDemonSD/WZML-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
237 lines (210 loc) · 11.9 KB
/
__main__.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
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
from time import time, monotonic
from datetime import datetime
from sys import executable
from os import execl as osexecl
from asyncio import create_subprocess_exec, gather
from uuid import uuid4
from base64 import b64decode
from requests import get as rget
from pytz import timezone
from bs4 import BeautifulSoup
from signal import signal, SIGINT
from aiofiles.os import path as aiopath, remove as aioremove
from aiofiles import open as aiopen
from pyrogram.handlers import MessageHandler, CallbackQueryHandler
from pyrogram.filters import command, private, regex
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from bot import bot, bot_name, config_dict, user_data, botStartTime, LOGGER, Interval, DATABASE_URL, QbInterval, INCOMPLETE_TASK_NOTIFIER, scheduler
from bot.version import get_version
from .helper.ext_utils.fs_utils import start_cleanup, clean_all, exit_clean_up
from .helper.ext_utils.bot_utils import get_readable_time, cmd_exec, sync_to_async, new_task, set_commands, update_user_ldata, get_stats
from .helper.ext_utils.db_handler import DbManger
from .helper.telegram_helper.bot_commands import BotCommands
from .helper.telegram_helper.message_utils import sendMessage, editMessage, editReplyMarkup, sendFile, deleteMessage, delete_all_messages
from .helper.telegram_helper.filters import CustomFilters
from .helper.telegram_helper.button_build import ButtonMaker
from .helper.listeners.aria2_listener import start_aria2_listener
from .helper.themes import BotTheme
from .modules import authorize, clone, gd_count, gd_delete, gd_list, cancel_mirror, mirror_leech, status, torrent_search, torrent_select, ytdlp, \
rss, shell, eval, users_settings, bot_settings, speedtest, save_msg, images, imdb, anilist, mediainfo, mydramalist, gen_pyro_sess, \
gd_clean, broadcast, category_select
async def stats(client, message):
msg, btns = await get_stats(message)
await sendMessage(message, msg, btns, photo='IMAGES')
@new_task
async def start(client, message):
buttons = ButtonMaker()
buttons.ubutton(BotTheme('ST_BN1_NAME'), BotTheme('ST_BN1_URL'))
buttons.ubutton(BotTheme('ST_BN2_NAME'), BotTheme('ST_BN2_URL'))
reply_markup = buttons.build_menu(2)
if len(message.command) > 1 and message.command[1] == "wzmlx":
await deleteMessage(message)
elif len(message.command) > 1 and config_dict['TOKEN_TIMEOUT']:
userid = message.from_user.id
encrypted_url = message.command[1]
input_token, pre_uid = (b64decode(encrypted_url.encode()).decode()).split('&&')
if int(pre_uid) != userid:
return await sendMessage(message, '<b>Temporary Token is not yours!</b>\n\n<i>Kindly generate your own.</i>')
data = user_data.get(userid, {})
if 'token' not in data or data['token'] != input_token:
return await sendMessage(message, '<b>Temporary Token already used!</b>\n\n<i>Kindly generate a new one.</i>')
elif config_dict['LOGIN_PASS'] is not None and data['token'] == config_dict['LOGIN_PASS']:
return await sendMessage(message, '<b>Bot Already Logged In via Password</b>\n\n<i>No Need to Accept Temp Tokens.</i>')
buttons.ibutton('Activate Temporary Token', f'pass {input_token}', 'header')
reply_markup = buttons.build_menu(2)
msg = '<b><u>Generated Temporary Login Token!</u></b>\n\n'
msg += f'<b>Temp Token:</b> <code>{input_token}</code>\n\n'
msg += f'<b>Validity:</b> {get_readable_time(int(config_dict["TOKEN_TIMEOUT"]))}'
return await sendMessage(message, msg, reply_markup)
elif await CustomFilters.authorized(client, message):
start_string = BotTheme('ST_MSG', help_command=f"/{BotCommands.HelpCommand}")
await sendMessage(message, start_string, reply_markup, photo='IMAGES')
elif config_dict['BOT_PM']:
await sendMessage(message, BotTheme('ST_BOTPM'), reply_markup, photo='IMAGES')
else:
await sendMessage(message, BotTheme('ST_UNAUTH'), reply_markup, photo='IMAGES')
await DbManger().update_pm_users(message.from_user.id)
async def token_callback(_, query):
user_id = query.from_user.id
input_token = query.data.split()[1]
data = user_data.get(user_id, {})
if 'token' not in data or data['token'] != input_token:
return await query.answer('Already Used, Generate New One', show_alert=True)
update_user_ldata(user_id, 'token', str(uuid4()))
update_user_ldata(user_id, 'time', time())
await query.answer('Activated Temporary Token!', show_alert=True)
kb = query.message.reply_markup.inline_keyboard[1:]
kb.insert(0, [InlineKeyboardButton('✅️ Activated ✅', callback_data='pass activated')])
await editReplyMarkup(query.message, InlineKeyboardMarkup(kb))
async def login(_, message):
if config_dict['LOGIN_PASS'] is None:
return
elif len(message.command) > 1:
user_id = message.from_user.id
input_pass = message.command[1]
if user_data.get(user_id, {}).get('token', '') == config_dict['LOGIN_PASS']:
return await sendMessage(message, '<b>Already Bot Login In!</b>')
if input_pass == config_dict['LOGIN_PASS']:
update_user_ldata(user_id, 'token', config_dict['LOGIN_PASS'])
return await sendMessage(message, '<b>Bot Permanent Login Successfully!</b>')
else:
return await sendMessage(message, '<b>Invalid Password!</b>\n\nKindly put the correct Password .')
else:
await sendMessage(message, '<b>Bot Login Usage :</b>\n\n<code>/cmd {password}</code>')
async def restart(client, message):
restart_message = await sendMessage(message, BotTheme('RESTARTING'))
if scheduler.running:
scheduler.shutdown(wait=False)
await delete_all_messages()
for interval in [QbInterval, Interval]:
if interval:
interval[0].cancel()
await sync_to_async(clean_all)
proc1 = await create_subprocess_exec('pkill', '-9', '-f', 'gunicorn|aria2c|qbittorrent-nox|ffmpeg|rclone')
proc2 = await create_subprocess_exec('python3', 'update.py')
await gather(proc1.wait(), proc2.wait())
async with aiopen(".restartmsg", "w") as f:
await f.write(f"{restart_message.chat.id}\n{restart_message.id}\n")
osexecl(executable, executable, "-m", "bot")
async def ping(_, message):
start_time = monotonic()
reply = await sendMessage(message, BotTheme('PING'))
end_time = monotonic()
await editMessage(reply, BotTheme('PING_VALUE', value=int((end_time - start_time) * 1000)))
async def log(_, message):
buttons = ButtonMaker()
buttons.ibutton('📑 Log Display', f'wzmlx {message.from_user.id} logdisplay')
buttons.ibutton('📨 Web Paste', f'wzmlx {message.from_user.id} webpaste')
await sendFile(message, 'log.txt', buttons=buttons.build_menu(1))
async def search_images():
if query_list := config_dict['IMG_SEARCH']:
try:
total_pages = config_dict['IMG_PAGE']
base_url = "https://www.wallpaperflare.com/search"
for query in query_list:
query = query.strip().replace(" ", "+")
for page in range(1, total_pages + 1):
url = f"{base_url}?wallpaper={query}&width=1280&height=720&page={page}"
r = rget(url)
soup = BeautifulSoup(r.text, "html.parser")
images = soup.select('img[data-src^="https://c4.wallpaperflare.com/wallpaper"]')
if len(images) == 0:
LOGGER.info("Maybe Site is Blocked on your Server, Add Images Manually !!")
for img in images:
img_url = img['data-src']
if img_url not in config_dict['IMAGES']:
config_dict['IMAGES'].append(img_url)
if len(config_dict['IMAGES']) != 0:
config_dict['STATUS_LIMIT'] = 2
if DATABASE_URL:
await DbManger().update_config({'IMAGES': config_dict['IMAGES'], 'STATUS_LIMIT': config_dict['STATUS_LIMIT']})
except Exception as e:
LOGGER.error(f"An error occurred: {e}")
async def bot_help(client, message):
buttons = ButtonMaker()
user_id = message.from_user.id
buttons.ibutton('Basic', f'wzmlx {user_id} guide basic')
buttons.ibutton('Users', f'wzmlx {user_id} guide users')
buttons.ibutton('Mics', f'wzmlx {user_id} guide miscs')
buttons.ibutton('Owner & Sudos', f'wzmlx {user_id} guide admin')
buttons.ibutton('Close', f'wzmlx {user_id} close')
await sendMessage(message, "㊂ <b><i>Help Guide Menu!</i></b>\n\n<b>NOTE: <i>Click on any CMD to see more minor detalis.</i></b>", buttons.build_menu(2))
async def restart_notification():
now=datetime.now(timezone(config_dict['TIMEZONE']))
if await aiopath.isfile(".restartmsg"):
with open(".restartmsg") as f:
chat_id, msg_id = map(int, f)
else:
chat_id, msg_id = 0, 0
async def send_incompelete_task_message(cid, msg):
try:
if msg.startswith("⌬ <b><i>Restarted Successfully!</i></b>"):
await bot.edit_message_text(chat_id=chat_id, message_id=msg_id, text=msg)
await aioremove(".restartmsg")
else:
await bot.send_message(chat_id=cid, text=msg, disable_web_page_preview=True, disable_notification=True)
except Exception as e:
LOGGER.error(e)
if INCOMPLETE_TASK_NOTIFIER and DATABASE_URL:
if notifier_dict := await DbManger().get_incomplete_tasks():
for cid, data in notifier_dict.items():
msg = BotTheme('RESTART_SUCCESS', time=now.strftime('%I:%M:%S %p'), date=now.strftime('%d/%m/%y'), timz=config_dict['TIMEZONE'], version=get_version()) if cid == chat_id else BotTheme('RESTARTED')
msg += "\n\n⌬ <b><i>Incomplete Tasks!</i></b>"
for tag, links in data.items():
msg += f"\n➲ {tag}: "
for index, link in enumerate(links, start=1):
msg += f" <a href='{link}'>{index}</a> |"
if len(msg.encode()) > 4000:
await send_incompelete_task_message(cid, msg)
msg = ''
if msg:
await send_incompelete_task_message(cid, msg)
if await aiopath.isfile(".restartmsg"):
try:
await bot.edit_message_text(chat_id=chat_id, message_id=msg_id, text=BotTheme('RESTART_SUCCESS', time=now.strftime('%I:%M:%S %p'), date=now.strftime('%d/%m/%y'), timz=config_dict['TIMEZONE'], version=get_version()))
except Exception as e:
LOGGER.error(e)
await aioremove(".restartmsg")
async def main():
await gather(start_cleanup(), torrent_search.initiate_search_tools(), restart_notification(), search_images(), set_commands(bot))
await sync_to_async(start_aria2_listener, wait=False)
bot.add_handler(MessageHandler(
start, filters=command(BotCommands.StartCommand) & private))
bot.add_handler(CallbackQueryHandler(
token_callback, filters=regex(r'^pass')))
bot.add_handler(MessageHandler(
login, filters=command(BotCommands.LoginCommand) & private))
bot.add_handler(MessageHandler(log, filters=command(
BotCommands.LogCommand) & CustomFilters.sudo))
bot.add_handler(MessageHandler(restart, filters=command(
BotCommands.RestartCommand) & CustomFilters.sudo))
bot.add_handler(MessageHandler(ping, filters=command(
BotCommands.PingCommand) & CustomFilters.authorized & ~CustomFilters.blacklisted))
bot.add_handler(MessageHandler(bot_help, filters=command(
BotCommands.HelpCommand) & CustomFilters.authorized & ~CustomFilters.blacklisted))
bot.add_handler(MessageHandler(stats, filters=command(
BotCommands.StatsCommand) & CustomFilters.authorized & ~CustomFilters.blacklisted))
LOGGER.info(f"WZML-X Bot [@{bot_name}] Started!")
signal(SIGINT, exit_clean_up)
bot.loop.run_until_complete(main())
bot.loop.run_forever()