
    cjh	2                    (   d Z ddlmZ ddlZddlZddlmZ ddl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 erddlmZ dZ edd      Z edd      Z G d de      Z G d d      Z G d d      Z G d de      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)deque)TYPE_CHECKINGAnyCallableDequeTypeVar)Enum   )PrivateChannel   )MaxConcurrencyReached)Message)
BucketTypeCooldownCooldownMappingDynamicCooldownMappingMaxConcurrencyCr   )boundMCr   c                  8    e Zd ZdZdZdZdZdZdZdZ	ddZ
dd	Zy
)r   r   r      r            c                   | t         j                  u r|j                  j                  S | t         j                  u r$|j                  xs |j                  j                  S | t         j
                  u r|j
                  j                  S | t         j                  u r:|j                  xr |j                  j                  |j                  j                  fS | t         j                  u r.|j
                  j                  xs |j
                  j                  S | t         j                  u rPt        |j
                  t              r|j
                  j                  S |j                  j                  j                  S y N)r   userauthoridguildchannelmembercategoryrole
isinstancer   top_roleselfmsgs     Q/var/www/html/venv/lib/python3.12/site-packages/discord/ext/commands/cooldowns.pyget_keyzBucketType.get_key>   s   :??"::== Z%%%II+///Z''';;>>!Z&&&II.#)),,>>Z(((KK((7CKK;;;Z__$
 $.ckk>#JCKKgggPSPZPZPcPcggg %    c                $    | j                  |      S r   )r-   r)   s     r,   __call__zBucketType.__call__P   s    ||C  r.   Nr+   r   returnr   )__name__
__module____qualname__defaultr   r"   r#   r$   r%   r&   r-   r0    r.   r,   r   r   5   s0    GDEGFHDh$!r.   r   c                  R    e Zd ZdZdZddZdddZdddZdddZddZ	dd	Z
dd
Zy)r   zRepresents a cooldown for a command.

    Attributes
    ----------
    rate: :class:`int`
        The total number of tokens available per :attr:`per` seconds.
    per: :class:`float`
        The length of the cooldown period in seconds.
    )rateper_window_tokens_lastc                    t        |      | _        t        |      | _        d| _        | j                  | _        d| _        y )N        )intr9   floatr:   r;   r<   r=   )r*   r9   r:   s      r,   __init__zCooldown.__init__a   s1    T	*! II
r.   Nc                    |st        j                          }| j                  }|| j                  | j                  z   kD  r| j                  }|S )a  Returns the number of available tokens before rate limiting is applied.

        Parameters
        ----------
        current: Optional[:class:`float`]
            The time in seconds since Unix epoch to calculate tokens at.
            If not supplied then :func:`time.time()` is used.

        Returns
        -------
        :class:`int`
            The number of tokens available before the cooldown is to be applied.
        )timer<   r;   r:   r9   r*   currenttokenss      r,   
get_tokenszCooldown.get_tokensh   s>     iikGT\\DHH,,YYFr.   c                    |xs t        j                          }| j                  |      }|dk(  r| j                  || j                  z
  z
  S y)a  Returns the time in seconds until the cooldown will be reset.

        Parameters
        ----------
        current: Optional[:class:`float`]
            The current time in seconds since Unix epoch.
            If not supplied, then :func:`time.time()` is used.

        Returns
        -------
        :class:`float`
            The number of seconds to wait before this cooldown will be reset.
        r   r?   )rD   rH   r:   r;   rE   s      r,   get_retry_afterzCooldown.get_retry_after   sD     (TYY[)Q;88w566r.   c                .   |xs t        j                          }|| _        | j                  |      | _        | j                  | j                  k(  r|| _        | j                  dk(  r| j                  || j
                  z
  z
  S | xj                  dz  c_        y)a  Updates the cooldown rate limit.

        Parameters
        ----------
        current: Optional[:class:`float`]
            The time in seconds since Unix epoch to update the rate limit at.
            If not supplied, then :func:`time.time()` is used.

        Returns
        -------
        Optional[:class:`float`]
            The retry-after time in seconds if rate limited.
        r   r   N)rD   r=   rH   r<   r9   r;   r:   )r*   rF   s     r,   update_rate_limitzCooldown.update_rate_limit   sz     (TYY[
w/ <<499$"DL <<188w566 	r.   c                4    | j                   | _        d| _        y)z(Reset the cooldown to its initial state.r?   N)r9   r<   r=   r*   s    r,   resetzCooldown.reset   s    yy
r.   c                B    t        | j                  | j                        S )zCreates a copy of this cooldown.

        Returns
        -------
        :class:`Cooldown`
            A new instance of this cooldown.
        )r   r9   r:   rN   s    r,   copyzCooldown.copy   s     		488,,r.   c           	     p    d| j                    d| j                   d| j                   d| j                   d	S )Nz<Cooldown rate: z per: z	 window: z	 tokens: >)r9   r:   r;   r<   rN   s    r,   __repr__zCooldown.__repr__   s;    tyykz :~Yt||nA7	
