2020-06-12 14:20:18 -04:00
|
|
|
import asyncio
|
2020-07-19 11:13:54 -04:00
|
|
|
import inspect
|
|
|
|
import io
|
2020-06-21 15:14:41 -04:00
|
|
|
import os
|
2023-10-12 19:45:15 -04:00
|
|
|
import sys
|
2023-05-07 17:19:42 -04:00
|
|
|
from datetime import timedelta
|
2020-06-12 14:20:18 -04:00
|
|
|
from platform import python_version
|
2023-05-07 17:19:42 -04:00
|
|
|
from time import time
|
2023-07-23 13:04:13 -04:00
|
|
|
from traceback import print_exc
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
import git
|
2020-06-21 15:14:41 -04:00
|
|
|
import psutil
|
2020-06-12 14:20:18 -04:00
|
|
|
from telethon import version
|
2020-09-22 09:20:43 -04:00
|
|
|
from telethon.tl.types import Channel, Chat
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2023-10-16 14:32:57 -04:00
|
|
|
from ubot import ldr, startup_time
|
2020-06-12 14:20:18 -04:00
|
|
|
|
|
|
|
|
2020-07-19 11:13:54 -04:00
|
|
|
@ldr.add("eval", owner=True, hide_help=True)
|
|
|
|
async def evaluate(event):
|
|
|
|
if not event.args:
|
|
|
|
await event.reply("Give me code to run!")
|
|
|
|
return
|
|
|
|
|
|
|
|
eval_msg = await event.reply("Processing…")
|
2023-10-12 21:57:53 -04:00
|
|
|
|
|
|
|
# helpful variables
|
2020-07-19 11:13:54 -04:00
|
|
|
reply = await event.get_reply_message()
|
2023-10-12 21:57:53 -04:00
|
|
|
client = event.client
|
2020-07-19 11:13:54 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
eval_ret = eval(event.args)
|
|
|
|
except Exception as exception:
|
2023-07-23 13:04:13 -04:00
|
|
|
print_exc()
|
2020-07-19 11:13:54 -04:00
|
|
|
eval_ret = exception
|
|
|
|
|
|
|
|
if inspect.isawaitable(eval_ret):
|
|
|
|
isawait = " (awaited)"
|
|
|
|
eval_ret = await eval_ret
|
|
|
|
else:
|
|
|
|
isawait = ""
|
|
|
|
|
|
|
|
if len(f"**Evaluation:**\n{event.args}\n**Return{isawait}:**\n{eval_ret}") > 4096:
|
|
|
|
text_io = io.BytesIO(str(eval_ret).encode("utf-8"))
|
|
|
|
text_io.name = "return.txt"
|
|
|
|
await eval_msg.edit("Output too large for a message, sending as a file…")
|
|
|
|
await eval_msg.reply(file=text_io)
|
|
|
|
return
|
|
|
|
|
|
|
|
await eval_msg.edit(f"**Evaluation:**\n{event.args}\n**Return{isawait}:**\n{eval_ret}")
|
|
|
|
|
|
|
|
|
2021-07-11 13:23:53 -04:00
|
|
|
@ldr.add("exec", owner=True, hide_help=True)
|
2021-06-22 15:34:53 -04:00
|
|
|
async def execute(event):
|
|
|
|
exec_msg = await event.reply("Processing…")
|
|
|
|
reply = await event.get_reply_message()
|
2023-10-12 21:57:53 -04:00
|
|
|
client = event.client
|
2021-06-22 15:34:53 -04:00
|
|
|
|
|
|
|
if not event.args:
|
|
|
|
await event.edit("Give me code to run!")
|
|
|
|
return
|
|
|
|
|
|
|
|
temp_locals = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(
|
2023-10-12 21:57:53 -04:00
|
|
|
f'async def __ex(event, reply, client): ' +
|
2021-06-22 15:34:53 -04:00
|
|
|
''.join(f'\n {l}' for l in event.args.split('\n')),
|
|
|
|
globals(),
|
|
|
|
temp_locals
|
|
|
|
)
|
|
|
|
|
2023-10-12 21:57:53 -04:00
|
|
|
eval_ret = await temp_locals['__ex'](event, reply, client)
|
2021-06-22 15:34:53 -04:00
|
|
|
except Exception as exception:
|
2023-07-23 13:04:13 -04:00
|
|
|
print_exc()
|
2021-06-22 15:34:53 -04:00
|
|
|
eval_ret = exception
|
|
|
|
|
|
|
|
if len(f"**Execution:**\n`{event.args}`\n**Return:**\n`{eval_ret}`") > 4096:
|
|
|
|
text_io = io.BytesIO(str(eval_ret).encode("utf-8"))
|
|
|
|
text_io.name = "return.txt"
|
|
|
|
await exec_msg.edit("Output too large for a message, sending as a file…")
|
|
|
|
await event.respond(file=text_io)
|
|
|
|
return
|
|
|
|
|
|
|
|
await exec_msg.edit(f"**Execution:**\n`{event.args}`\n**Return:**\n`{eval_ret}`")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("reload", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def reload_modules(event):
|
|
|
|
reload_msg = await event.reply("Reloading modules…")
|
|
|
|
|
|
|
|
errors = ldr.reload_all_modules()
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
await reload_msg.edit(errors)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
await reload_msg.edit("Successfully reloaded.")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
@ldr.add("update", owner=True, hide_help=True)
|
|
|
|
async def update_bot(event):
|
|
|
|
update_msg = await event.reply("Pulling changes…")
|
|
|
|
repo = git.Repo(os.getcwd())
|
|
|
|
repo.remotes.origin.pull()
|
|
|
|
await update_msg.edit("Changes pulled successfully!")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("sysd", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def sysd(event):
|
|
|
|
try:
|
|
|
|
neo = "neofetch --stdout"
|
|
|
|
|
|
|
|
fetch = await asyncio.create_subprocess_shell(
|
|
|
|
neo,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE,
|
|
|
|
)
|
|
|
|
|
|
|
|
stdout, stderr = await fetch.communicate()
|
|
|
|
|
|
|
|
await event.reply(f"`{stdout.decode().strip()}{stderr.decode().strip()}`")
|
|
|
|
except FileNotFoundError:
|
|
|
|
await event.reply("Neofetch not found!")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("alive", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def alive(event):
|
|
|
|
alive_format = "**Telethon version:** {0}\n" \
|
2020-06-21 15:14:41 -04:00
|
|
|
"**Python version:** {1}\n" \
|
2023-10-12 19:45:15 -04:00
|
|
|
"**Memory usage:** {2} MiB\n" \
|
2023-05-07 17:19:42 -04:00
|
|
|
"**Uptime:** {3}"
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2023-10-12 19:45:15 -04:00
|
|
|
await event.reply(
|
|
|
|
alive_format.format(
|
|
|
|
version.__version__,
|
|
|
|
python_version(),
|
|
|
|
int(psutil.Process(os.getpid()).memory_info().rss / 1048576),
|
|
|
|
timedelta(seconds=int(time() - startup_time))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@ldr.add("dbstat", owner=True, hide_help=True)
|
|
|
|
async def dbstat(event):
|
|
|
|
dbstat_format = "**Size on disk:** {0} MiB\n" \
|
|
|
|
"**Chat count:** {1}\n" \
|
|
|
|
"**Cache memory usage:** {2} MiB\n" \
|
|
|
|
"**Cached chat count:** {3}"
|
|
|
|
|
|
|
|
await event.reply(
|
|
|
|
dbstat_format.format(
|
|
|
|
round(os.stat("database.sqlite").st_size / 1048576, 2),
|
|
|
|
ldr.db.chat_table.select().count(),
|
|
|
|
round(sys.getsizeof(ldr.db.cached_chat_wrappers) / 1048576, 2),
|
|
|
|
len(ldr.db.cached_chat_wrappers)
|
|
|
|
)
|
|
|
|
)
|
2020-06-12 14:20:18 -04:00
|
|
|
|
|
|
|
|
2023-05-07 17:08:49 -04:00
|
|
|
@ldr.add("shutdown", pattern_extra="(f|)", owner=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def shutdown(event):
|
|
|
|
await event.reply("Goodbye…")
|
2023-05-07 17:08:49 -04:00
|
|
|
|
|
|
|
if event.other_args[0]:
|
2023-10-16 14:32:57 -04:00
|
|
|
await ldr.micro_bot.stop_client(reason="Shutdown command issued.", exit_code=1)
|
2023-05-07 17:08:49 -04:00
|
|
|
else:
|
2023-10-16 14:32:57 -04:00
|
|
|
await ldr.micro_bot.stop_client(reason="Shutdown command issued.")
|
2020-06-12 14:20:18 -04:00
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("blacklist", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def add_blacklist(event):
|
2020-06-12 23:34:12 -04:00
|
|
|
user_entity = await get_user(event)
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2020-06-12 23:34:12 -04:00
|
|
|
if not user_entity:
|
2020-06-12 14:20:18 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
userid = user_entity.id
|
|
|
|
userfullname = f"{user_entity.first_name} {user_entity.last_name or ''}".strip()
|
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
ldr.db.blacklist_user(userid)
|
2020-06-12 14:20:18 -04:00
|
|
|
await event.reply(f"Successfully blacklisted **{userfullname}** (`{userid}`)")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("unblacklist", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def rem_blacklist(event):
|
2020-06-12 23:34:12 -04:00
|
|
|
user_entity = await get_user(event)
|
2020-06-12 23:26:54 -04:00
|
|
|
|
2020-06-12 23:34:12 -04:00
|
|
|
if not user_entity:
|
2020-06-12 14:20:18 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
userid = user_entity.id
|
|
|
|
userfullname = f"{user_entity.first_name} {user_entity.last_name or ''}".strip()
|
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
ldr.db.unblacklist_user(userid)
|
2020-06-12 14:20:18 -04:00
|
|
|
await event.reply(f"Successfully unblacklisted **{userfullname}** (`{userid}`)")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("showblacklist", sudo=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def show_blacklist(event):
|
2023-10-12 21:54:01 -04:00
|
|
|
blacklist_string = "\n".join([str(user_id) for user_id in ldr.db.blacklisted_users])
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
await event.reply(f"**Blacklisted users:**\n\n{blacklist_string}")
|
2020-06-12 14:20:18 -04:00
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("sudo", owner=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def add_sudo(event):
|
2020-06-12 23:34:12 -04:00
|
|
|
user_entity = await get_user(event)
|
2020-06-12 23:26:54 -04:00
|
|
|
|
2020-06-12 23:34:12 -04:00
|
|
|
if not user_entity:
|
2020-06-12 14:20:18 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
userid = user_entity.id
|
|
|
|
userfullname = f"{user_entity.first_name} {user_entity.last_name or ''}".strip()
|
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
ldr.db.sudo_user(userid)
|
2020-06-12 14:20:18 -04:00
|
|
|
await event.reply(f"Successfully sudo'd **{userfullname}** (`{userid}`)")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("unsudo", owner=True, hide_help=True)
|
2020-06-12 14:20:18 -04:00
|
|
|
async def rem_sudo(event):
|
2020-06-12 23:34:12 -04:00
|
|
|
user_entity = await get_user(event)
|
|
|
|
|
|
|
|
if not user_entity:
|
|
|
|
return
|
|
|
|
|
|
|
|
userid = user_entity.id
|
|
|
|
userfullname = f"{user_entity.first_name} {user_entity.last_name or ''}".strip()
|
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
ldr.db.unsudo_user(userid)
|
2020-06-12 23:34:12 -04:00
|
|
|
await event.reply(f"Successfully unsudo'd **{userfullname}** (`{userid}`)")
|
|
|
|
|
|
|
|
|
2020-06-17 19:20:19 -04:00
|
|
|
@ldr.add("showsudo", sudo=True, hide_help=True)
|
2020-06-12 23:34:12 -04:00
|
|
|
async def show_sudo(event):
|
2023-10-12 21:54:01 -04:00
|
|
|
sudo_string = "\n".join([str(user_id) for user_id in ldr.db.sudo_users])
|
2021-03-23 19:16:21 -04:00
|
|
|
await event.reply(f"**Sudo users:**\n\n{sudo_string}")
|
2020-06-12 23:34:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def get_user(event):
|
2020-06-12 14:20:18 -04:00
|
|
|
if event.args:
|
2020-06-12 23:26:54 -04:00
|
|
|
try:
|
|
|
|
event.args = int(event.args)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-06-12 14:20:18 -04:00
|
|
|
try:
|
2020-09-22 09:20:43 -04:00
|
|
|
user = await event.client.get_entity(event.args)
|
|
|
|
|
|
|
|
if isinstance(user, (Chat, Channel)):
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
return user
|
2020-06-12 14:20:18 -04:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
await event.reply("The ID or username you provided was invalid!")
|
|
|
|
return
|
|
|
|
elif event.is_reply:
|
|
|
|
reply = await event.get_reply_message()
|
2020-10-03 11:27:11 -04:00
|
|
|
reply_id = reply.sender_id
|
2020-06-12 14:20:18 -04:00
|
|
|
|
|
|
|
if reply_id:
|
|
|
|
try:
|
2020-09-22 09:20:43 -04:00
|
|
|
user = await event.client.get_entity(reply_id)
|
|
|
|
|
|
|
|
if isinstance(user, (Chat, Channel)):
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
return user
|
2020-06-12 14:20:18 -04:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
await event.reply("There was an error getting the user's ID!")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
await event.reply("Sudoing failed!")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
await event.reply("Give me a user ID, username or reply!")
|
|
|
|
return
|