3
uQh.F                 @   sD   d dl mZmZmZ ddlmZ ddlmZ eG dd deZdS )   )EventBuilderEventCommonname_inner_event   )utils)typesc               @   s.   e Zd ZdZedddZG dd deZdS )
ChatActionaO  
    Occurs on certain chat actions:

    * Whenever a new chat is created.
    * Whenever a chat's title or photo is changed or removed.
    * Whenever a new message is pinned.
    * Whenever a user scores in a game.
    * Whenever a user joins or is added to the group.
    * Whenever a user is removed or leaves a group if it has
      less than 50 members or the removed user was a bot.

    Note that "chat" refers to "small group, megagroup and broadcast
    channel", whereas "group" refers to "small group and megagroup" only.

    Example
        .. code-block:: python

            from telethon import events

            @client.on(events.ChatAction)
            async def handler(event):
                # Welcome every new user
                if event.user_joined:
                    await event.reply('Welcome to the group!')
    Nc             C   s  t |tjr0|j r0| jtj|j|j|jdS t |tjrZ|j rZ| j|j	|j|jdS t |tj
r| jtj|j|jp|d|jdS t |tjr| jtj|jd|jdS t |tjtjfot |jtjrZ|j}|jj}t |tj r| j|d|jdS t |tjr2|jg|jkp|j}| j|||jdS t |tjrf| j||jrZtj|jnd|jdS t |tjr| j||jd|jdS t |tjr| j|d|j|jdS t |tj r| j||j|jdS t |tj!r| j||j|j"dS t |tj#r| j||jddS t |tj$r:|j%r:| j||j&gd	S t |tj'r| j||j(d
S n\t |tj)rt*|j+t*|j,kr| jtj|j|j|j+r|j-nd |j,r|j-nd dS d S )N)pin_idspinT)added_byusers)	kicked_byr   )r   created	new_title)r   r   r   )r   r   )r   	new_photo)r	   )	new_score)r   r   r   ).
isinstancer   ZUpdatePinnedChannelMessagesZpinnedEventZPeerChannelZ
channel_idmessagesZUpdatePinnedMessagesZpeerZUpdateChatParticipantAddZPeerChatZchat_idZ
inviter_iduser_idZUpdateChatParticipantDeleteZUpdateNewMessageZUpdateNewChannelMessagemessageMessageServiceactionZMessageActionChatJoinedByLinkZfrom_idZMessageActionChatAddUserZ	sender_idr   ZMessageActionChatDeleteUserr   get_peer_idZMessageActionChatCreatetitleZMessageActionChannelCreateZMessageActionChatEditTitleZMessageActionChatEditPhotophotoZMessageActionChatDeletePhotoZMessageActionPinMessagereply_toZreply_to_msg_idZMessageActionGameScoreZscoreZUpdateChannelParticipantboolZnew_participantZprev_participantZactor_id)clsupdateZothersZself_idmsgr   r    r!   >/tmp/pip-build-2nz6shyl/telethon/telethon/events/chataction.pybuild"   s    










zChatAction.buildc            	       s   e Zd ZdZd- fdd	Z fddZdd Zd	d
 Zdd Zdd Z	dd Z
edd Zdd Zedd Zdd Zedd Zdd Zedd Zdd  Zed!d" Zed#d$ Zd%d& Zed'd( Zd)d* Zed+d, Z  ZS ).zChatAction.Eventa  
        Represents the event of a new chat action.

        Members:
            action_message  (`MessageAction <https://tl.telethon.dev/types/message_action.html>`_):
                The message invoked by this Chat Action.

            new_pin (`bool`):
                `True` if there is a new pin.

            new_photo (`bool`):
                `True` if there's a new chat photo (or it was removed).

            photo (:tl:`Photo`, optional):
                The new photo (or `None` if it was removed).

            user_added (`bool`):
                `True` if the user was added by some other.

            user_joined (`bool`):
                `True` if the user joined on their own.

            user_left (`bool`):
                `True` if the user left on their own.

            user_kicked (`bool`):
                `True` if the user was kicked by some other.

            created (`bool`, optional):
                `True` if this chat was just created.

            new_title (`str`, optional):
                The new title string for the chat, if applicable.

            new_score (`str`, optional):
                The new score string for the game, if applicable.

            unpin (`bool`):
                `True` if the existing pin gets unpinned.
        Nc                sT  t |tjr|| _|j}nd | _t j||r4|d nd d |d k	| _|| _d | _	|d k	| _
