ubot/ubot.py

147 lines
3.9 KiB
Python
Executable File

#!/usr/bin/python3
from telethon import TelegramClient, events
from config import *
client = TelegramClient('ubot', api_id, api_hash)
@client.on(events.NewMessage)
async def edit(event):
msg = event.raw_text
if msg.startswith('.alive'):
rpl = "Userbot alive and well!"
await event.edit(rpl)
if '.shg' in msg:
rpl = "¯\_(ツ)_/¯"
msg = event.text.replace('.shg', rpl)
await event.edit(msg)
if msg.startswith('.stats'):
from telethon.tl.custom import Dialog
from telethon.tl.types import Channel, User, Chat
pms = bots = 0
groups = gadmin = gcreator = 0
chans = cadmin = ccreator = 0
unread = mentions = 0
dialog: Dialog
async for dialog in event.client.iter_dialogs():
entity = dialog.entity
if isinstance(entity, Channel):
if entity.broadcast:
chans += 1
if entity.creator or entity.admin_rights:
cadmin += 1
if entity.creator:
ccreator += 1
elif entity.megagroup:
groups += 1
if entity.creator or entity.admin_rights:
gadmin += 1
if entity.creator:
gcreator += 1
elif isinstance(entity, Chat):
groups += 1
if entity.creator or entity.admin_rights:
gadmin += 1
if entity.creator:
gcreator += 1
elif isinstance(entity, User):
pms += 1
if entity.bot:
bots +=1
mentions += dialog.unread_mentions_count
unread += dialog.unread_count
me = await client.get_me()
name = (me.first_name)
rpl = f'**Stats for {name}**\n'
rpl += f'**Private:** {pms} ({pms - bots} users / {bots} bots)\n'
rpl += f'**Groups:** {groups} ({gadmin} admin / {gcreator} creator)\n'
rpl += f'**Channels:** {chans} ({cadmin} admin / {ccreator} creator)\n'
rpl += f'**Unread:** {unread} ({mentions} mentions)\n'
await event.edit(rpl)
if msg.startswith('.fire'):
rpl = 'IMA FIRING MAH LAZER'
await event.edit(rpl)
if msg.startswith('.purgeme'):
count = int(msg.split()[1])
i = 1
async for msgs in client.iter_messages(event.chat_id, from_user='me'):
if (i > count + 1):
break
i = i + 1
await msgs.delete()
rpl = "Purge complete! Purged " + str(count) + " messages."
await client.send_message(logchat, rpl)
if msg.startswith('.sh'):
if event.is_channel and not event.is_group:
await event.edit('Sorry, this is not allowed!')
rpl = f'Someone attempted to execute a command on a channel.\n'
rpl += f'Message: `{msg}`\n'
rpl += f'Execution aborted.\n'
await client.send_message(logchat, rpl)
return
import os
cmd = msg.split(' ', 1)[1]
host = os.uname()[1]
user = os.environ['USER']
out = os.popen(cmd + ' 2>&1').read()
rpl = f'`{user}@{host}$ {cmd}`\n'
rpl += f'`{out}`\n'
await event.edit(rpl)
if msg.startswith('.sys'):
import os
from telethon import version
from platform import python_version
mtot = open('/proc/meminfo').readlines()[0].split()[1]
mavl = open('/proc/meminfo').readlines()[2].split()[1]
mfree = (int(mtot) - int(mavl)) / 1024
mem = round(mfree, 1)
disk = os.popen('df -h /').readlines()[1].split()[2]
kernel = os.popen('uname -r').read()
rpl = f'**System usage stats:**\n'
rpl += f'**Memory:** {mem}M\n'
rpl += f'**Disk:** {disk}\n'
rpl += f'**Kernel:** {kernel}\n'
rpl += f'**Telethon:** {version.__version__}\n'
rpl += f'**Python:** {python_version()}\n'
await event.edit(rpl)
if msg.startswith('.sauce'):
rpl = 'Sauce: [ghnou/ubot](https://git.ghnou.su/ghnou/ubot)'
await event.edit(rpl)
if msg.startswith('.help'):
rpl = '**Available userbot commands:**\n'
rpl += '.alive - Check whether the bot is running.\n'
rpl += '.shg - ¯\_(ツ)_/¯\n'
rpl += '.stats - Get user account stats.\n'
rpl += '.fire - IMA FIRING MAH LAZER\n'
rpl += '.purgeme - Purge messages.\n'
rpl += '.sh - Execute a shell command.\n'
rpl += '.sys - Show system information.\n'
rpl += '.sauce - Get the bot\'s source code.\n'
rpl += '.help - Show this help message.\n'
await event.edit(rpl)
client.start()
client.run_until_disconnected()