r.   )r9   rA   r:   rA   r2   Noner   )rF   float | Noner2   r@   )rF   rV   r2   rA   )rF   rV   r2   rV   r2   rU   )r2   r   r2   str)r3   r4   r5   __doc__	__slots__rB   rH   rJ   rL   rO   rQ   rT   r7   r.   r,   r   r   T   s1     ?I .,<
-
r.   r   c                      e Zd Z	 	 	 	 	 	 ddZddZedd       Zedd       Zedd       Z	ddZ
dddZdd	Zddd
Z	 d	 	 	 	 	 ddZy)r   c                Z    t        |      st        d      i | _        || _        || _        y )Nz.Cooldown type must be a BucketType or callable)callable	TypeError_cache	_cooldown_type)r*   originaltypes      r,   rB   zCooldownMapping.__init__   s,    
 ~LMM+-*2/3
r.   c                    t        | j                  | j                        }| j                  j	                         |_        |S r   )r   ra   rb   r`   rQ   r*   rets     r,   rQ   zCooldownMapping.copy   s/    dnndjj9[[%%'

r.   c                    | j                   d uS r   )ra   rN   s    r,   validzCooldownMapping.valid   s    ~~T))r.   c                    | j                   S r   rb   rN   s    r,   rd   zCooldownMapping.type   s    zzr.   c                (     | t        ||      |      S r   )r   )clsr9   r:   rd   s       r,   from_cooldownzCooldownMapping.from_cooldown   s    8D#&--r.   c                $    | j                  |      S r   rk   r)   s     r,   _bucket_keyzCooldownMapping._bucket_key   s    zz#r.   Nc                    |xs t        j                          }| j                  j                         D cg c]$  \  }}||j                  |j                  z   kD  s#|& }}}|D ]  }| j                  |=  y c c}}w r   )rD   r`   itemsr=   r:   )r*   rF   kv	dead_keyss        r,   _verify_cache_integrityz'CooldownMapping._verify_cache_integrity   sk     (TYY[#';;#4#4#6T41a'AGGaeeO:SQT	T 	AA	 Us   $A7A7c                6    | j                   j                         S r   )ra   rQ   r*   messages     r,   create_bucketzCooldownMapping.create_bucket   s    ~~""$$r.   c                   | j                   t        j                  u r| j                  S | j	                  |       | j                  |      }|| j                  vr$| j                  |      }||| j                  |<   |S | j                  |   }|S r   )rb   r   r6   ra   rv   rp   r`   rz   )r*   ry   rF   keybuckets        r,   
get_bucketzCooldownMapping.get_bucket   s    ::+++>>!$$W-w'dkk!''0F!#)C   [[%Fr.   c                H    | j                  ||      }|j                  |      S r   )r~   rL   )r*   ry   rF   r}   s       r,   rL   z!CooldownMapping.update_rate_limit  s%     '2''00r.   )rc   zCooldown | Nonerd   Callable[[Message], Any]r2   rU   )r2   r   r2   bool)r2   r   )rm   ztype[C]r2   r   r1   r   )rF   rV   r2   rU   ry   r   r2   r   )ry   r   rF   rV   r2   r   )ry   r   rF   rV   r2   rV   )r3   r4   r5   rB   rQ   propertyri   rd   classmethodrn   rp   rv   rz   r~   rL   r7   r.   r,   r   r      s    
4!
4 '
4 
	