t |tjrl|nd | _d | _d | _d | _ | _ | _ | _| _|dkrd| _n|rd| _|| _|dks|d k	r||krd| _n|rd| _|| _t|| _t |trdd |D | _n|r*tj|g| _ng | _d | _d | _|| _|
| _|	 | _d S )N    )Z	chat_peerZmsg_idFTc             S   s   g | ]}t j|qS r!   )r   r   ).0ur!   r!   r"   
<listcomp>   s    z-ChatAction.Event.__init__.<locals>.<listcomp>)r   r   r   action_messageZpeer_idsuper__init__Znew_pin_pin_ids_pinned_messagesr   ZPhotor   	_added_by
_kicked_byZ
user_addedZuser_joinedZ	user_leftZuser_kickedZunpinr   r   list	_user_idsr   r   _users_input_usersr   r   )selfwherer   r   r   r   r   r   r	   r
   r   )	__class__r!   r"   r*      sD    


zChatAction.Event.__init__c                s(   t  j| | jr$| jj|| jd  d S )N)r)   _set_clientr(   Z_finish_init	_entities)r3   client)r5   r!   r"   r6      s    zChatAction.Event._set_clientc                s$   | j j| j I dH f||I dH S )z
            Responds to the chat action message (not as a reply). Shorthand for
            `telethon.client.messages.MessageMethods.send_message` with
            ``entity`` already set.
            N)_clientsend_messageget_input_chat)r3   argskwargsr!   r!   r"   respond   s    zChatAction.Event.respondc                sH   | j s| j||I dH S | j j|d< | jj| j I dH f||I dH S )a   
            Replies to the chat action message (as a reply). Shorthand for
            `telethon.client.messages.MessageMethods.send_message` with
            both ``entity`` and ``reply_to`` already set.

            Has the same effect as `respond` if there is no message.
            Nr   )r(   r>   idr9   r:   r;   )r3   r<   r=   r!   r!   r"   reply   s
    zChatAction.Event.replyc                s4   | j s
dS | jj| j I dH | j gf||I dH S )a  
            Deletes the chat action message. You're responsible for checking
            whether you have the permission to do so, or to except the error
            otherwise. Shorthand for
            `telethon.client.messages.MessageMethods.delete_messages` with
            ``entity`` and ``message_ids`` already set.

            Does nothing if no message action triggered this event.
            N)r(   r9   Zdelete_messagesr;   )r3   r<   r=   r!   r!   r"   delete   s
    
zChatAction.Event.deletec                s,   | j dkr| j I dH  | j r(| j d S dS )z
            If ``new_pin`` is `True`, this returns the `Message
            <telethon.tl.custom.message.Message>` object that was pinned.
            Nr$   )r,   get_pinned_messages)r3   r!   r!   r"   get_pinned_message
  s    
z#ChatAction.Event.get_pinned_messagec                s@   | j s| j S | j I dH }|r:| jj| j| j dI dH | _| jS )z
            If ``new_pin`` is `True`, this returns a `list` of `Message
            <telethon.tl.custom.message.Message>` objects that were pinned.
            N)ids)r+   r;   r9   Zget_messagesZ_input_chatr,   )r3   Zchatr!   r!   r"   rB     s    z$ChatAction.Event.get_pinned_messagesc             C   s:   | j r4t| j tj r4| jjtj| j }|r4|| _ | j S )zY
            The user who added ``users``, if applicable (`None` otherwise).
            )r-   r   r   Userr7   getr   r   )r3   Zabyr!   r!   r"   r   $  s
    zChatAction.Event.added_byc                s*   | j  r$| jr$| jj| jI dH | _| jS )zT
            Returns `added_by` but will make an API call if necessary.
            N)r   r-   r9   
