
    cjhT                    T   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZm Z  dZ!erddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( dd
l)mZ* ddZ+ddZ, G d d      Z- G d d      Z. G d d      Z/y)ae  
The MIT License (MIT)

Copyright (c) 2015-2021 Rapptz
Copyright (c) 2021-present Pycord Development

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
    )annotationsN)partial)groupby)TYPE_CHECKINGAnyCallableClassVarIteratorSequence   )	ActionRowButton)	Component)
SelectMenu)_component_factory)get   )ItemItemCallbackType)View)InteractionInteractionMessage)Message)ConnectionStatec              #  p   K   | D ]+  }t        |t              r|j                  E d {    (| - y 7 wN)
isinstanceActionRowComponentchildren)
componentsitems     B/var/www/html/venv/lib/python3.12/site-packages/discord/ui/view.py_walk_all_componentsr$   7   s5      d./}}$$J	$s   %646c                    t        | t              rddlm} |j	                  |       S t        | t
              rddlm} |j	                  |       S t        j                  |       S )Nr   r   )Select)	r   ButtonComponentbuttonr   from_componentSelectComponentselectr&   r   )	componentr   r&   s      r#   _component_to_itemr-   ?   sO    )_-"$$Y//)_-"$$Y//y))    c                  8    e Zd ZdZddZd	dZd
dZd
dZddZy)_ViewWeightsweightsc                    g d| _         d }t        ||      }t        ||      D ]  \  }}|D ]  }| j                  |         y )Nr   r   r   r   r   c                R    | j                   t        j                  S | j                   S r   )rowsysmaxsize)is    r#   <lambda>z'_ViewWeights.__init__.<locals>.<lambda>Q   s    quu} !%% r.   key)r2   sortedr   add_item)selfr    r<   r6   groupr"   s         r#   __init__z_ViewWeights.__init__N   sO    "1?(,!(4 	$JC $d#$	$r.   c                ~    t        | j                        D ]  \  }}||j                  z   dk  s|c S  t        d      )N   z"could not find open space for item)	enumerater2   width
ValueError)r?   r"   indexweights       r#   find_open_spacez_ViewWeights.find_open_spaceW   sB    &t||4 	ME6

"a'	 =>>r.   c                r   |j                   r| j                  |j                      |j                  z   }|dkD  rt        d|j                    d| d      || j                  |j                   <   |j                   |_        y | j                  |      }| j                  |xx   |j                  z  cc<   ||_        y )NrC   zitem would not fit at row z (z > 5 width))r6   r2   rE   rF   _rendered_rowrI   )r?   r"   totalrG   s       r#   r>   z_ViewWeights.add_item^   s    88LL*TZZ7Eqy 0
"UG;O  &+DLL"!%D((.ELL4::-!&Dr.   c                    |j                   3| j                  |j                   xx   |j                  z  cc<   d |_         y y r   )rK   r2   rE   r?   r"   s     r#   remove_itemz_ViewWeights.remove_iteml   s9    )LL++,

:,!%D *r.   c                    g d| _         y )Nr4   r1   r?   s    r#   clearz_ViewWeights.clearq   s	    &r.   N)r    z
list[Item]r"   r   returnintr"   r   rT   NonerT   rW   )	__name__
__module____qualname__	__slots__rA   rI   r>   rO   rR    r.   r#   r0   r0   K   s     I$?'&
'r.   r0   c                     e Zd ZU dZdZded<   g Zded<   d*dZdd	d
	 	 	 	 	 d+dZd,dZ	d*dZ
