U
    h.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r.|js.| jt|j|j|jdS t |tjrV|jsV| j|j	|j|jdS t |tj
r| jt|j|jpxd|jdS t |tjr| jt|jd|jdS t |tjtjfrXt |jtjrX|j}|jj}t |tj r| j|d|jdS t |tjr0|jg|jkp|j}| j|||jdS t |tjrd| j||jrXt|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$r8|j%r8| 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+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othersself_idmsgr   r    r"   >/tmp/pip-unpacked-wheel-c81u5j2r/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|g| _ng | _d | _d | _|| _|
| _|	 | _d S )N    )Z	chat_peerZmsg_idFTc                 S   s   g | ]}t |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,      sL    

 
zChatAction.Event.__init__c                    s(   t  | | jr$| j|| jd  d S )N)r+   _set_clientr*   Z_finish_init	_entities)r5   clientr7   r"   r#   r9      s    zChatAction.Event._set_clientc                    s$   | 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r5   argskwargsr"   r"   r#   respond   s    zChatAction.Event.respondc                    sH   | j s| j||I dH S | j j|d< | 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*   rB   idr<   r=   r>   r?   r"   r"   r#   reply   s    zChatAction.Event.replyc                    s4   | j s
dS | 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*   r<   Zdelete_messagesr>   r?   r"   r"   r#   delete   s    
 zChatAction.Event.deletec                    s,   | j dkr|  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r5   r"   r"   r#   get_pinned_message
  s    
z#ChatAction.Event.get_pinned_messagec                    s@   | j s| j S |  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>   r<   Zget_messagesZ_input_chatr.   )r5   Zchatr"   r"   r#   rF     s     z$ChatAction.Event.get_pinned_messagesc                 C   s8   | j r2t| j tjs2| jt| j }|r2|| _ | j S )zY
            The user who added ``users``, if applicable (`None` otherwise).
            )r/   r   r   Userr:   getr   r   )r5   Zabyr"   r"   r#   r   $  s
    zChatAction.Event.added_byc                    s(   | j s"| jr"| j| jI dH | _| jS )zT
            Returns `added_by` but will make an API call if necessary.
            N)r   r/   r<   
get_entityrG   r"   r"   r#   get_added_by0  s    zChatAction.Event.get_added_byc                 C   s8   | j r2t| j tjs2| jt| j }|r2|| _ | j S )zZ
            The user who kicked ``users``, if applicable (`None` otherwise).
            )r0   r   r   rJ   r:   rK   r   r   )r5   Zkbyr"   r"   r#   r   9  s
    zChatAction.Event.kicked_byc                    s(   | j s"| jr"| j| jI dH | _| jS )zU
            Returns `kicked_by` but will make an API call if necessary.
            N)r   r0   r<   rL   rG   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   r3   rG   r"   r"   r#   userN  s    zChatAction.Event.userc                    s"   | j s|  I dH r| jd S dS )zP
            Returns `user` but will make an API call if necessary.
            Nr%   )r   	get_usersr3   rG   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_usersr4   rG   r"   r"   r#   
input_user`  s    zChatAction.Event.input_userc                    s"   | j s|  I dH r| jd S dS )zV
            Returns `input_user` but will make an API call if necessary.
            Nr%   )rR   get_input_usersr4   rG   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r2   rG   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"   )r:   )r'   r   rG   r"   r#   r)     s   
z*ChatAction.Event.users.<locals>.<listcomp>)r2   r3   rG   r"   rG   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 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   rJ   	UserEmptyr&   r"   r"   r#   r)     s   z.ChatAction.Event.get_users.<locals>.<listcomp>)r2   r   lenr3   r*   Z_reload_messageaction_entitiesrG   r"   r"   r#   rP     s    $zChatAction.Event.get_usersc              
   C   s   | j dkr| jrg | _ | jD ]}z | j t| j|  W qW n ttfk
rX   Y nX z,| j | jj	
t|d   W qW q tk
r   Y qX q| j pg S )zG
            Input version of the ``self.users`` property.
            Nr%   )r4   r2   appendr   get_input_peerr:   KeyError	TypeErrorr<   Z_mb_entity_cacherK   Z
resolve_idZ_as_input_peerAttributeError)r5   r   r"   r"   r#   rR     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|qS r"   )r   r   rJ   rW   r   r[   r&   r"   r"   r#   r)     s   z4ChatAction.Event.get_input_users.<locals>.<listcomp>)r2   rR   rX   r4   r*   rY   rG   r"   r"   r#   rT     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.
            NrV   rG   r"   r"   r#   user_ids  s    zChatAction.Event.user_ids)	NNNNNNNNN)__name__
__module____qualname____doc__r,   r9   rB   rD   rE   rH   rF   propertyr   rM   r   rN   rO   rQ   rS   rU   r   r   rP   rR   rT   r_   __classcell__r"   r"   r7   r#   r   z   sN   )               7	
	
	





r   )NN)r`   ra   rb   rc   classmethodr$   r   r   r"   r"   r"   r#   r      s   Wr   N)	commonr   r   r    r   tlr   r   r"   r"   r"   r#   <module>   s   