Skip to content

Commit b4f745d

Browse files
Update add_chat_members method return type
Closes #242
1 parent 6f8f1ab commit b4f745d

5 files changed

Lines changed: 73 additions & 7 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ def get_title_list(s: str) -> list:
655655
Dialog
656656
Restriction
657657
EmojiStatus
658+
FailedToAddMember
658659
Folder
659660
GroupCallMember
660661
ChatColor

pyrogram/methods/chats/add_chat_members.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Union, List
2020

2121
import pyrogram
22-
from pyrogram import raw
22+
from pyrogram import raw, types
2323

2424

2525
class AddChatMembers:
@@ -28,8 +28,9 @@ async def add_chat_members(
2828
chat_id: Union[int, str],
2929
user_ids: Union[Union[int, str], List[Union[int, str]]],
3030
forward_limit: int = 100
31-
) -> bool:
32-
"""Add new chat members to a group, supergroup or channel
31+
) -> List["types.FailedToAddMember"]:
32+
"""Add new chat members to a group, supergroup or channel.
33+
This method can't be used to join a chat. Members can't be added to a channel if it has more than 200 members.
3334
3435
.. include:: /_includes/usable-by/users.rst
3536
@@ -48,7 +49,7 @@ async def add_chat_members(
4849
Defaults to 100 (max amount).
4950
5051
Returns:
51-
``bool``: On success, True is returned.
52+
List of :obj:`~pyrogram.types.FailedToAddMember`: On success, an empty list is returned, otherwise a list of :obj:`~pyrogram.types.FailedToAddMember` is returned.
5253
5354
Example:
5455
.. code-block:: python
@@ -67,17 +68,21 @@ async def add_chat_members(
6768
if not isinstance(user_ids, list):
6869
user_ids = [user_ids]
6970

71+
missing_invitees = []
72+
7073
if isinstance(peer, raw.types.InputPeerChat):
7174
for user_id in user_ids:
72-
await self.invoke(
75+
r = await self.invoke(
7376
raw.functions.messages.AddChatUser(
7477
chat_id=peer.chat_id,
7578
user_id=await self.resolve_peer(user_id),
7679
fwd_limit=forward_limit
7780
)
7881
)
82+
83+
missing_invitees.extend(r.missing_invitees)
7984
else:
80-
await self.invoke(
85+
r = await self.invoke(
8186
raw.functions.channels.InviteToChannel(
8287
channel=peer,
8388
users=[
@@ -87,4 +92,6 @@ async def add_chat_members(
8792
)
8893
)
8994

90-
return True
95+
missing_invitees.extend(r.missing_invitees)
96+
97+
return types.List(types.FailedToAddMember._parse(inv) for inv in missing_invitees)

pyrogram/types/messages_and_media/message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,7 @@ async def reply_text(
18821882
Use as a shortcut for:
18831883
18841884
.. code-block:: python
1885+
18851886
from pyrogram import types
18861887
18871888
await client.send_message(

pyrogram/types/user_and_chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from .chat_settings import ChatSettings
4444
from .dialog import Dialog
4545
from .emoji_status import EmojiStatus
46+
from .failed_to_add_member import FailedToAddMember
4647
from .folder import Folder
4748
from .found_contacts import FoundContacts
4849
from .global_privacy_settings import GlobalPrivacySettings
@@ -103,6 +104,7 @@
103104
"ChatAdministratorRights",
104105
"ChatJoiner",
105106
"EmojiStatus",
107+
"FailedToAddMember",
106108
"Folder",
107109
"FoundContacts",
108110
"GlobalPrivacySettings",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Optional
20+
21+
from pyrogram import raw
22+
from ..object import Object
23+
24+
25+
class FailedToAddMember(Object):
26+
"""Contains information about a user that has failed to be added to a chat.
27+
28+
Parameters:
29+
user_id (``int``):
30+
User identifier.
31+
32+
premium_would_allow_invite (``bool``, *optional*):
33+
True, if subscription to Telegram Premium would have allowed to add the user to the chat.
34+
35+
premium_required_to_send_messages (``bool``, *optional*):
36+
True, if subscription to Telegram Premium is required to send the user chat invite link.
37+
"""
38+
def __init__(
39+
self,
40+
*,
41+
user_id: int,
42+
premium_would_allow_invite: Optional[bool] = None,
43+
premium_required_to_send_messages: Optional[bool] = None,
44+
):
45+
self.user_id = user_id
46+
self.premium_would_allow_invite = premium_would_allow_invite
47+
self.premium_required_to_send_messages = premium_required_to_send_messages
48+
49+
@staticmethod
50+
def _parse(missing_invite: "raw.types.MissingInvitee") -> "FailedToAddMember":
51+
return FailedToAddMember(
52+
user_id=missing_invite.user_id,
53+
premium_would_allow_invite=missing_invite.premium_would_allow_invite,
54+
premium_required_to_send_messages=missing_invite.premium_required_for_pm
55+
)

0 commit comments

Comments
 (0)