PagerMaid_Plugins_Pyro/pfp/main.py

77 lines
2.6 KiB
Python
Raw Permalink Normal View History

2023-01-02 05:27:53 +00:00
import contextlib
from typing import Optional
from pyrogram.enums import ChatType
from pyrogram.errors import PhotoCropSizeSmall, ImageProcessFailed, BadRequest
from pyrogram.raw.functions.photos import UploadContactProfilePhoto
from pyrogram.raw.types import InputUser
2024-09-28 14:35:08 +00:00
from pagermaid.config import Config
2023-01-02 05:27:53 +00:00
from pagermaid.enums import Client, Message
from pagermaid.listener import listener
2024-09-28 14:35:08 +00:00
from pagermaid.utils import lang, safe_remove
2023-01-02 05:27:53 +00:00
async def get_photo(message: Message) -> Optional[str]:
if reply := message.reply_to_message:
if reply.photo:
return await reply.download()
2023-07-01 12:18:58 +00:00
elif (
reply.document
and reply.document.mime_type
and reply.document.mime_type.startswith("image")
):
2023-01-02 05:27:53 +00:00
return await reply.download()
elif message.photo:
return await message.download()
2023-07-01 12:18:58 +00:00
elif (
message.document
and message.document.mime_type
and message.document.mime_type.startswith("image")
):
2023-01-02 05:27:53 +00:00
return await message.download()
return None
async def set_photo(client: Client, user: InputUser, photo: str, me: bool) -> None:
if me:
await client.set_profile_photo(photo=photo)
else:
try:
await client.invoke(
UploadContactProfilePhoto(
user_id=user,
suggest=False,
save=True,
file=await client.save_file(photo),
)
)
except BadRequest:
await set_photo(client, user, photo, True)
safe_remove(photo)
2023-07-01 12:18:58 +00:00
@listener(command="pfp", description=lang("pfp_des"))
2023-01-02 05:27:53 +00:00
async def pfp(client: Client, message: Message):
2023-07-01 12:18:58 +00:00
"""Sets your profile picture."""
2023-01-02 05:27:53 +00:00
me = await client.get_me()
peer = await client.resolve_peer(me.id)
with contextlib.suppress(Exception):
if message.chat.type == ChatType.PRIVATE:
peer = await client.resolve_peer(message.chat.id)
photo = await get_photo(message)
if not photo:
return await message.edit(f"{lang('error_prefix')}{lang('pfp_e_notp')}")
if not Config.SILENT:
2023-07-01 12:18:58 +00:00
message = await message.edit(lang("pfp_process"))
2023-01-02 05:27:53 +00:00
try:
await set_photo(client, peer, photo, peer.user_id == me.id)
await message.edit("头像修改成功啦 ~")
return
except PhotoCropSizeSmall:
await message.edit(f"{lang('error_prefix')}{lang('pfp_e_size')}")
except ImageProcessFailed:
await message.edit(f"{lang('error_prefix')}{lang('pfp_e_img')}")
except Exception:
await message.edit(f"{lang('error_prefix')}{lang('pfp_e_notp')}")