ubot/ubot.py

168 lines
4.4 KiB
Python
Raw Normal View History

2021-03-26 20:00:10 +01:00
#!/usr/bin/python3
2021-03-14 18:50:09 +01:00
from telethon import TelegramClient, events
2021-03-26 21:51:02 +01:00
from config import *
2021-03-14 18:50:09 +01:00
client = TelegramClient('ubot', api_id, api_hash)
@client.on(events.NewMessage)
async def edit(event):
2021-03-14 19:15:50 +01:00
msg = event.raw_text
2021-03-14 18:50:09 +01:00
if event.sender_id != myid:
return
2021-03-22 10:53:11 +01:00
if msg.startswith('.alive'):
2021-03-14 19:15:50 +01:00
rpl = "Userbot alive and well!"
2021-03-15 01:48:29 +01:00
await event.edit(rpl)
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
if '.shg' in msg:
2021-03-22 10:47:38 +01:00
rpl = "¯\_(ツ)_/¯"
2021-03-14 19:15:50 +01:00
msg = event.text.replace('.shg', rpl)
await event.edit(msg)
2021-03-14 18:50:09 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.stats'):
from telethon.tl.custom import Dialog
from telethon.tl.types import Channel, User, Chat
2021-03-14 19:15:50 +01:00
pms = bots = 0
groups = gadmin = gcreator = 0
chans = cadmin = ccreator = 0
unread = mentions = 0
dialog: Dialog
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
async for dialog in event.client.iter_dialogs():
entity = dialog.entity
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
if isinstance(entity, Channel):
if entity.broadcast:
chans += 1
if entity.creator or entity.admin_rights:
cadmin += 1
if entity.creator:
ccreator += 1
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
elif entity.megagroup:
groups += 1
if entity.creator or entity.admin_rights:
gadmin += 1
if entity.creator:
gcreator += 1
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
elif isinstance(entity, Chat):
groups += 1
if entity.creator or entity.admin_rights:
gadmin += 1
if entity.creator:
gcreator += 1
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
elif isinstance(entity, User):
pms += 1
if entity.bot:
bots +=1
2021-03-14 18:50:09 +01:00
2021-03-14 19:15:50 +01:00
mentions += dialog.unread_mentions_count
unread += dialog.unread_count
2021-03-14 18:50:09 +01:00
2021-03-16 22:34:53 +01:00
me = await client.get_me()
name = (me.first_name)
2021-03-15 01:33:40 +01:00
rpl = f'**Stats for {name}**\n'
2021-03-16 21:42:57 +01:00
rpl += f'**Private:** {pms} ({pms - bots} users / {bots} bots)\n'
2021-03-15 01:33:40 +01:00
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'
2021-03-14 19:15:50 +01:00
await event.edit(rpl)
2021-03-14 18:50:09 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.fire'):
2021-03-14 19:15:50 +01:00
rpl = 'IMA FIRING MAH LAZER'
2021-03-15 01:48:29 +01:00
await event.edit(rpl)
2021-03-21 13:09:50 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.purgeme'):
2021-03-21 13:09:50 +01:00
count = int(msg.split()[1])
i = 1
async for msgs in client.iter_messages(event.chat_id, from_user='me'):
if (i > count + 1):
2021-03-22 11:04:47 +01:00
break
2021-03-21 13:09:50 +01:00
i = i + 1
await msgs.delete()
rpl = "Purge complete! Purged " + str(count) + " messages."
2021-03-28 23:17:31 +02:00
await client.send_message(logchat, rpl)
2021-03-15 01:46:15 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.sh'):
2021-03-22 11:04:47 +01:00
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'
2021-03-28 23:17:31 +02:00
await client.send_message(logchat, rpl)
2021-03-22 11:04:47 +01:00
return
2021-03-26 22:00:54 +01:00
import os
2021-03-22 11:04:47 +01:00
cmd = msg.split(' ', 1)[1]
host = os.uname()[1]
user = os.environ['USER']
2021-03-30 00:30:25 +02:00
out = os.popen(cmd + ' 2>&1').read()
2021-03-22 11:04:47 +01:00
rpl = f'`{user}@{host}$ {cmd}`\n'
rpl += f'`{out}`\n'
await event.edit(rpl)
2021-03-22 10:25:15 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.sys'):
2021-03-26 23:01:07 +01:00
import os
from telethon import version
from platform import python_version
2021-03-26 23:01:07 +01:00
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)
2021-03-22 10:25:15 +01:00
disk = os.popen('df -h /').readlines()[1].split()[2]
2021-04-06 15:59:26 +02:00
kernel = os.popen('uname -r').read()
2021-03-22 10:25:15 +01:00
2021-03-22 10:47:38 +01:00
rpl = f'**System usage stats:**\n'
rpl += f'**Memory:** {mem}M\n'
rpl += f'**Disk:** {disk}\n'
2021-04-06 16:18:18 +02:00
rpl += f'**Kernel:** {kernel}'
rpl += f'**Telethon:** {version.__version__}\n'
rpl += f'**Python:** {python_version()}\n'
2021-03-22 10:25:15 +01:00
await event.edit(rpl)
if msg.startswith('.spam'):
cnt = int(msg.split(' ', 2)[1])
msg = msg.split(' ', 2)[2]
for i in range(0,cnt):
await event.respond(msg)
2021-04-21 20:25:58 +02:00
if msg.startswith('.re'):
import os
regex = str(msg.split(' ', 1)[1])
inmsg = await event.get_reply_message()
outmsg = os.popen('echo "' + str(inmsg.text) + '" | sed "' + regex + '"').read()
rpl = f'`{regex}`\n'
rpl += f'{outmsg}\n'
await event.edit(rpl)
2021-03-21 20:37:58 +01:00
2021-03-22 10:53:11 +01:00
if msg.startswith('.sauce'):
2021-04-21 01:36:25 +02:00
rpl = 'Sauce: [ghnou/ubot](https://git.nixmagic.com/ghnou/ubot)'
2021-03-21 13:16:44 +01:00
await event.edit(rpl)
2021-03-22 10:53:11 +01:00
if msg.startswith('.help'):
2021-03-16 21:42:57 +01:00
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'
2021-03-21 13:17:42 +01:00
rpl += '.purgeme - Purge messages.\n'
2021-03-21 20:37:58 +01:00
rpl += '.sh - Execute a shell command.\n'
2021-03-30 00:48:03 +02:00
rpl += '.sys - Show system information.\n'
rpl += '.spam - Spam messages.. yea.\n'
2021-04-21 20:25:58 +02:00
rpl += '.re - s/foo/bar/\n'
2021-03-16 21:42:57 +01:00
rpl += '.sauce - Get the bot\'s source code.\n'
2021-03-21 13:09:50 +01:00
rpl += '.help - Show this help message.\n'
2021-03-16 21:42:57 +01:00
await event.edit(rpl)
2021-03-14 18:50:09 +01:00
client.start()
client.run_until_disconnected()