o
    Zc                     @   s   d Z ddlmZ ddlZddlmZ z
ddlmZmZ W n e	y'   e	dw ddl
mZ dd	lmZ d
dlmZ ddgiZg dZG dd deZG dd deZdd ZeZeZdS )z
Sends updates to a Discord bot.

Usage:
>>> from tqdm.contrib.discord import tqdm, trange
>>> for i in trange(10, token='{token}', channel_id='{channel_id}'):
...     ...

![screenshot](https://img.tqdm.ml/screenshot-discord.png)
    )absolute_importN)getenv)ClientClientConfigzPlease `pip install disco-py`   )tqdm)_range   )
MonoWorkerzgithub.com/	casperdcl)	DiscordIOtqdm_discordtdranger   trangec                       s(   e Zd ZdZ fddZdd Z  ZS )r   z.Non-blocking file-like IO using a Discord Bot.c              
      s   t t|   t }||_t|}| jj| _z|j	
|| j| _W dS  ty? } ztt| d| _W Y d}~dS d}~ww )z0Creates a new message in the given `channel_id`.N)superr   __init__r   tokenr   	__class____name__textapichannels_messages_createmessage	Exception	tqdm_autowritestr)selfr   
channel_idconfigclienter    J/var/www/chikooza/env/lib/python3.10/site-packages/tqdm/contrib/discord.pyr      s   
zDiscordIO.__init__c              
   C   s   |sd}| dd }|| jkrdS | j}|du rdS || _z| |jd| d }W |S  tyG } ztt	| W Y d}~dS d}~ww )z,Replaces internal `message`'s text with `s`.z... N`)
replacestripr   r   submiteditr   r   r   r   )r   sr   futurer!   r#   r#   r$   r   ,   s    
zDiscordIO.write)r   
__module____qualname____doc__r   r   __classcell__r#   r#   r"   r$   r      s    r   c                       s8   e Zd ZdZ fddZ fddZ fddZ  ZS )r   a  
    Standard `tqdm.auto.tqdm` but also sends updates to a Discord Bot.
    May take a few seconds to create (`__init__`).

    - create a discord bot (not public, no requirement of OAuth2 code
      grant, only send message permissions) & invite it to a channel:
      <https://discordpy.readthedocs.io/en/latest/discord.html>
    - copy the bot `{token}` & `{channel_id}` and paste below

    >>> from tqdm.contrib.discord import tqdm, trange
    >>> for i in tqdm(iterable, token='{token}', channel_id='{channel_id}'):
    ...     ...
    c                    sx   | ds/| }tdtj t|dtd|dtd| _	t
d| dd|d< tt| j|i | d	S )
a  
        Parameters
        ----------
        token  : str, required. Discord token
            [default: ${TQDM_DISCORD_TOKEN}].
        channel_id  : int, required. Discord channel ID
            [default: ${TQDM_DISCORD_CHANNEL_ID}].
        mininterval  : float, optional.
          Minimum of [default: 1.5] to avoid rate limit.

        See `tqdm.auto.tqdm.__init__` for other parameters.
        disable
HTTPClientr   TQDM_DISCORD_TOKENr   TQDM_DISCORD_CHANNEL_IDg      ?minintervalN)getcopylogging	getLoggersetLevelWARNINGr   popr   diomaxr   r   r   r   argskwargsr"   r#   r$   r   M   s   
ztqdm_discord.__init__c                    sj   t t| jdi | | j}|dd r#|d dddd|d< nd|d< | j| jdi | d S )N
bar_formatz<bar/>z	{bar:10u}z{bar}z{l_bar}{bar:10u}{r_bar}r#   )	r   r   displayformat_dictr7   r(   r>   r   format_meter)r   rB   fmtr"   r#   r$   rD   c   s   ztqdm_discord.displayc                    s0   t t| j|i | | js| jd d S d S )Nr&   )r   r   clearr2   r>   r   r@   r"   r#   r$   rH   m   s   ztqdm_discord.clear)r   r.   r/   r0   r   rD   rH   r1   r#   r#   r"   r$   r   ?   s
    
r   c                  O   s   t t|  fi |S )z
    A shortcut for `tqdm.contrib.discord.tqdm(xrange(*args), **kwargs)`.
    On Python3+, `range` is used instead of `xrange`.
    )r   r   )rA   rB   r#   r#   r$   r   s   s   r   )r0   
__future__r   r9   osr   disco.clientr   r   ImportErrorautor   r   utilsr   utils_workerr
   
__author____all__r   r   r   r   r#   r#   r#   r$   <module>   s&    

"4	