async def upload_func(_, message: Message): if len(message.text.split()) != 2: return await eor(message, text="Invalid Arguments") url_or_path = message.text.split(None, 1)[1] start = time() body = { "Started": ctime(start), } m = await eor(message, text=section("Uploading", body)) async def upload_file(path: str): task, task_id = await add_task( message.reply_document, "Uploader", path, ) body["Task ID"] = task_id await eor( m, text=section("Uploading", body), ) await task await rm_task(task_id) elapsed = int(time() - start) body["Took"] = f"{elapsed}s" return await eor(m, text=section("Uploaded", body)) try: if isfile(url_or_path): return await upload_file(url_or_path) task, task_id = await download(url_or_path) body["Task ID"] = task_id await eor( m, text=section("Downloading", body), ) await task file = task.result() await rm_task(task_id) await upload_file(file) remove(file) except Exception as e: e = format_exc() e = e.splitlines()[-1] return await eor(m, text=f"**Error:** `{str(e)}`")
async def get_chat_info(chat, already=False): if not already: chat = await app.get_chat(chat) chat_id = chat.id username = chat.username title = chat.title type_ = chat.type is_scam = chat.is_scam description = chat.description members = chat.members_count is_restricted = chat.is_restricted link = f"[Link](t.me/{username})" if username else "Null" dc_id = chat.dc_id photo_id = chat.photo.big_file_id if chat.photo else None body = { "ID": chat_id, "DC": dc_id, "Type": type_, "Name": [title], "Username": [("@" + username) if username else "Null"], "Mention": [link], "Members": members, "Scam": is_scam, "Restricted": is_restricted, "Description": [description], } caption = section("Chat info", body) return [caption, photo_id]
async def get_user_info(user, already=False): if not already: user = await app.get_users(user) if not user.first_name: return ["Deleted account", None] user_id = user.id username = user.username first_name = user.first_name mention = user.mention("Link") dc_id = user.dc_id photo_id = user.photo.big_file_id if user.photo else None is_gbanned = await is_gbanned_user(user_id) is_sudo = user_id in SUDOERS karma = await user_global_karma(user_id) body = { "ID": user_id, "DC": dc_id, "Name": [first_name], "Username": [("@" + username) if username else "Null"], "Mention": [mention], "Sudo": is_sudo, "Karma": karma, "Gbanned": is_gbanned, } caption = section("User info", body) return [caption, photo_id]
async def _get_tasks_text(): await rm_task() # Clean completed tasks if not tasks: return f"{arrow('')} No pending task" text = bold("Tasks") + "\n" for i, task in enumerate(list(tasks.items())): indent = w * 4 t, started = task[1] elapsed = round(time() - started) info = t._repr_info() id = task[0] text += section( f"{indent}Task {i}", body={ "Name": t.get_name(), "Task ID": id, "Status": info[0].capitalize(), "Origin": info[2].split("/")[-1].replace(">", ""), "Running since": f"{elapsed}s", }, indent=8, ) return text
async def crypto(_, message): if len(message.command) < 2: return await message.reply("/crypto [currency]") currency = message.text.split(None, 1)[1].lower() btn = ikb( {"Available Currencies": "https://plotcryptoprice.herokuapp.com"}, ) m = await message.reply("`Processing...`") try: r = await get( "https://x.wazirx.com/wazirx-falcon/api/v2.0/crypto_rates", timeout=5, ) except Exception: return await m.edit("[ERROR]: Something went wrong.") if currency not in r: return await m.edit( "[ERROR]: INVALID CURRENCY", reply_markup=btn, ) body = {i.upper(): j for i, j in r.get(currency).items()} text = section( "Current Crypto Rates For " + currency.upper(), body, ) await m.edit(text, reply_markup=btn)
async def convert( main_message: Message, reply_messages, status_message: Message, start_time: float, ): m = status_message documents = [] for message in reply_messages: if not message.document: return await m.edit("Not document, ABORTED!") if message.document.mime_type.split("/")[0] != "image": return await m.edit("Invalid mime type!") if message.document.file_size > 5000000: return await m.edit("Size too large, ABORTED!") documents.append(await message.download()) for img_path in documents: img = Image.open(img_path).convert("RGB") img.save(img_path, "JPEG", quality=100) pdf = BytesIO(img2pdf.convert(documents)) pdf.name = "wbb.pdf" if len(main_message.command) >= 2: names = main_message.text.split(None, 1)[1] if not names.endswith(".pdf"): pdf.name = names + ".pdf" else: pdf.name = names elapsed = round(time() - start_time, 2) await main_message.reply_document( document=pdf, caption=section( "IMG2PDF", body={ "Title": pdf.name, "Size": f"{pdf.__sizeof__() / (10 ** 6)}MB", "Pages": len(documents), "Took": f"{elapsed}s", }, ), ) await m.delete() pdf.close() for file in documents: if path.exists(file): remove(file)
async def upload_file(path: str): task, task_id = await add_task( message.reply_document, "Uploader", path, ) body["Task ID"] = task_id await eor( m, text=section("Uploading", body), ) await task await rm_task(task_id) elapsed = int(time() - start) body["Took"] = f"{elapsed}s" return await eor(m, text=section("Uploaded", body))
async def command_karma(_, message): chat_id = message.chat.id if not message.reply_to_message: m = await message.reply_text("Analyzing Karma...") karma = await get_karmas(chat_id) if not karma: return await m.edit("No karma in DB for this chat.") msg = f"Karma list of {message.chat.title}" limit = 0 karma_dicc = {} for i in karma: user_id = await alpha_to_int(i) user_karma = karma[i]["karma"] karma_dicc[str(user_id)] = user_karma karma_arranged = dict( sorted( karma_dicc.items(), key=lambda item: item[1], reverse=True, ) ) if not karma_dicc: return await m.edit("No karma in DB for this chat.") userdb = await get_user_id_and_usernames(app) karma = {} for user_idd, karma_count in karma_arranged.items(): if limit > 15: break if int(user_idd) not in list(userdb.keys()): continue username = userdb[int(user_idd)] karma["@" + username] = ["**" + str(karma_count) + "**"] limit += 1 await m.edit(section(msg, karma)) else: if not message.reply_to_message.from_user: return await message.reply("Anon user hash no karma.") user_id = message.reply_to_message.from_user.id karma = await get_karma(chat_id, await int_to_alpha(user_id)) if karma: karma = karma["karma"] await message.reply_text(f"**Total Points**: __{karma}__") else: karma = 0 await message.reply_text(f"**Total Points**: __{karma}__")
async def arq_stats(_, message): data = await arq.stats() if not data.ok: return await message.reply_text(data.result) server = data.result body = { "Uptime": server.uptime, "Requests Since Uptime": server.requests, "CPU": server.cpu, "Memory": server.memory.server, "Platform": server.platform, "Python": server.python, "Users": server.users, "Bot": [server.bot], } text = section("A.R.Q", body) await message.reply_text(text)
async def download_func(_, message: Message): reply = message.reply_to_message start = time() body = { "Started": ctime(start), } m = await eor( message, text=section("Downloading", body), ) if reply: task, task_id = await add_task( reply.download, task_name="Downloader", ) body["Task ID"] = task_id await eor( m, text=section("Downloading", body), ) await task await rm_task(task_id) file = task.result() elapsed = int(time() - start) body["File"] = file.split("/")[-1] body["Took"] = f"{elapsed}s" return await eor(m, text=section("Downloaded", body)) text = message.text if len(text.split()) < 2: return await eor(message, text="Invalid Arguments") url = text.split(None, 1)[1] try: task, task_id = await download(url) body["Task ID"] = task_id await eor( m, text=section("Downloading", body), ) await task file = task.result() await rm_task(task_id) except Exception as e: e = format_exc() e = e.splitlines()[-1] return await eor(m, text=f"**Error:** `{str(e)}`") elapsed = int(time() - start) body["File"] = file.split("/")[-1] body["Took"] = f"{elapsed}s" await eor(m, text=section("Downloaded", body))
async def parse(_, message: Message): r = message.reply_to_message has_wpp = False if not r: return await eor(message, text="Reply to a message with a webpage") m_ = await eor(message, text="Parsing...") if not r.web_page: text = r.text or r.caption if text: m = await app2.send_message("me", text) await sleep(1) await m.delete() if m.web_page: r = m has_wpp = True else: has_wpp = True if not has_wpp: return await m_.edit("Replied message has no webpage preview.", ) wpp = r.web_page body = { "Title": [wpp.title or "Null"], "Description": [(wpp.description[:50] + "...") if wpp.description else "Null"], "URL": [wpp.display_url or "Null"], "Author": [wpp.author or "Null"], "Site Name": [wpp.site_name or "Null"], "Type": wpp.type or "Null", } text = section("Preview", body) t = wpp.type if t == "photo": media = wpp.photo func = app2.send_photo elif t == "audio": media = wpp.audio func = app2.send_audio elif t == "video": media = wpp.video func = app2.send_video elif t == "document": media = wpp.document func = app2.send_document else: media = None func = None if media and func: await m_.delete() return await func( m_.chat.id, media.file_id, caption=text, ) await m_.edit(text, disable_web_page_preview=True)