From 25b15bc044bc74bbbcca97103df32c4fed787152 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 2 Jan 2023 13:27:53 +0800 Subject: [PATCH] =?UTF-8?q?pfp=20=E5=BF=AB=E9=80=9F=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pfp/main.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pfp/main.py diff --git a/pfp/main.py b/pfp/main.py new file mode 100644 index 0000000..7e8a101 --- /dev/null +++ b/pfp/main.py @@ -0,0 +1,72 @@ +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 + +from pagermaid import Config +from pagermaid.enums import Client, Message +from pagermaid.listener import listener +from pagermaid.single_utils import safe_remove +from pagermaid.utils import lang + + +async def get_photo(message: Message) -> Optional[str]: + if reply := message.reply_to_message: + if reply.photo: + return await reply.download() + elif reply.document and reply.document.mime_type and reply.document.mime_type.startswith("image"): + return await reply.download() + elif message.photo: + return await message.download() + elif message.document and message.document.mime_type and message.document.mime_type.startswith("image"): + 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) + + +@listener( + command="pfp", + description=lang("pfp_des") +) +async def pfp(client: Client, message: Message): + """ Sets your profile picture. """ + 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: + message = await message.edit(lang('pfp_process')) + 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')}")