get_entity)r3   r!   r!   r"   get_added_by0  s    zChatAction.Event.get_added_byc             C   s:   | j r4t| j tj r4| jjtj| j }|r4|| _ | j S )zZ
            The user who kicked ``users``, if applicable (`None` otherwise).
            )r.   r   r   rE   r7   rF   r   r   )r3   Zkbyr!   r!   r"   r   9  s
    zChatAction.Event.kicked_byc                s*   | j  r$| jr$| jj| jI dH | _| jS )zU
            Returns `kicked_by` but will make an API call if necessary.
            N)r   r.   r9   rG   )r3   r!   r!   r"   get_kicked_byE  s    zChatAction.Event.get_kicked_byc             C   s   | j r| jd S dS )z
            The first user that takes part in this action. For example, who joined.

            Might be `None` if the information can't be retrieved or
            there is no user taking part.
            r$   N)r   r1   )r3   r!   r!   r"   userN  s    zChatAction.Event.userc                s"   | j s| j I dH r| jd S dS )zP
            Returns `user` but will make an API call if necessary.
            Nr$   )r   	get_usersr1   )r3   r!   r!   r"   get_userY  s    zChatAction.Event.get_userc             C   s   | j r| jd S dS )zF
            Input version of the ``self.user`` property.
            r$   N)input_usersr2   )r3   r!   r!   r"   
input_user`  s    zChatAction.Event.input_userc                s"   | j s| j I dH r| jd S dS )zV
            Returns `input_user` but will make an API call if necessary.
            Nr$   )rM   get_input_usersr2   )r3   r!   r!   r"   get_input_userh  s    zChatAction.Event.get_input_userc             C   s   | j r| j d S dS )zQ
            Returns the marked signed ID of the first user, if any.
            r$   N)r0   )r3   r!   r!   r"   r   o  s    zChatAction.Event.user_idc                s0    j s
g S  jdkr* fdd j D  _ jS )z
            A list of users that take part in this action. For example, who joined.

            Might be empty if the information can't be retrieved or there
            are no users taking part.
            Nc                s    g | ]}| j kr j | qS r!   )r7   )r%   r   )r3   r!   r"   r'     s   z*ChatAction.Event.users.<locals>.<listcomp>)r0   r1   )r3   r!   )r3   r"   r   w  s    

zChatAction.Event.usersc                sX   | j s
g S | jdks(t| jt| j krR| jrR| jj I dH  dd | jjD | _| jS )zQ
            Returns `users` but will make an API call if necessary.
            Nc             S   s"   g | ]}t |tjtjfr|qS r!   )r   r   rE   	UserEmpty)r%   r&   r!   r!   r"   r'     s    z.ChatAction.Event.get_users.<locals>.<listcomp>)r0   r   lenr1   r(   Z_reload_messageaction_entities)r3   r!   r!   r"   rK     s    $zChatAction.Event.get_usersc             C   s   | j dkr| jrg | _ x| jD ]|}y| j jtj| j|  wW n ttfk
rX   Y nX y*| j j| jj	j
tj|d j  wW q tk
r   Y qX qW | j pg S )zG
            Input version of the ``self.users`` property.
            Nr$   )r2   r0   appendr   get_input_peerr7   KeyError	TypeErrorr9   Z_mb_entity_cacherF   Z
resolve_idZ_as_input_peerAttributeError)r3   r   r!   r!   r"   rM     s    
zChatAction.Event.input_usersc                sL   | j s
g S | jdks(t| jt| j krB| jrBdd | jjD | _| jpJg S )zW
            Returns `input_users` but will make an API call if necessary.
            Nc             S   s(   g | ] }t |tjtjfrtj|qS r!   )r   r   rE   rQ   r   rU   )r%   r&   r!   r!   r"   r'     s   z4ChatAction.Event.get_input_users.<locals>.<listcomp>)r0   rM   rR   r2   r(   rS   )r3   r!   r!   r"   rO     s    $z ChatAction.Event.get_input_usersc             C   s   | j r| j dd S dS )zL
            Returns the marked signed ID of the users, if any.
            N)r0   )r3   r!   r!   r"   user_ids  s    zChatAction.Event.user_ids)	NNNNNNNNN)__name__
__module____qualname____doc__r*   r6   r>   r@   rA   rC   rB   propertyr   rH   r   rI   rJ   rL   rN   rP   r   r   rK   rM   rO   rY   __classcell__r!   r!   )r5   r"   r   z   s0   (  5			r   )NN)rZ   r[   r\   r]   classmethodr#   r   r   r!   r!   r!   r"   r      s   Wr   N)	commonr   r   r    r   tlr   r   r!   r!   r!   r"   <module>   s   