|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import Union |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import raw |
| 23 | +from pyrogram.utils import compute_password_check |
| 24 | + |
| 25 | + |
| 26 | +class TransferChatOwnership: |
| 27 | + async def transfer_chat_ownership( |
| 28 | + self: "pyrogram.Client", |
| 29 | + chat_id: Union[int, str], |
| 30 | + user_id: Union[int, str], |
| 31 | + password: str, |
| 32 | + ) -> bool: |
| 33 | + """Changes the owner of a chat. |
| 34 | + |
| 35 | + Requires owner privileges in the chat. Available only for supergroups and channel chats. |
| 36 | +
|
| 37 | + .. include:: /_includes/usable-by/users.rst |
| 38 | +
|
| 39 | + Parameters: |
| 40 | + chat_id (``int`` | ``str``): |
| 41 | + Unique identifier (int) or username (str) of the target chat. |
| 42 | +
|
| 43 | + user_id (``int`` | ``str``): |
| 44 | + Unique identifier (int) or username (str) of the new owner. |
| 45 | + The ownership can't be transferred to a bot or to a deleted user. |
| 46 | +
|
| 47 | + password (``str``): |
| 48 | + The 2-step verification password of the current user. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + ``bool``: True on success. |
| 52 | +
|
| 53 | + Raises: |
| 54 | + ValueError: In case of invalid parameters. |
| 55 | + RPCError: In case of a Telegram RPC error. |
| 56 | +
|
| 57 | + Example: |
| 58 | + .. code-block:: python |
| 59 | +
|
| 60 | + await app.transfer_chat_ownership(chat_id, user_id, "password") |
| 61 | + """ |
| 62 | + |
| 63 | + peer_channel = await self.resolve_peer(chat_id) |
| 64 | + peer_user = await self.resolve_peer(user_id) |
| 65 | + |
| 66 | + if not isinstance(peer_channel, raw.types.InputPeerChannel): |
| 67 | + raise ValueError("The chat_id must belong to a channel/supergroup.") |
| 68 | + |
| 69 | + if not isinstance(peer_user, raw.types.InputPeerUser): |
| 70 | + raise ValueError("The user_id must belong to a user.") |
| 71 | + |
| 72 | + r = await self.invoke( |
| 73 | + raw.functions.channels.EditCreator( |
| 74 | + channel=peer_channel, |
| 75 | + user_id=peer_user, |
| 76 | + password=compute_password_check( |
| 77 | + await self.invoke(raw.functions.account.GetPassword()), password |
| 78 | + ), |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + return bool(r) |
0 commit comments