PagerMaid-Pyro/pagermaid/modules/account.py

123 lines
5.0 KiB
Python
Raw Normal View History

2022-05-23 12:40:30 +00:00
from os import remove
from pyrogram.errors import UsernameNotOccupied, PeerIdInvalid
from pyrogram.types import User
from pagermaid import Config
from pagermaid.listener import listener
from pagermaid.utils import lang, Message
2022-06-20 13:55:14 +00:00
import contextlib
2022-05-23 12:40:30 +00:00
2022-06-20 13:55:14 +00:00
@listener(is_plugin=False, command="profile",
2022-05-23 12:40:30 +00:00
description=lang('profile_des'),
parameters="<username>")
2022-06-20 13:55:14 +00:00
async def profile(message: Message):
2022-05-23 12:40:30 +00:00
""" Queries profile of a user. """
if len(message.parameter) > 1:
await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
if not Config.SILENT:
2022-06-08 03:18:21 +00:00
message = await message.edit(lang('profile_process'))
2022-05-23 12:40:30 +00:00
if message.reply_to_message:
user = message.reply_to_message.from_user
if not user:
return await message.edit(f"{lang('error_prefix')}{lang('profile_e_no')}")
else:
if len(message.parameter) == 1:
user = message.parameter[0]
if user.isdigit():
user = int(user)
else:
2022-06-20 13:55:14 +00:00
user = await message.bot.get_me()
2022-05-23 12:40:30 +00:00
if message.entities is not None:
if message.entities[0].type == "text_mention":
user = message.entities[0].user
elif message.entities[0].type == "phone_number":
user = int(message.parameter[0])
else:
return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
if not isinstance(user, User):
try:
2022-06-20 13:55:14 +00:00
user = await message.bot.get_users(user)
2022-05-23 12:40:30 +00:00
except PeerIdInvalid:
return await message.edit(f"{lang('error_prefix')}{lang('profile_e_nof')}")
except UsernameNotOccupied:
return await message.edit(f"{lang('error_prefix')}{lang('profile_e_nou')}")
except OverflowError:
return await message.edit(f"{lang('error_prefix')}{lang('profile_e_long')}")
except Exception as exception:
raise exception
user_type = "Bot" if user.is_bot else lang('profile_user')
username_system = f"@{user.username}" if user.username is not None else lang('profile_noset')
if not user.first_name:
await message.edit(f"{lang('error_prefix')}{lang('profile_e_no')}")
return
first_name = user.first_name.replace("\u2060", "")
last_name = user.last_name.replace("\u2060", "") if user.last_name is not None else lang('profile_noset')
verified = lang('profile_yes') if user.is_verified else lang('profile_no')
restricted = lang('profile_yes') if user.is_restricted else lang('profile_no')
caption = f"**{lang('profile_name')}:** \n" \
f"{lang('profile_username')}: {username_system} \n" \
f"ID: {user.id} \n" \
f"{lang('profile_fname')}: {first_name} \n" \
f"{lang('profile_lname')}: {last_name} \n" \
f"{lang('profile_verified')}: {verified} \n" \
f"{lang('profile_restricted')}: {restricted} \n" \
f"{lang('profile_type')}: {user_type} \n" \
f"[{first_name}](tg://user?id={user.id})"
2022-06-20 13:55:14 +00:00
photo = await message.bot.download_media(user.photo.big_file_id)
2022-05-23 12:40:30 +00:00
reply_to = message.reply_to_message
try:
2022-06-20 13:55:14 +00:00
await message.bot.send_photo(
2022-05-23 12:40:30 +00:00
message.chat.id,
photo,
caption=caption,
reply_to_message_id=reply_to.id if reply_to else None
)
await message.delete()
return remove(photo)
except TypeError:
await message.edit(caption)
@listener(is_plugin=False, outgoing=True, command="block",
need_admin=True,
description=lang('block_des'),
parameters="(username/uid/reply)")
2022-06-20 13:55:14 +00:00
async def block_user(message: Message):
2022-05-23 12:40:30 +00:00
""" Block a user. """
if len(message.parameter) > 1:
await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
if not Config.SILENT:
2022-06-08 03:18:21 +00:00
message = await message.edit(lang('block_process'))
2022-05-25 11:26:50 +00:00
user = message.obtain_user()
2022-05-23 12:40:30 +00:00
if not user:
return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
2022-06-20 13:55:14 +00:00
with contextlib.suppress(Exception):
if await message.bot.block_user(user):
2022-06-08 03:18:21 +00:00
return await message.edit(f"{lang('block_success')} `{user}`")
2022-05-23 12:40:30 +00:00
await message.edit(f"`{user}` {lang('block_exist')}")
@listener(is_plugin=False, outgoing=True, command="unblock",
need_admin=True,
description=lang('unblock_des'),
parameters="<username/uid/reply>")
2022-06-20 13:55:14 +00:00
async def unblock_user(message: Message):
2022-05-23 12:40:30 +00:00
""" Unblock a user. """
if len(message.parameter) > 1:
await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
return
if not Config.SILENT:
2022-06-08 03:18:21 +00:00
message = await message.edit(lang('unblock_process'))
2022-05-25 11:26:50 +00:00
user = message.obtain_user()
2022-05-23 12:40:30 +00:00
if not user:
return await message.edit(f"{lang('error_prefix')}{lang('arg_error')}")
2022-06-20 13:55:14 +00:00
with contextlib.suppress(Exception):
if await message.bot.unblock_user(user):
2022-06-08 03:18:21 +00:00
return await message.edit(f"{lang('unblock_success')} `{user}`")
2022-05-23 12:40:30 +00:00
await message.edit(f"`{user}` {lang('unblock_exist')}")