|
| 1 | +# Pyrofork - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan> |
| 3 | +# |
| 4 | +# This file is part of Pyrofork. |
| 5 | +# |
| 6 | +# Pyrofork 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 | +# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import Union |
| 20 | +import pyrogram |
| 21 | +from pyrogram import raw, utils |
| 22 | + |
| 23 | + |
| 24 | +class TransferChatOwnership: |
| 25 | + async def transfer_chat_ownership( |
| 26 | + self: "pyrogram.Client", |
| 27 | + chat_id: Union[int, str], |
| 28 | + user_id: Union[int, str], |
| 29 | + password: str, |
| 30 | + ) -> bool: |
| 31 | + """Transfer the owner of a chat or channel to another user. |
| 32 | +
|
| 33 | + .. note: |
| 34 | +
|
| 35 | + Requires owner privileges. |
| 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 | + Unique identifier for the target chat in form of a *t.me/joinchat/* link, identifier (int) or username |
| 43 | + of the target channel/supergroup (in the format @username). |
| 44 | +
|
| 45 | + user_id (``int`` | ``str``): |
| 46 | + Unique identifier (int) or username (str) of the new owner. |
| 47 | + For a contact that exists in your Telegram address book you can use his phone number (str). |
| 48 | +
|
| 49 | + password (``str``): |
| 50 | + The 2-step verification password of the current user. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + ``bool``: True on success. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + ValueError: In case of invalid parameters. |
| 57 | + RPCError: In case of a Telegram RPC error. |
| 58 | +
|
| 59 | + Example: |
| 60 | + .. code-block:: python |
| 61 | +
|
| 62 | + await app.transfer_chat_ownership(chat_id, user_id, "password") |
| 63 | + """ |
| 64 | + peer_channel = await self.resolve_peer(chat_id) |
| 65 | + peer_user = await self.resolve_peer(user_id) |
| 66 | + |
| 67 | + if not isinstance(peer_channel, raw.types.InputPeerChannel): |
| 68 | + raise ValueError("The chat_id must belong to a channel/supergroup.") |
| 69 | + |
| 70 | + if not isinstance(peer_user, raw.types.InputPeerUser): |
| 71 | + raise ValueError("The user_id must belong to a user.") |
| 72 | + |
| 73 | + r = await self.invoke( |
| 74 | + raw.functions.channels.EditCreator( |
| 75 | + channel=peer_channel, |
| 76 | + user_id=peer_user, |
| 77 | + password=utils.compute_password_check( |
| 78 | + await self.invoke(raw.functions.account.GetPassword()), password |
| 79 | + ), |
| 80 | + ) |
| 81 | + ) |
| 82 | + |
| 83 | + return bool(r) |
0 commit comments