d-dZedd	 	 	 	 	 d.d       Zed/d       Zd0dZd0dZd*dZd1dZd2dZd*dZd3dZ	 	 	 	 	 	 	 	 d4dZd5dZd6dZd Zd5dZd7dZd*dZd8d Zd8d!Zd8d"Z d8d#Z!d$d%d9d&Z"d$d%d9d'Z#ed(        Z$e$jJ                  d)        Z$y$):r   a  Represents a UI view.

    This object must be inherited to create a UI within Discord.

    .. versionadded:: 2.0

    Parameters
    ----------
    *items: :class:`Item`
        The initial items attached to this view.
    timeout: Optional[:class:`float`]
        Timeout in seconds from last interaction with the UI before no longer accepting input.
        If ``None`` then there is no timeout.

    Attributes
    ----------
    timeout: Optional[:class:`float`]
        Timeout from last interaction with the UI before no longer accepting input.
        If ``None`` then there is no timeout.
    children: List[:class:`Item`]
        The list of children attached to this view.
    disable_on_timeout: :class:`bool`
        Whether to disable the view when the timeout is reached. Defaults to ``False``.
    message: Optional[:class:`.Message`]
        The message that this view is attached to.
        If ``None`` then the view has not been sent with a message.
    parent: Optional[:class:`.Interaction`]
        The parent interaction which this view was sent from.
        If ``None`` then the view was not sent using :meth:`InteractionResponse.send_message`.
    TzClassVar[bool]__discord_ui_view__z ClassVar[list[ItemCallbackType]]__view_children_items__c                    g }t        | j                        D ]?  }|j                  j                         D ]   }t	        |d      s|j                  |       " A t        |      dkD  rt        d      || _        y )N__discord_ui_model_type__   z&View cannot have more than 25 children)	reversed__mro____dict__valueshasattrappendlen	TypeErrorr`   )clsr    basemembers       r#   __init_subclass__zView.__init_subclass__   st    +-S[[) 	,D--..0 ,6#>?OOF+,	,
 x=2DEE&.#r.   g     f@F)timeoutdisable_on_timeoutc               j   || _         || _        g | _        | j                  D ]i  } |j                  di |j
                  }t        || |      |_        | |_        t        | |j                  |       | j                  j                  |       k t        | j                        | _        |D ]  }| j                  |        t        j                          }t#        j$                  d      j'                         | _        d | _        d | _        d | _        |j1                         | _        d | _        d | _        y )N   r]   )rp   rq   r    r`   rb   __discord_ui_model_kwargs__r   callback_viewsetattrrY   ri   r0   _View__weightsr>   asyncioget_running_looposurandomhexid_View__cancel_callback_View__timeout_expiry_View__timeout_taskcreate_future_View__stopped_messageparent)r?   rp   rq   itemsfuncr"   loops          r#   rA   zView.__init__   s    "4$&00 	'D777 22D $D$5DMDJD$--.MM  &	' &dmm4 	 DMM$	  '')zz"~))+@D.29=/3/A/A/C=A*.r.   c                |    d| j                   j                   d| j                   dt        | j                         dS )N<z	 timeout=z
 children=>)	__class__rY   rp   rj   r    rQ   s    r#   __repr__zView.__repr__   s:    4>>**+9T\\N*SQUQ^Q^M_L``abbr.   c                  K   	 | j                   y | j                  | j                         S t        j                         }|| j                  k\  r| j                         S t        j                  | j                  |z
         d {    7 wr   )rp   r   _dispatch_timeouttime	monotonicry   sleep)r?   nows     r#   __timeout_task_implzView.__timeout_task_impl   s     ||#$$,--// .."Cd+++--// -- 5 5 ;<<<  =s   BBBBc                    dd}t        | j                  |      }g }t        ||      D ]9  \  }}|D cg c]  }|j                          }}|s&|j	                  d|d       ; |S c c}w )Nc                "    | j                   xs dS )Nr   )rK   )r"   s    r#   r<   zView.to_components.<locals>.key   s    %%**r.   r;   r   )typer!   rS   )r=   r    r   to_component_dictri   )r?   r<   r    r!   _r@   r"   s          r#   to_componentszView.to_components   s    	+ $--S1+-
c2 
	HAu=BCT..0CHC"*
	  Ds   A)rp   c                  t        |      }t        |j                        D ]  }|j                  t	        |              |S )a  Converts a message's components into a :class:`View`.

        The :attr:`.Message.components` of a message are read-only
        and separate types from those in the ``discord.ui`` namespace.
        In order to modify and edit message components they must be
        converted into a :class:`View` first.

        Parameters
        ----------
        message: :class:`.Message`
            The message with components to convert into a view.
        timeout: Optional[:class:`float`]
            The timeout of the converted view.

        Returns
        -------
        :class:`View`
            The converted view. This always returns a :class:`View` and not
            one of its subclasses.
        r   )r   r$   r!   r>   r-   )rl   messagerp   viewr,   s        r#   from_messagezView.from_message   s?    0 G$-g.@.@A 	9IMM,Y78	9r.   c                ^    | j                   r!t        j                         | j                   z   S y r   )rp   r   r   rQ   s    r#   _expires_atzView._expires_at  s"    <<>>#dll22r.   c                   t        | j                        dkD  rt        d      t        |t              st        d|j                        | j                  j                  |       | |_	        | j                  j                  |       y)a  Adds an item to the view.

        Parameters
        ----------
        item: :class:`Item`
            The item to add to the view.

        Raises
        ------
        TypeError
            An :class:`Item` was not passed.
        ValueError
            Maximum number of children has been exceeded (25)
            or the row the item is trying to be added to is full.
        rc   z#maximum number of children exceededzexpected Item not N)rj   r    rF   r   r   rk   r   rx   r>   rv   ri   rN   s     r#   r>   zView.add_item  sn    " t}}"BCC$%00BCDD%