4
 * *   . .%  9=11)51	1r.   c                  N     e Zd Z	 	 	 	 	 	 d fdZddZedd       ZddZ xZS )	r   c                4    t         |   d |       || _        y r   )superrB   _factory)r*   factoryrd   	__class__s      r,   rB   zDynamicCooldownMapping.__init__  s     	t$7>r.   c                    t        | j                  | j                        }| j                  j	                         |_        |S r   )r   r   rb   r`   rQ   rf   s     r,   rQ   zDynamicCooldownMapping.copy  s/    $T]]DJJ?[[%%'

r.   c                     y)NTr7   rN   s    r,   ri   zDynamicCooldownMapping.valid  s    r.   c                $    | j                  |      S r   )r   rx   s     r,   rz   z$DynamicCooldownMapping.create_bucket  s    }}W%%r.   )r   zCallable[[Message], Cooldown]rd   r   r2   rU   )r2   r   r   r   )	r3   r4   r5   rB   rQ   r   ri   rz   __classcell__)r   s   @r,   r   r     s>    ?4?<T?	?
  &r.   r   c                  R    e Zd ZdZdZddZddZddZddZddZ	dd	dd
Z
ddZy)
_Semaphorea  This class is a version of a semaphore.

    If you're wondering why asyncio.Semaphore isn't being used,
    it's because it doesn't expose the internal value. This internal
    value is necessary because I need to support both `wait=True` and
    `wait=False`.

    An asyncio.Queue could have been used to do this as well -- but it is
    not as efficient since internally that uses two queues and is a bit
    overkill for what is basically a counter.
    )valueloop_waitersc                b    || _         t        j                         | _        t	               | _        y r   )r   asyncioget_event_loopr   r   r   )r*   numbers     r,   rB   z_Semaphore.__init__/  s"     
/6/E/E/G	/4wr.   c                N    d| j                    dt        | j                         dS )Nz<_Semaphore value=z	 waiters=rS   )r   lenr   rN   s    r,   rT   z_Semaphore.__repr__4  s%    #DJJ<yT]]9K8LANNr.   c                     | j                   dk(  S Nr   )r   rN   s    r,   lockedz_Semaphore.locked7  s    zzQr.   c                2    t        | j                        dkD  S r   )r   r   rN   s    r,   	is_activez_Semaphore.is_active:  s    4==!A%%r.   c                    | j                   rJ| j                   j                         }|j                         s|j                  d        y | j                   rIy y r   )r   popleftdone
set_result)r*   futures     r,   wake_upz_Semaphore.wake_up=  s>    mm]]**,F;;=!!$'	 mmr.   Fwaitc                 K   |s| j                   dk  ry| j                   dk  rP| j                  j                         }| j                  j	                  |       	 | d {    | j                   dk  rP| xj                   dz  c_         y7 *#  |j                          | j                   dkD  r |j                         s| j                           xY ww)Nr   Fr   T)r   r   create_futurer   appendcancel	cancelledr   )r*   r   r   s      r,   acquirez_Semaphore.acquireD  s     

ajjAoYY,,.FMM  (	 jjAo 	

a
 ::>&*:*:*<LLNs7   ACB B
 B $C4C
B ACCc                N    | xj                   dz  c_         | j                          y )Nr   )r   r   rN   s    r,   releasez_Semaphore.releaseW  s    

a
r.   N)r   r@   r2   rU   rX   r   rW   )r   r   r2   r   )r3   r4   r5   rZ   r[   rB   rT   r   r   r   r   r   r7   r.   r,   r   r      s7    
 .I7
O& -2 &r.   r   c                  @    e Zd ZdZd	dZd
dZddZddZddZddZ	y)r   )r   r:   r   _mappingc                   i | _         || _        || _        || _        |dk  rt	        d      t        |t              st        dt        |            y )Nr   z.max_concurrency 'number' cannot be less than 1z5max_concurrency 'per' must be of type BucketType not )	r   r:   r   r   
ValueErrorr'   r   r_   rd   )r*   r   r:   r   s       r,   rB   zMaxConcurrency.__init___  s[    /1"!	Q;MNN#z*GS	}U  +r.   c                f    | j                  | j                  | j                  | j                        S )N)r:   r   )r   r   r:   r   rN   s    r,   rQ   zMaxConcurrency.copym  s#    ~~dkktxxdii~HHr.   c                V    d| j                   d| j                   d| j                   dS )Nz<MaxConcurrency per=z number=z wait=rS   )r:   r   r   rN   s    r,   rT   zMaxConcurrency.__repr__p  s,    "488,ht{{m6$))TUV	
r.   c                8    | j                   j                  |      S r   )r:   r-   rx   s     r,   r-   zMaxConcurrency.get_keyu  s    xx((r.   c                D  K   | j                  |      }	 | j                  |   }|j                  | j                         d {   }|s t        | j                  | j                        y # t        $ r' t        | j                        x| j                  |<   }Y vw xY w7 Zw)Nr   )	r-   r   KeyErrorr   r   r   r   r   r:   )r*   ry   r|   semacquireds        r,   r   zMaxConcurrency.acquirex  s     ll7#	?--$C $))44'TXX>> 	  	?'1$++'>>DMM#	? 5s3   B A+ B B'B +-BB BB c                   K   | j                  |      }	 | j                  |   }|j                          |j                  | j
                  k\  r|j                         s| j                  |= y y y # t        $ r Y y w xY wwr   )r-   r   r   r   r   r   r   )r*   ry   r|   r   s       r,   r   zMaxConcurrency.release  ss      ll7#	--$C
 KKM99#CMMOc" -<#  		s(   A<A- A	A<-	A96A<8A99A<N)r   r@   r:   r   r   r   r2   rU   )r*   r   r2   r   rX   )ry   r   r2   r   )ry   r   r2   rU   )
r3   r4   r5   r[   rB   rQ   rT   r-   r   r   r7   r.   r,   r   r   \  s&    5II

)
?#r.   )rZ   
__future__r   r   rD   collectionsr   typingr   r   r   r   r	   discord.enumsr
   abcr   errorsr   ry   r   __all__r   r   r   r   r   r   r   r   r7   r.   r,   <module>r      s   2 #    ? ?  ! )" C()T)*! !>r
 r
j@1 @1F&_ &(9 9x6# 6#r.   