Пример #1
0
    def unban_chat_member(self, chat_id: int or str, user_id: int or str):
        """Use this method to unban a previously kicked user in a supergroup or channel.
        The user will **not** return to the group or channel automatically, but will be able to join via link, etc.
        You must be an administrator for this to work.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                For a private channel/supergroup you can use its *t.me/joinchat/* link.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

        Returns:
            True on success.

        Raises:
            :class:`Error <pyrogram.Error>`
        """
        self.send(
            functions.channels.EditBanned(
                channel=self.resolve_peer(chat_id),
                user_id=self.resolve_peer(user_id),
                banned_rights=types.ChannelBannedRights(until_date=0)))

        return True
Пример #2
0
    def kick_chat_member(self,
                         chat_id: int or str,
                         user_id: int or str,
                         until_date: int = 0):
        """Use this method to kick a user from a group, a supergroup or a channel.
        In the case of supergroups and channels, the user will not be able to return to the group on their own using
        invite links, etc., unless unbanned first. You must be an administrator in the chat for this to work and must
        have the appropriate admin rights.

        Note:
            In regular groups (non-supergroups), this method will only work if the "All Members Are Admins" setting is
            off in the target group. Otherwise members may only be removed by the group's creator or by the member
            that added them.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

            until_date (``int``, *optional*):
                Date when the user will be unbanned, unix time.
                If user is banned for more than 366 days or less than 30 seconds from the current time they are
                considered to be banned forever. Defaults to 0 (ban forever).

        Returns:
            True on success.

        Raises:
            :class:`Error <pyrogram.Error>`
        """
        chat_peer = self.resolve_peer(chat_id)
        user_peer = self.resolve_peer(user_id)

        if isinstance(chat_peer, types.InputPeerChannel):
            self.send(
                functions.channels.EditBanned(
                    channel=chat_peer,
                    user_id=user_peer,
                    banned_rights=types.ChannelBannedRights(
                        until_date=until_date,
                        view_messages=True,
                        send_messages=True,
                        send_media=True,
                        send_stickers=True,
                        send_gifs=True,
                        send_games=True,
                        send_inline=True,
                        embed_links=True)))
        else:
            self.send(
                functions.messages.DeleteChatUser(chat_id=abs(chat_id),
                                                  user_id=user_peer))

        return True
Пример #3
0
    def restrict_chat_member(self,
                             chat_id: Union[int, str],
                             user_id: Union[int, str],
                             until_date: int = 0,
                             can_send_messages: bool = False,
                             can_send_media_messages: bool = False,
                             can_send_other_messages: bool = False,
                             can_add_web_page_previews: bool = False) -> bool:
        """Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for
        this to work and must have the appropriate admin rights. Pass True for all boolean parameters to lift
        restrictions from a user.

        Args:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.

            user_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target user.
                For a contact that exists in your Telegram address book you can use his phone number (str).

            until_date (``int``, *optional*):
                Date when the user will be unbanned, unix time.
                If user is banned for more than 366 days or less than 30 seconds from the current time they are
                considered to be banned forever. Defaults to 0 (ban forever).

            can_send_messages (``bool``, *optional*):
                Pass True, if the user can send text messages, contacts, locations and venues.

            can_send_media_messages (``bool``, *optional*):
                Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes,
                implies can_send_messages.

            can_send_other_messages (``bool``, *optional*):
                Pass True, if the user can send animations, games, stickers and use inline bots,
                implies can_send_media_messages.

            can_add_web_page_previews (``bool``, *optional*):
                Pass True, if the user may add web page previews to their messages, implies can_send_media_messages

        Returns:
            True on success.

        Raises:
            :class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
        """
        send_messages = True
        send_media = True
        send_stickers = True
        send_gifs = True
        send_games = True
        send_inline = True
        embed_links = True

        if can_send_messages:
            send_messages = None

        if can_send_media_messages:
            send_messages = None
            send_media = None

        if can_send_other_messages:
            send_media = None
            send_stickers = None
            send_gifs = None
            send_games = None
            send_inline = None

        if can_add_web_page_previews:
            send_media = None
            embed_links = None

        self.send(
            functions.channels.EditBanned(
                channel=self.resolve_peer(chat_id),
                user_id=self.resolve_peer(user_id),
                banned_rights=types.ChannelBannedRights(
                    until_date=until_date,
                    send_messages=send_messages,
                    send_media=send_media,
                    send_stickers=send_stickers,
                    send_gifs=send_gifs,
                    send_games=send_games,
                    send_inline=send_inline,
                    embed_links=embed_links)))

        return True