azils3/bash
0
1# Ultroid - UserBot2# Copyright (C) 2021-2025 TeamUltroid3#4# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >5# PLease read the GNU Affero General Public License in6# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.7 8from . import get_help9 10__doc__ = get_help("help_bot")11 12import os13import sys14import time15from platform import python_version as pyver16from random import choice17 18from telethon import __version__19from telethon.errors.rpcerrorlist import (20 BotMethodInvalidError,21 ChatSendMediaForbiddenError,22)23 24from pyUltroid.version import __version__ as UltVer25 26from . import HOSTED_ON, LOGS27 28try:29 from git import Repo30except ImportError:31 LOGS.error("bot: 'gitpython' module not found!")32 Repo = None33 34from telethon.utils import resolve_bot_file_id35 36from . import (37 ATRA_COL,38 LOGS,39 OWNER_NAME,40 ULTROID_IMAGES,41 Button,42 Carbon,43 Telegraph,44 Var,45 allcmds,46 asst,47 bash,48 call_back,49 callback,50 def_logs,51 eor,52 get_string,53 heroku_logs,54 in_pattern,55 inline_pic,56 restart,57 shutdown,58 start_time,59 time_formatter,60 udB,61 ultroid_cmd,62 ultroid_version,63 updater,64)65 66 67def ULTPIC():68 return inline_pic() or choice(ULTROID_IMAGES)69 70 71buttons = [72 [73 Button.url(get_string("bot_3"), "https://github.com/TeamUltroid/Ultroid"),74 Button.url(get_string("bot_4"), "t.me/UltroidSupportChat"),75 ]76]77 78# Will move to strings79alive_txt = """80The Ultroid Userbot81 82 โ Version - {}83 โ Py-Ultroid - {}84 โ Telethon - {}85"""86 87in_alive = "{}\n\n๐ <b>Ultroid Version -><b> <code>{}</code>\n๐ <b>PyUltroid -></b> <code>{}</code>\n๐ <b>Python -></b> <code>{}</code>\n๐ <b>Uptime -></b> <code>{}</code>\n๐ <b>Branch -></b>[ {} ]\n\nโข <b>Join @TeamUltroid</b>"88 89 90@callback("alive")91async def alive(event):92 text = alive_txt.format(ultroid_version, UltVer, __version__)93 await event.answer(text, alert=True)94 95 96@ultroid_cmd(97 pattern="alive( (.*)|$)",98)99async def lol(ult):100 match = ult.pattern_match.group(1).strip()101 inline = None102 if match in ["inline", "i"]:103 try:104 res = await ult.client.inline_query(asst.me.username, "alive")105 return await res[0].click(ult.chat_id)106 except BotMethodInvalidError:107 pass108 except BaseException as er:109 LOGS.exception(er)110 inline = True111 pic = udB.get_key("ALIVE_PIC")112 if isinstance(pic, list):113 pic = choice(pic)114 uptime = time_formatter((time.time() - start_time) * 1000)115 header = udB.get_key("ALIVE_TEXT") or get_string("bot_1")116 y = Repo().active_branch117 xx = Repo().remotes[0].config_reader.get("url")118 rep = xx.replace(".git", f"/tree/{y}")119 kk = f" `[{y}]({rep})` "120 if inline:121 kk = f"<a href={rep}>{y}</a>"122 parse = "html"123 als = in_alive.format(124 header,125 f"{ultroid_version} [{HOSTED_ON}]",126 UltVer,127 pyver(),128 uptime,129 kk,130 )131 132 if _e := udB.get_key("ALIVE_EMOJI"):133 als = als.replace("๐", _e)134 else:135 parse = "md"136 als = (get_string("alive_1")).format(137 header,138 OWNER_NAME,139 f"{ultroid_version} [{HOSTED_ON}]",140 UltVer,141 uptime,142 pyver(),143 __version__,144 kk,145 )146 147 if a := udB.get_key("ALIVE_EMOJI"):148 als = als.replace("โต", a)149 if pic:150 try:151 await ult.reply(152 als,153 file=pic,154 parse_mode=parse,155 link_preview=False,156 buttons=buttons if inline else None,157 )158 return await ult.try_delete()159 except ChatSendMediaForbiddenError:160 pass161 except BaseException as er:162 LOGS.exception(er)163 try:164 await ult.reply(file=pic)165 await ult.reply(166 als,167 parse_mode=parse,168 buttons=buttons if inline else None,169 link_preview=False,170 )171 return await ult.try_delete()172 except BaseException as er:173 LOGS.exception(er)174 await eor(175 ult,176 als,177 parse_mode=parse,178 link_preview=False,179 buttons=buttons if inline else None,180 )181 182 183@ultroid_cmd(pattern="ping$", chats=[], type=["official", "assistant"])184async def _(event):185 start = time.time()186 x = await event.eor("Pong !")187 end = round((time.time() - start) * 1000)188 uptime = time_formatter((time.time() - start_time) * 1000)189 await x.edit(get_string("ping").format(end, uptime))190 191 192@ultroid_cmd(193 pattern="cmds$",194)195async def cmds(event):196 await allcmds(event, Telegraph)197 198 199heroku_api = Var.HEROKU_API200 201 202@ultroid_cmd(203 pattern="restart$",204 fullsudo=True,205)206async def restartbt(ult):207 ok = await ult.eor(get_string("bot_5"))208 call_back()209 who = "bot" if ult.client._bot else "user"210 udB.set_key("_RESTART", f"{who}_{ult.chat_id}_{ok.id}")211 if heroku_api:212 return await restart(ok)213 await bash("git pull && pip3 install -r requirements.txt")214 await bash("pip3 install -r requirements.txt --break-system-packages")215 if len(sys.argv) > 1:216 os.execl(sys.executable, sys.executable, "main.py")217 else:218 os.execl(sys.executable, sys.executable, "-m", "pyUltroid")219 220 221@ultroid_cmd(222 pattern="shutdown$",223 fullsudo=True,224)225async def shutdownbot(ult):226 await shutdown(ult)227 228 229@ultroid_cmd(230 pattern="logs( (.*)|$)",231 chats=[],232)233async def _(event):234 opt = event.pattern_match.group(1).strip()235 file = f"ultroid{sys.argv[-1]}.log" if len(sys.argv) > 1 else "ultroid.log"236 if opt == "heroku":237 await heroku_logs(event)238 elif opt == "carbon" and Carbon:239 event = await event.eor(get_string("com_1"))240 with open(file, "r") as f:241 code = f.read()[-2500:]242 file = await Carbon(243 file_name="ultroid-logs",244 code=code,245 backgroundColor=choice(ATRA_COL),246 )247 if isinstance(file, dict):248 await event.eor(f"`{file}`")249 return250 await event.reply("**Ultroid Logs.**", file=file)251 elif opt == "open":252 with open("ultroid.log", "r") as f:253 file = f.read()[-4000:]254 return await event.eor(f"`{file}`")255 elif (256 opt.isdigit() and 5 <= int(opt) <= 100257 ): # Check if input is a number between 10 and 100258 num_lines = int(opt)259 with open("ultroid.log", "r") as f:260 lines = f.readlines()[-num_lines:]261 file = "".join(lines)262 return await event.eor(f"`{file}`")263 else:264 await def_logs(event, file)265 await event.try_delete()266 267 268@in_pattern("alive", owner=True)269async def inline_alive(ult):270 pic = udB.get_key("ALIVE_PIC")271 if isinstance(pic, list):272 pic = choice(pic)273 uptime = time_formatter((time.time() - start_time) * 1000)274 header = udB.get_key("ALIVE_TEXT") or get_string("bot_1")275 y = Repo().active_branch276 xx = Repo().remotes[0].config_reader.get("url")277 rep = xx.replace(".git", f"/tree/{y}")278 kk = f"<a href={rep}>{y}</a>"279 als = in_alive.format(280 header, f"{ultroid_version} [{HOSTED_ON}]", UltVer, pyver(), uptime, kk281 )282 283 if _e := udB.get_key("ALIVE_EMOJI"):284 als = als.replace("๐", _e)285 builder = ult.builder286 if pic:287 try:288 if ".jpg" in pic:289 results = [290 await builder.photo(291 pic, text=als, parse_mode="html", buttons=buttons292 )293 ]294 else:295 if _pic := resolve_bot_file_id(pic):296 pic = _pic297 buttons.insert(298 0, [Button.inline(get_string("bot_2"), data="alive")]299 )300 results = [301 await builder.document(302 pic,303 title="Inline Alive",304 description="@TeamUltroid",305 parse_mode="html",306 buttons=buttons,307 )308 ]309 return await ult.answer(results)310 except BaseException as er:311 LOGS.exception(er)312 result = [313 await builder.article(314 "Alive", text=als, parse_mode="html", link_preview=False, buttons=buttons315 )316 ]317 await ult.answer(result)318 319 320@ultroid_cmd(pattern="update( (.*)|$)")321async def _(e):322 xx = await e.eor(get_string("upd_1"))323 if e.pattern_match.group(1).strip() and (324 "fast" in e.pattern_match.group(1).strip()325 or "soft" in e.pattern_match.group(1).strip()326 ):327 await bash("git pull -f && pip3 install -r requirements.txt")328 await bash("pip3 install -r requirements.txt --break-system-packages")329 call_back()330 await xx.edit(get_string("upd_7"))331 os.execl(sys.executable, "python3", "-m", "pyUltroid")332 # return333 m = await updater()334 branch = (Repo.init()).active_branch335 if m:336 x = await asst.send_file(337 udB.get_key("LOG_CHANNEL"),338 ULTPIC(),339 caption="โข **Update Available** โข",340 force_document=False,341 buttons=Button.inline("Changelogs", data="changes"),342 )343 Link = x.message_link344 await xx.edit(345 f'<strong><a href="{Link}">[ChangeLogs]</a></strong>',346 parse_mode="html",347 link_preview=False,348 )349 else:350 await xx.edit(351 f'<code>Your BOT is </code><strong>up-to-date</strong><code> with </code><strong><a href="https://github.com/TeamUltroid/Ultroid/tree/{branch}">[{branch}]</a></strong>',352 parse_mode="html",353 link_preview=False,354 )355 356 357@callback("updtavail", owner=True)358async def updava(event):359 await event.delete()360 await asst.send_file(361 udB.get_key("LOG_CHANNEL"),362 ULTPIC(),363 caption="โข **Update Available** โข",364 force_document=False,365 buttons=Button.inline("Changelogs", data="changes"),366 )367 