T"r.   c                    	 | j                   j                  |       | j                  j                  |       y# t        $ r Y yw xY w)zRemoves an item from the view.

        Parameters
        ----------
        item: :class:`Item`
            The item to remove from the view.
        N)r    removerx   rO   rF   rN   s     r#   rO   zView.remove_item*  sA    	-MM  & NN&&t,  		s   9 	AAc                l    | j                   j                          | j                  j                          y)z Removes all items from the view.N)r    rR   rx   rQ   s    r#   clear_itemszView.clear_items:  s"    r.   c                0    t        | j                  |      S )ao  Get an item from the view with the given custom ID. Alias for `utils.get(view.children, custom_id=custom_id)`.

        Parameters
        ----------
        custom_id: :class:`str`
            The custom_id of the item to get

        Returns
        -------
        Optional[:class:`Item`]
            The item with the matching ``custom_id`` if it exists.
        )	custom_id)r   r    )r?   r   s     r#   get_itemzView.get_item?  s     4==I66r.   c                   K   yw)aS  |coro|

        A callback that is called when an interaction happens within the view
        that checks whether the view should process item callbacks for the interaction.

        This is useful to override if, for example, you want to ensure that the
        interaction author is a given user.

        The default implementation of this returns ``True``.

        If this returns ``False``, :meth:`on_check_failure` is called.

        .. note::

            If an exception occurs within the body then the check
            is considered a failure and :meth:`on_error` is called.

        Parameters
        ----------
        interaction: :class:`~discord.Interaction`
            The interaction that occurred.

        Returns
        -------
        :class:`bool`
            Whether the view children's callbacks should be called.
        Tr]   r?   interactions     r#   interaction_checkzView.interaction_checkN  s     8    c                "  K   | j                   r}| j                          | j                  r | j                  j                  j                  r| j
                  }n| j                  }|r%|j                  |        d{   }|r|| _        yyyy7 w)zr|coro|

        A callback that is called when a view's timeout elapses without being explicitly stopped.
        )r   N)rq   disable_all_itemsr   flags	ephemeralr   r   edit)r?   r   ms      r#   
