Skip to content

Commit 2e42fba

Browse files
Otazuki004wulan17
authored andcommitted
pyrofork: Add transfer_chat_ownership method (#145)
Signed-off-by: wulan17 <wulan17@komodos.id>
1 parent 5413197 commit 2e42fba

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def get_title_list(s: str) -> list:
285285
hide_general_topic
286286
reopen_forum_topic
287287
reopen_general_topic
288+
transfer_chat_ownership
288289
unhide_general_topic
289290
update_color
290291
update_folder

pyrogram/methods/chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from .set_chat_username import SetChatUsername
6868
from .set_send_as_chat import SetSendAsChat
6969
from .set_slow_mode import SetSlowMode
70+
from .transfer_chat_ownership import TransferChatOwnership
7071
from .unarchive_chats import UnarchiveChats
7172
from .unban_chat_member import UnbanChatMember
7273
from .unpin_all_chat_messages import UnpinAllChatMessages
@@ -130,6 +131,7 @@ class Chats(
130131
GetSendAsChats,
131132
SetSendAsChat,
132133
SetChatProtectedContent,
134+
TransferChatOwnership,
133135
UpdateColor,
134136
UpdateFolder
135137
):
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)