
    i%                    t   d Z ddlmZ ddlmZmZmZ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ZddlZddl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dlmZ dZ edd          Z  G d de          Z! G d de
e                    Z" G d de"e                    Z# G d d          Z$ G d d          Z%dS )a:  
The MIT License (MIT)

Copyright (c) 2015-present Rapptz

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.
    )annotations)	AnyCallableDequeDictOptionalUnionGenericTypeVarTYPE_CHECKING)Enum)PrivateChannelN)deque   )MaxConcurrencyReached)ContextCooldown)Self   )Message)
BucketTyper   CooldownMappingDynamicCooldownMappingMaxConcurrencyT_contraT)contravariantc                  :    e Zd ZdZdZdZdZdZdZdZ	ddZ
ddZdS )r   r   r      r            msgUnion[Message, Context[Any]]returnr   c                   | t           j        u r|j        j        S | t           j        u r|j        p|j        j        S | t           j        u r|j        j        S | t           j        u r|j        o|j        j        |j        j        fS | t           j        u r"t          |j        dd           p|j        j        S | t           j	        u r2t          |j        t                    r|j        n|j        j        j        S d S )Ncategory)r   userauthoridguildchannelmemberr'   getattrrole
isinstancer   top_roleselfr#   s     _/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/discord/ext/commands/cooldowns.pyget_keyzBucketType.get_key@   s    :?"":= Z%%%I+//Z''';>!Z&&&Y/39<#*-@@Z(((CKT::IckMMZ_$$
 $.ck>#J#JcCKKPSPZPcgg %$    c                ,    |                      |          S N)r5   r2   s     r4   __call__zBucketType.__call__R   s    ||C   r6   N)r#   r$   r%   r   )__name__