on_timeoutzView.on_timeoutl  s|     
 """"$==DMM$7$7$A$A++,,!,,D,11$%DM   # 2s   A9B;B<Bc                   K   yw)a8  |coro|
        A callback that is called when a :meth:`View.interaction_check` returns ``False``.
        This can be used to send a response when a check failure occurs.

        Parameters
        ----------
        interaction: :class:`~discord.Interaction`
            The interaction that occurred.
        Nr]   r   s     r#   on_check_failurezView.on_check_failure~  s     r   c                   K   t        d|  d| dt        j                         t        j                  |j
                  ||j                  t        j                         yw)a  |coro|

        A callback that is called when an item's callback or :meth:`interaction_check`
        fails with an error.

        The default implementation prints the traceback to stderr.

        Parameters
        ----------
        error: :class:`Exception`
            The exception that was raised.
        item: :class:`Item`
            The item that failed the dispatch.
        interaction: :class:`~discord.Interaction`
            The interaction that led to the failure.
        zIgnoring exception in view z
 for item :)fileN)printr7   stderr	tracebackprint_exceptionr   __traceback__)r?   errorr"   r   s       r#   on_errorzView.on_error  sK     & 	+D6D6C#**U!!OOUE$7$7cjj	
s   AA!c                |  K   	 | j                   r&t        j                         | j                   z   | _        | j	                  |       d {   }|s| j                  |       d {   S |j                  |       d {    y 7 97  7 	# t        $ r&}| j                  |||       d {  7  cY d }~S d }~ww xY wwr   )	rp   r   r   r   r   r   ru   	Exceptionr   )r?   r"   r   allowes        r#   _scheduled_taskzView._scheduled_task  s     
	=||(,(84<<(G%00==E!22;???--,,,	 >?, 	=q$<<<<	=s   B<AB
 
BB
 %B&B
 )B<*B
 >B?B
 B<B
 B
 B
 
	B9B4)B,*B4.B9/B<4B99B<c                Z   t        |j                        | _        | j                  rt	        j
                         }| j                  | j                  j                          t        j                         | j                  z   | _
        |j                  | j                               | _        y y r   )r   remove_viewr   rp   ry   rz   r   cancelr   r   r   create_task_View__timeout_task_impl)r?   storer   s      r#   _start_listening_from_storez View._start_listening_from_store  s    !():):!;<<++-D"".##**,$(NN$4t||$CD!"&"2"243K3K3M"ND r.   c                    | j                   j                         ry | j                   j                  d       t        j                  | j                         d| j                          y )NTzdiscord-ui-view-timeout-name)r   done
set_resultry   r   r   r~   rQ   s    r#   r   zView._dispatch_timeout  sM    >> !!$'OO&>twwi$H	
r.   c                    | j                   j                         ry |j                  r|j                  | _        t        j                  | j                  ||      d| j                          y )Nzdiscord-ui-view-dispatch-r   )r   r   r   ry   r   r   r~   )r?   r"   r   s      r#   _dispatch_itemzView._dispatch_item  sW    >> &..DL  {3,TWWI6	
r.   c                F   | j                   D ci c]6  }|j                         s|j                  j                  |j                  f|8 }}| j                   D cg c]  }|j                         r| }}t        |      D ]J  }	 ||j                  j                  |j                  f   }|j                  |       |j                  |       L || _         y c c}w c c}w # t        t        f$ r1 t        |      }|j                         sY |j                  |       Y w xY wr   )r    is_dispatchabler   valuer   r$   refresh_componentri   KeyErrorAttributeErrorr-   )r?   r!   r"   	old_stater    r,   olders          r#   refreshzView.refresh  s    AE2
8<QUQeQeQgTYY__dnn-t32
	 2
 "]] 
$2F2F2HD 
  
 .j9 
	'I	'!9>>#7#79L9L"MN ''	2&
	' !%2
 
 n- &))4++-%	&s.   C%CC0C%C  *D D D c                    | j                   j                         s| j                   j                  d       d| _        | j                  !| j                  j                          d| _        | j                  r| j                  |        d| _        yy)zhStops listening to interaction events from this view.

        This operation cannot be undone.
        FN)r   r   r   r   r   r   r   rQ   s    r#   stopz	View.stop  sx    
 ~~""$NN%%e, $*&&("&D!!""4(%)D" "r.   c                6    | j                   j                         S )z*Whether the view has finished interacting.)r   r   rQ   s    r#   is_finishedzView.is_finished  s    ~~""$$r.   c                    | j                   duS )z9Whether the view has been added for dispatching purposes.N)r   rQ   s    r#   is_dispatchingzView.is_dispatching  s    %%T11r.   c                Z    | j                   du xr t        d | j                  D              S )zWhether the view is set up as persistent.

        A persistent view has all their components with a set ``custom_id`` and
        a :attr:`timeout` set to ``None``.
        Nc              3  <   K   | ]  }|j                           y wr   )is_persistent).0r"   s     r#   	<genexpr>z%View.is_persistent.<locals>.<genexpr>  s      ,
%)D ,
s   )rp   allr    rQ   s    r#   r   zView.is_persistent  s2     ||t# 
 ,
-1]],
 )
 	
r.   c                6   K   | j                    d{   S 7 w)a7  Waits until the view has finished interacting.

        A view is considered finished when :meth:`stop`
        is called, or it times out.

        Returns
        -------
        :class:`bool`
            If ``True``, then the view timed out. If ``False`` then
            the view finished normally.
        N)r   rQ   s    r#   waitz	View.wait  s      ^^###s   N)
exclusionsc               B    | j                   D ]  }|||vs
d|_         y)z
        Disables all items in the view.

        Parameters
        ----------
        exclusions: Optional[List[:class:`Item`]]
            A list of items in `self.children` to not disable from the view.
        NTr    disabledr?   r   childs      r#   r   zView.disable_all_items  s+     ]] 	&E!U*%<!%	&r.   c               B    | j                   D ]  }|||vs
d|_         y)z
        Enables all items in the view.

        Parameters
        ----------
        exclusions: Optional[List[:class:`Item`]]
            A list of items in `self.children` to not enable from the view.
        NFr   r   s      r#   enable_all_itemszView.enable_all_items!  s+     ]] 	'E!U*%<!&	'r.   c                    | j                   S r   r   rQ   s    r#   r   zView.message.  s    }}r.   c                    || _         y r   r   )r?   r   s     r#   r   zView.message2  s	    r.   rX   )r   r   rp   float | Nonerq   bool)rT   str)rT   zlist[dict[str, Any]])r   r   rp   r   rT   r   )rT   r   rV   )r   r   rT   zItem | None)r   r   rT   r   )r   r   rT   rW   )r   r   r"   r   r   r   rT   rW   )r"   r   r   r   )r   	ViewStorerT   rW   )r!   list[Component])rT   r   )r   zlist[Item] | NonerT   rW   )&rY   rZ   r[   __doc__r_   __annotations__r`   ro   rA   r   r   r   classmethodr   propertyr   r>   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   setterr]   r.   r#   r   r   u   s`   > +/.@B=B
/ !&#(	// / !	/>c="( =B.:	 8  
#8- 
7<&$	

&*
9D
	
0=O


!,*"%2
$ DH & CG '   ^^ r.   r   c                  ^    e Zd ZddZedd       Zd ZdddZddZddZ	ddZ
dd	Zdd
Zy)r   c                .    i | _         i | _        || _        y r   )_views_synced_message_views_state)r?   states     r#   rA   zViewStore.__init__8  s    LN68"',r.   c                    | j                   j                         D ci c]%  \  }\  }}|j                         r|j                  |' }}}t	        |j                               S c c}}w r   )r  r   r   r~   listrg   )r?   r   r   viewss       r#   persistent_viewszViewStore.persistent_views?  sb     #'++"3"3"5
IT1!!# GGTM
 

 ELLN##
s   *A$c                    g }| j                   j                         D ]*  \  }\  }}|j                         s|j                  |       , |D ]  }| j                   |=  y r   )r  r   r   ri   )r?   	to_removekr   r   s        r#   __verify_integrityzViewStore.__verify_integrityH  sa    79	 KK--/ 	$LAya!  #	$  	AA	r.   Nc                   | j                          |j                  |        |j                  D ]E  }|j                         s||f| j                  |j
                  j                  ||j                  f<   G ||| j                  |<   y y r   )	_ViewStore__verify_integrityr   r    r   r  r   r   r   r  )r?   r   
message_idr"   s       r#   add_viewzViewStore.add_viewQ  s    !((.MM 	ZD##%NRTX\TYY__j$..IJ	Z !59D&&z2 "r.   c                T   |j                   D ]O  }|j                         s| j                  j                  |j                  j
                  |j                  fd        Q | j                  j                         D ]-  \  }}|j                  |j                  k(  s | j                  |=  y  y r   )
r    r   r  popr   r   r   r  r   r~   )r?   r   r"   r<   r   s        r#   r   zViewStore.remove_view\  s    MM 	ID##%$.. A4H	I 44::< 	JCxx477"..s3	r.   c                B   | j                          |j                  xr |j                  j                  }|||f}| j                  j	                  |      xs | j                  j	                  |d |f      }|y |\  }}|j                  |       |j                  ||       y r   )r  r   r~   r  r   refresh_stater   )	r?   component_typer   r   r  r<   r   r   r"   s	            r#   dispatchzViewStore.dispatchf  s    !!,!4!4!O9L9L9O9O
z95 $ 
T9-)
 =
d;'D+.r.   c                    || j                   v S r   )r  r?   r  s     r#   is_message_trackedzViewStore.is_message_trackedv  s    T7777r.   c                :    | j                   j                  |d       S r   )r  r  r  s     r#   remove_message_trackingz!ViewStore.remove_message_trackingy  s    ))--j$??r.   c                |    | j                   |   }|j                  |D cg c]  }t        |       c}       y c c}w r   )r  r   r   )r?   r  r!   r   ds        r#   update_from_messagezViewStore.update_from_message|  s2    ))*5Z@(+@A@s   9)r  r   )rT   zSequence[View]r   )r   r   r  z
int | None)r   r   )r  rU   r   r   r   r   )r  rU   )r  rU   rT   zView | None)r  rU   r!   zlist[ComponentPayload])rY   rZ   r[   rA   r   r
  r  r  r   r  r  r  r   r]   r.   r#   r   r   7  s@    - $ $	:/ 8@Br.   r   )r!   r   rT   zIterator[Component])r,   r   rT   r   )0r   
__future__r   ry   r{   r7   r   r   	functoolsr   	itertoolsr   typingr   r   r   r	   r
   r   r!   r   r   r   r'   r   r   r*   r   utilsr   r"   r   r   __all__interactionsr   r   r   r   r  r   types.componentsComponentPayloadr$   r-   r0   r   r   r]   r.   r#   <module>r*     s   2 #  	 
     M M 8 2 " 6 +  (
 >!'@	*'' ''T DHB HBr.   