o
    eOj                     @   sx  d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ eddZG dd deZeej dkde
dedede!ddf
ddZ"eej dkde
dede!deddf
ddZ#e$ej%de	dedede!deddfddZ&eej 'd de
dedede!ddf
d!d"Z(dS )#    )RouterF)
FSMContext)StateStatesGroup)MessageCallbackQuery)AsyncSession)User)get_all_channelsadd_channelremove_channelget_channel_by_id)is_admin)build_channels_list_keyboardbuild_back_to_panel_keyboard)get_text)check_bot_is_adminadmin_channels)namec                   @   s   e Zd Ze ZdS )ChannelStateN)__name__
__module____qualname__r   waiting_channel r   r   H/var/www/www-root/data/www/ai-bot.pdev.uz/bot/handlers/admin/channels.pyr      s    
r   zadmin:channelscallbacksessiondb_userlangreturnNc                    sx   t | jj|s| jtd|ddI dH  dS t|I dH }td|}t||}| jj||dI dH  |  I dH  dS )zKanallar ro'yxatini ko'rsatishaccess-deniedT
show_alertNzchannels-listreply_markup)	r   	from_useridanswerr   r   r   message	edit_text)r   r   r   r    channelstextkeyboardr   r   r   callback_channels_list   s   

r/   zchannel:addstatec                    sj   t | jj|s| jtd|ddI dH  dS | jtd|I dH  |tjI dH  |  I dH  dS )zKanal qo'shish so'rovir"   Tr#   Nzchannel-add-prompt)	r   r'   r(   r)   r   r*   	set_stater   r   )r   r   r    r0   r   r   r   callback_channel_add+   s   r2   r*   c                    s  |  I dH  | j }z&|ds|d rt|}n|d}|d| I dH }	|	j}W n t	yH   | 
td|I dH  Y dS w t||I dH }
|
rc|
jrc| 
td|I dH  dS t||I dH }|sz| 
td|I dH  dS z/||I dH }	|	jpt|}|	j}|	j}|s|sz
||I dH }W n	 t	y   Y nw W n t	y   | 
td|I dH  Y dS w t|||||| jjdI dH  | 
td||d	I dH  dS )
z'Kanal username yoki ID ni qayta ishlashN-@channel-not-foundzchannel-already-existszchannel-bot-not-admin)r   
channel_idchannel_titlechannel_usernameinvite_linkadded_byzchannel-added)title)clearr-   strip
startswithlstripisdigitintget_chatr(   	Exceptionr)   r   r   	is_activer   r;   strusernamer9   export_chat_invite_linkr   r'   )r*   r   r   r    r0   botr-   r6   rF   chatexistingbot_is_adminr;   r9   r   r   r   process_channel_add<   s^   




	rL   zchannel:remove:c                    s   t | jj|s| jtd|ddI dH  dS t| jdd }t||I dH }|rN| td|I dH  t	|I dH }t
||}| jj|dI dH  dS | jtd	|ddI dH  dS )
zKanalni o'chirishr"   Tr#   N:   zchannel-removedr%   r5   )r   r'   r(   r)   r   rA   datasplitr   r   r   r*   edit_reply_markup)r   r   r   r    r6   removedr,   r.   r   r   r   callback_channel_remove~   s   
rS   ))aiogramr   r   aiogram.fsm.contextr   aiogram.fsm.stater   r   aiogram.typesr   r   sqlalchemy.ext.asyncior	   bot.database.modelsr
   bot.database.crud.channelr   r   r   r   bot.handlers.admin.panelr   bot.keyboards.admin_kbr   r   bot.services.i18nr   !bot.services.subscription_checkerr   routerr   callback_queryrO   rE   r/   r2   r*   r   rL   r>   rS   r   r   r   r   <module>   s~    

A