__module____qualname__defaultr(   r+   r,   r-   r'   r/   r5   r9    r6   r4   r   r   7   sc        GDEGFHDh h h h$! ! ! ! ! !r6   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dS ).r   originalOptional[Cooldown]typeCallable[[T_contra], Any]r%   Nonec                l    t          |          st          d          i | _        || _        || _        d S )Nz.Cooldown type must be a BucketType or callable)callable	TypeError_cache	_cooldown_type)r3   r@   rB   s      r4   __init__zCooldownMapping.__init__W   s;    
 ~~ 	NLMMM+--504


r6   CooldownMapping[T_contra]c                v    t          | j        | j                  }| j                                        |_        |S r8   )r   rI   rJ   rH   copyr3   rets     r4   rN   zCooldownMapping.copyc   s/    dndj99[%%''

r6   boolc                    | j         d uS r8   )rI   r3   s    r4   validzCooldownMapping.validh   s    ~T))r6   c                    | j         S r8   rJ   rS   s    r4   rB   zCooldownMapping.typel   s
    zr6   ratefloatperr   c                6     | t          ||          |          S r8   r   )clsrW   rY   rB   s       r4   from_cooldownzCooldownMapping.from_cooldownp   s    s8D#&&---r6   r#   r   r   c                ,    |                      |          S r8   rV   r2   s     r4   _bucket_keyzCooldownMapping._bucket_keyt   s    zz#r6   NcurrentOptional[float]c                    pt          j                     fd| j                                        D             }|D ]
}| j        |= d S )Nc                @    g | ]\  }}|j         |j        z   k    |S r>   )_lastrY   ).0kvr_   s      r4   
<listcomp>z;CooldownMapping._verify_cache_integrity.<locals>.<listcomp>|   s/    TTT41a'AGaeO:S:SQ:S:S:Sr6   )timerH   items)r3   r_   	dead_keysre   s    `  r4   _verify_cache_integrityz'CooldownMapping._verify_cache_integrityw   s`     (TY[[TTTT4;#4#4#6#6TTT	 	 	AA	 	r6   messager   c                4    | j                                         S r8   )rI   rN   r3   rl   s     r4   create_bucketzCooldownMapping.create_bucket   s    ~""$$$r6   c                    | j         t          j        u r| j        S |                     |           |                     |          }|| j        vr"|                     |          }|
|| j        |<   n| j        |         }|S r8   )rJ   r   r=   rI   rk   r^   rH   ro   )r3   rl   r_   keybuckets        r4   
get_bucketzCooldownMapping.get_bucket   s    :+++>!$$W---w''dk!!''00F!#)C [%Fr6   r   tokensintc                d    |                      ||          }|d S |                    ||          S )N)rt   )rs   update_rate_limit)r3   rl   r_   rt   rr   s        r4   rw   z!CooldownMapping.update_rate_limit   s8    '22>4'''???r6   )r@   rA   rB   rC   r%   rD   )r%   rL   r%   rQ   )r%   rC   )rW   rX   rY   rX   rB   rC   r%   r   )r#   r   r%   r   r8   )r_   r`   r%   rD   )rl   r   r%   r   )rl   r   r_   r`   r%   rA   Nr   )rl   r   r_   r`   rt   ru   r%   r`   )r:   r;   r<   rK   rN   propertyrT   rB   classmethodr\   r^   rk   ro   rs   rw   r>   r6   r4   r   r   V   s       
5 
5 
5 
5   
 * * * X*    X . . . [.       % % % %    @ @ @ @ @ @ @r6   r   c                  H     e Zd Zd fdZdd	Zedd            ZddZ xZS )r   factory(Callable[[T_contra], Optional[Cooldown]]rB   rC   r%   rD   c                Z    t                                          d |           || _        d S r8   )superrK   _factory)r3   r}   rB   	__class__s      r4   rK   zDynamicCooldownMapping.__init__   s*    
 	t$$$BIr6    DynamicCooldownMapping[T_contra]c                v    t          | j        | j                  }| j                                        |_        |S r8   )r   r   rJ   rH   rN   rO   s     r4   rN   zDynamicCooldownMapping.copy   s/    $T]DJ??[%%''

r6   rQ   c                    dS )NTr>   rS   s    r4   rT   zDynamicCooldownMapping.valid   s    tr6   rl   r   rA   c                ,    |                      |          S r8   )r   rn   s     r4   ro   z$DynamicCooldownMapping.create_bucket   s    }}W%%%r6   )r}   r~   rB   rC   r%   rD   )r%   r   rx   )rl   r   r%   rA   )	r:   r;   r<   rK   rN   rz   rT   ro   __classcell__)r   s   @r4   r   r      s        J J J J J J   
    X& & & & & & & &r6   r   c                  T    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dS )
_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 inefficient since internally that uses two queues and is a bit
    overkill for what is basically a counter.
    )valueloop_waitersnumberru   r%   rD   c                j    || _         t          j                    | _        t	                      | _        d S r8   )r   asyncioget_running_loopr   r   r   )r3   r   s     r4   rK   z_Semaphore.__init__   s(     
/6/G/I/I	/4wwr6   strc                B    d| j          dt          | j                   dS )Nz<_Semaphore value=z	 waiters=>)r   lenr   rS   s    r4   __repr__z_Semaphore.__repr__   s'    NDJNNT]9K9KNNNNr6   rQ   c                    | j         dk    S Nr   )r   rS   s    r4   lockedz_Semaphore.locked   s    zQr6   c                2    t          | j                  dk    S r   )r   r   rS   s    r4   	is_activez_Semaphore.is_active   s    4=!!A%%r6   c                    | j         rM| j                                         }|                                s|                    d            d S | j         Kd S d S r8   )r   popleftdone
set_result)r3   futures     r4   wake_upz_Semaphore.wake_up   sh    m 	]**,,F;;== !!$'''	 m 	 	 	 	 	r6   Fwaitr   c                 K   |s| j         dk    rdS | j         dk    r| j                                        }| j                            |           	 | d {V  nM#  |                                 | j         dk    r(|                                s|                                   xY w| j         dk    | xj         dz  c_         dS )Nr   Fr   T)r   r   create_futurer   appendcancel	cancelledr   )r3   r   r   s      r4   acquirez_Semaphore.acquire   s       	
a5jAooY,,..FM  (((:>>&*:*:*<*<>LLNNN jAoo 	

a

ts   A A
B$c                N    | xj         dz  c_         |                                  d S ry   )r   r   rS   s    r4   releasez_Semaphore.release   s"    

a

r6   N)r   ru   r%   rD   r%   r   rx   )r%   rD   )r   rQ   r%   rQ   )r:   r;   r<   __doc__	__slots__rK   r   r   r   r   r   r   r>   r6   r4   r   r      s        
 
 .I7 7 7 7
O O O O   & & & &    -2      &     r6   r   c                  B    e Zd ZdZdd
ZddZddZddZddZddZ	dS )r   )r   rY   r   _mappingr   ru   rY   r   r   rQ   r%   rD   c                   i | _         || _        || _        || _        |dk    rt	          d          t          |t                    st          dt          |                    d S )Nr   z.max_concurrency 'number' cannot be less than 1z5max_concurrency 'per' must be of type BucketType not )	r   rY   r   r   
ValueErrorr0   r   rG   rB   )r3   r   rY   r   s       r4   rK   zMaxConcurrency.__init__   sv    /1"!	Q;;MNNN#z** 	caTXY\T]T]aabbb	c 	cr6   r   c                P    |                      | j        | j        | j                  S )N)rY   r   )r   r   rY   r   rS   s    r4   rN   zMaxConcurrency.copy   s     ~~dktxdi~HHHr6   r   c                8    d| j         d| j         d| j         dS )Nz<MaxConcurrency per=z number=z wait=r   )rY   r   r   rS   s    r4   r   zMaxConcurrency.__repr__   s)    YdhYY$+YYTYYYYYr6   rl   r$   r   c                6    | j                             |          S r8   )rY   r5   rn   s     r4   r5   zMaxConcurrency.get_key   s    x(((r6   c                (  K   |                      |          }	 | j        |         }n.# t          $ r! t          | j                  x| j        |<   }Y nw xY w|                    | j                   d {V }|st          | j        | j                  d S )Nr   )	r5   r   KeyErrorr   r   r   r   r   rY   )r3   rl   rq   semacquireds        r4   r   zMaxConcurrency.acquire  s      ll7##	?-$CC 	? 	? 	?'1$+'>'>>DM#	? $)44444444 	?'TX>>>	? 	?s   ' (AAc                   K   |                      |          }	 | j        |         }|                                 n# t          $ r Y d S w xY w|j        | j        k    r|                                s| j        |= d S d S d S r8   )r5   r   r   r   r   r   r   )r3   rl   rq   r   s       r4   r   zMaxConcurrency.release  s       ll7##	-$C
 KKMMMM	  	 	 	FF	 9##CMMOO#c""" $###s   ; 
A	A	N)r   ru   rY   r   r   rQ   r%   rD   )r%   r   r   )rl   r$   r%   r   )rl   r$   r%   rD   )
r:   r;   r<   r   rK   rN   r   r5   r   r   r>   r6   r4   r   r      s        5I
c 
c 
c 
cI I I IZ Z Z Z) ) ) )
? 
? 
? 
?# # # # # #r6   r   )&r   
__future__r   typingr   r   r   r   r   r	   r
   r   r   discord.enumsr   discord.abcr   rh   r   collectionsr   errorsr   contextr   discord.app_commandsr   typing_extensionsr   rl   r   __all__r   r   r   r   r   r   r>   r6   r4   <module>r      s#   0 # " " " " " ` _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _       & & & & & &         ) ) ) ) ) )       5 5 5 5 5 5 #&&&&&&"""""" 7:T222! ! ! ! ! ! ! !>@@ @@ @@ @@ @@gh' @@ @@ @@F& & & & &_X6 & & &,9 9 9 9 9 9 9 9x2# 2# 2# 2# 2# 2# 2# 2# 2# 2#r6   