import os
import logging
import random
import asyncio
from Script import script
from pyrogram import Client, filters, enums
from pyrogram.errors import ChatAdminRequired, FloodWait
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from database.ia_filterdb import Media, get_file_details, unpack_new_file_id, get_bad_files
from database.users_chats_db import db
from info import CHANNELS, ADMINS, AUTH_CHANNEL, LOG_CHANNEL, PICS, BATCH_FILE_CAPTION, CUSTOM_FILE_CAPTION, SUPPORT_CHAT, PROTECT_CONTENT, REQST_CHANNEL, SUPPORT_CHAT_ID, MAX_B_TN
from utils import get_settings, get_size, is_subscribed, save_group_settings, temp
from database.connections_mdb import active_connection
import re
import json
import base64
logger = logging.getLogger(__name__)

BATCH_FILES = {}

@Client.on_message(filters.command("start") & filters.incoming)
async def start(client, message):
    if message.chat.type in [enums.ChatType.GROUP, enums.ChatType.SUPERGROUP]:
        buttons = [[
                    InlineKeyboardButton('â�• ğ�– ğ�–½ğ�–½ ğ�–¬ğ�–¾ ğ�–³ğ�—ˆ ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰ â�•', url=f"http://t.me/{temp.U_NAME}?startgroup=true")
                ],[
                    InlineKeyboardButton('ğŸ›¡ ğ�–®ğ�—�ğ�—‡ğ�–¾ğ�—‹', callback_data="owner_info"),
                    InlineKeyboardButton('ğŸ§© ğ�–²ğ�—�ğ�—‰ğ�—‰ğ�—ˆğ�—‹ğ�—� ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰', url=f"https://t.me/{SUPPORT_CHAT}")
                ],[
                    InlineKeyboardButton('â„¹ï¸� ğ�–§ğ�–¾ğ�—…ğ�—‰', callback_data='help'),
                    InlineKeyboardButton('ğŸ˜Š ğ�– ğ�–»ğ�—ˆğ�—�ğ�—�', callback_data='about')
                ],[
                    InlineKeyboardButton('ğŸ”� ğ�–¨ğ�—‡ğ�—…ğ�—‚ğ�—‡ğ�–¾ ğ�–²ğ�–¾ğ�–ºğ�—‹ğ�–¼ğ�—�', switch_inline_query_current_chat='')
                  ]]
        reply_markup = InlineKeyboardMarkup(buttons)
        await message.reply(script.START_TXT.format(message.from_user.mention if message.from_user else message.chat.title, temp.U_NAME, temp.B_NAME), reply_markup=reply_markup)
        await asyncio.sleep(2)
        if not await db.get_chat(message.chat.id):
            total=await client.get_chat_members_count(message.chat.id)
            await client.send_message(LOG_CHANNEL, script.LOG_TEXT_G.format(message.chat.title, message.chat.id, total, "Unknown"))       
            await db.add_chat(message.chat.id, message.chat.title)
        return 
    if not await db.is_user_exist(message.from_user.id):
        await db.add_user(message.from_user.id, message.from_user.first_name)
        await client.send_message(LOG_CHANNEL, script.LOG_TEXT_P.format(message.from_user.id, message.from_user.mention))
    if len(message.command) != 2:
        buttons = [[
                    InlineKeyboardButton('â�• ğ�– ğ�–½ğ�–½ ğ�–¬ğ�–¾ ğ�–³ğ�—ˆ ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰ â�•', url=f"http://t.me/{temp.U_NAME}?startgroup=true")
                ],[
                    InlineKeyboardButton('ğŸ›¡ ğ�–®ğ�—�ğ�—‡ğ�–¾ğ�—‹', callback_data="owner_info"),
                    InlineKeyboardButton('ğŸ§© ğ�–²ğ�—�ğ�—‰ğ�—‰ğ�—ˆğ�—‹ğ�—� ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰', url=f"https://t.me/{SUPPORT_CHAT}")
                ],[
                    InlineKeyboardButton('â„¹ï¸� ğ�–§ğ�–¾ğ�—…ğ�—‰', callback_data='help'),
                    InlineKeyboardButton('ğŸ˜Š ğ�– ğ�–»ğ�—ˆğ�—�ğ�—�', callback_data='about'),
                ],[
                    InlineKeyboardButton('ğŸ”� ğ�–¨ğ�—‡ğ�—…ğ�—‚ğ�—‡ğ�–¾ ğ�–²ğ�–¾ğ�–ºğ�—‹ğ�–¼ğ�—�', switch_inline_query_current_chat='')
                  ]]
        reply_markup = InlineKeyboardMarkup(buttons)
        await message.reply_photo(
            photo=random.choice(PICS),
            caption=script.START_TXT.format(message.from_user.mention, temp.U_NAME, temp.B_NAME),
            reply_markup=reply_markup,
            parse_mode=enums.ParseMode.HTML
        )
        return
    if AUTH_CHANNEL and not await is_subscribed(client, message):
        try:
            invite_link = await client.create_chat_invite_link(int(AUTH_CHANNEL))
        except ChatAdminRequired:
            logger.error("Make sure Bot is admin in Forcesub channel")
            return
        btn = [
            [
                InlineKeyboardButton(
                    "ğŸ¤– ğ�–©ğ�—ˆğ�—‚ğ�—‡ ğ�–´ğ�—‰ğ�–½ğ�–ºğ�—�ğ�–¾ğ�—Œ ğ�–¢ğ�—�ğ�–ºğ�—‡ğ�—‡ğ�–¾ğ�—… ğŸ¤–", url=invite_link.invite_link
                )
            ]
        ]

        if message.command[1] != "subscribe" or message.command[1] != "send_all":
            try:
                kk, file_id = message.command[1].split("_", 1)
                pre = 'checksubp' if kk == 'filep' else 'checksub' 
                btn.append([InlineKeyboardButton("ğŸ”„ ğ�–³ğ�—‹ğ�—’ ğ�– ğ�—€ğ�–ºğ�—‚ğ�—‡ ğŸ”„", callback_data=f"{pre}#{file_id}")])
            except (IndexError, ValueError):
                btn.append([InlineKeyboardButton("ğŸ”„ ğ�–³ğ�—‹ğ�—’ ğ�– ğ�—€ğ�–ºğ�—‚ğ�—‡ ğŸ”„", url=f"https://t.me/{temp.U_NAME}?start={message.command[1]}")])
        await client.send_message(
            chat_id=message.from_user.id,
            text="**Please Join My Updates Channel to use this Bot!**",
            reply_markup=InlineKeyboardMarkup(btn),
            parse_mode=enums.ParseMode.MARKDOWN
            )
        return
    if len(message.command) == 2 and message.command[1] in ["subscribe", "error", "okay", "help"]:
        buttons = [[
                    InlineKeyboardButton('â�• ğ�– ğ�–½ğ�–½ ğ�–¬ğ�–¾ ğ�–³ğ�—ˆ ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰ â�•', url=f"http://t.me/{temp.U_NAME}?startgroup=true")
                ],[
                    InlineKeyboardButton('ğŸ›¡ ğ�–®ğ�—�ğ�—‡ğ�–¾ğ�—‹', callback_data="owner_info"),
                    InlineKeyboardButton('ğŸ§© ğ�–²ğ�—�ğ�—‰ğ�—‰ğ�—ˆğ�—‹ğ�—� ğ�–¦ğ�—‹ğ�—ˆğ�—�ğ�—‰', url=f"https://t.me/{SUPPORT_CHAT}")
                ],[
                    InlineKeyboardButton('â„¹ï¸� ğ�–§ğ�–¾ğ�—…ğ�—‰', callback_data='help'),
                    InlineKeyboardButton('ğŸ˜Š ğ�– ğ�–»ğ�—ˆğ�—�ğ�—�', callback_data='about')
                ],[
                    InlineKeyboardButton('ğŸ”� ğ�–¨ğ�—‡ğ�—…ğ�—‚ğ�—‡ğ�–¾ ğ�–²ğ�–¾ğ�–ºğ�—‹ğ�–¼ğ�—�', switch_inline_query_current_chat='')
                    
                  ]]
        reply_markup = InlineKeyboardMarkup(buttons)
        await message.reply_photo(
            photo=random.choice(PICS),
            caption=script.START_TXT.format(message.from_user.mention, temp.U_NAME, temp.B_NAME),
            reply_markup=reply_markup,
            parse_mode=enums.ParseMode.HTML
        )
        return
    data = message.command[1]
    try:
        pre, file_id = data.split('_', 1)
    except:
        file_id = data
        pre = ""
        
    if data.startswith("all"):
        _, key, pre = data.split("_", 2)
        files = temp.FILES_IDS.get(key)
        if not files:
            return await message.reply('<b><i>No such file exist.</b></i>')
        
        for file in files:
            title = file.file_name
            size=get_size(file.file_size)
            f_caption=file.caption
            if CUSTOM_FILE_CAPTION:
                try:
                    f_caption=CUSTOM_FILE_CAPTION.format(file_name= '' if title is None else title, file_size='' if size is None else size, file_caption='' if f_caption is None else f_caption)
                except:
                    f_caption=f_caption
            if f_caption is None:
                f_caption = f"{file.file_name}"
            await client.send_cached_media(
                chat_id=message.from_user.id,
                file_id=file.file_id,
                caption=f_caption,
                protect_content=True if pre == 'filep' else False,
                reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton('âš”ï¸� ğ�–¯ğ�–¨ğ�–±ğ�–® ğ�–´ğ�–¯ğ�–£ğ�– ğ�–³ğ�–¤ğ�–² âš”ï¸�', url=f"https://t.me/piroxbots") ] ] ),
            )
        return
    
    if data.split("-", 1)[0] == "BATCH":
        sts = await message.reply("<b>Please wait...</b>")
        file_id = data.split("-", 1)[1]
        msgs = BATCH_FILES.get(file_id)
        if not msgs:
            file = await client.download_media(file_id)
            try: 
                with open(file) as file_data:
                    msgs=json.loads(file_data.read())
            except:
                await sts.edit("FAILED")
                return await client.send_message(LOG_CHANNEL, "UNABLE TO OPEN FILE.")
            os.remove(file)
            BATCH_FILES[file_id] = msgs
        for msg in msgs:
            title = msg.get("title")
            size=get_size(int(msg.get("size", 0)))
            f_caption=msg.get("caption", "")
            if BATCH_FILE_CAPTION:
                try:
                    f_caption=BATCH_FILE_CAPTION.format(file_name= '' if title is None else title, file_size='' if size is None else size, file_caption='' if f_caption is None else f_caption)
                except Exception as e:
                    logger.exception(e)
                    f_caption=f_caption
            if f_caption is None:
                f_caption = f"{title}"
            try:
                await client.send_cached_media(
                    chat_id=message.from_user.id,
                    file_id=msg.get("file_id"),
                    caption=f_caption,
                    protect_content=msg.get('protect', False),
                    reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton('âš”ï¸� ğ�–¯ğ�–¨ğ�–±ğ�–® ğ�–´ğ�–¯ğ�–£ğ�– ğ�–³ğ�–¤ğ�–² âš”ï¸�', url=f"https://t.me/piroxbots") ] ] ),
                    
                )
            except FloodWait as e:
                await asyncio.sleep(e.x)
                logger.warning(f"Floodwait of {e.x} sec.")
                await client.send_cached_media(
                    chat_id=message.from_user.id,
                    file_id=msg.get("file_id"),
                    caption=f_caption,
                    protect_content=msg.get('protect', False),
                    reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton('âš”ï¸� ğ�–¯ğ�–¨ğ�–±ğ�–® ğ�–´ğ�–¯ğ�–£ğ�– ğ�–³ğ�–¤ğ�–² âš”ï¸�', url=f"https://t.me/piroxbots") ] ] ),
                )
            except Exception as e:
                logger.warning(e, exc_info=True)
                continue
            await asyncio.sleep(1) 
        await sts.delete()
        return
    elif data.split("-", 1)[0] == "DSTORE":
        sts = await message.reply("<b>Please wait...</b>")
        b_string = data.split("-", 1)[1]
        decoded = (base64.urlsafe_b64decode(b_string + "=" * (-len(b_string) % 4))).decode("ascii")
        try:
            f_msg_id, l_msg_id, f_chat_id, protect = decoded.split("_", 3)
        except:
            f_msg_id, l_msg_id, f_chat_id = decoded.split("_", 2)
            protect = "/pbatch" if PROTECT_CONTENT else "batch"
        diff = int(l_msg_id) - int(f_msg_id)
        async for msg in client.iter_messages(int(f_chat_id), int(l_msg_id), int(f_msg_id)):
            if msg.media:
                media = getattr(msg, msg.media.value)
                if BATCH_FILE_CAPTION:
                    try:
                        f_caption=BATCH_FILE_CAPTION.format(file_name=getattr(media, 'file_name', ''), file_size=getattr(media, 'file_size', ''), file_caption=getattr(msg, 'caption', ''))
                    except Exception as e:
                        logger.exception(e)
                        f_caption = getattr(msg, 'caption', '')
                else:
                    media = getattr(msg, msg.media.value)
                    file_name = getattr(media, 'file_name', '')
                    f_caption = getattr(msg, 'caption', file_name)
                try:
                    await msg.copy(message.chat.id, caption=f_caption, protect_content=True if protect == "/pbatch" else False)
                except FloodWait as e:
                    await asyncio.sleep(e.x)
                    await msg.copy(message.chat.id, caption=f_caption, protect_content=True if protect == "/pbatch" else False)
                except Exception as e:
                    logger.exception(e)
                    continue
            elif msg.empty:
                continue
            else:
                try:
                    await msg.copy(message.chat.id, protect_content=True if protect == "/pbatch" else False)
                except FloodWait as e:
                    await asyncio.sleep(e.x)
                    await msg.copy(message.chat.id, protect_content=True if protect == "/pbatch" else False)
                except Exception as e:
                    logger.exception(e)
                    continue
            await asyncio.sleep(1) 
        return await sts.delete()
        

    files_ = await get_file_details(file_id)           
    if not files_:
        pre, file_id = ((base64.urlsafe_b64decode(data + "=" * (-len(data) % 4))).decode("ascii")).split("_", 1)
        try:
            msg = await client.send_cached_media(
                chat_id=message.from_user.id,
                file_id=file_id,
                protect_content=True if pre == 'filep' else False,
                reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton('âš”ï¸� ğ�–¯ğ�–¨ğ�–±ğ�–® ğ�–´ğ�–¯ğ�–£ğ�– ğ�–³ğ�–¤ğ�–² âš”ï¸�', url="https://t.me/piroxbots") ] ] ),
            )
            filetype = msg.media
            file = getattr(msg, filetype.value)
            title = file.file_name
            size=get_size(file.file_size)
            f_caption = f"<code>{title}</code>"
            if CUSTOM_FILE_CAPTION:
                try:
                    f_caption=CUSTOM_FILE_CAPTION.format(file_name= '' if title is None else title, file_size='' if size is None else size, file_caption='')
                except:
                    return
            await msg.edit_caption(f_caption)
            return
        except:
            pass
        return await message.reply('<b><i>No such file exist.</b></i>')
    files = files_[0]
    title = files.file_name
    size=get_size(files.file_size)
    f_caption=files.caption
    if CUSTOM_FILE_CAPTION:
        try:
            f_caption=CUSTOM_FILE_CAPTION.format(file_name= '' if title is None else title, file_size='' if size is None else size, file_caption='' if f_caption is None else f_caption)
        except Exception as e:
            logger.exception(e)
            f_caption=f_caption
    if f_caption is None:
        f_caption = f"{files.file_name}"
    await client.send_cached_media(
        chat_id=message.from_user.id,
        file_id=file_id,
        caption=f_caption,
        protect_content=True if pre == 'filep' else False,
        reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton('âš”ï¸� ğ�–¯ğ�–¨ğ�–±ğ�–® ğ�–´ğ�–¯ğ�–£ğ�– ğ�–³ğ�–¤ğ�–² âš”ï¸�', url="https://t.me/piroxbots") ] ] ),
    )
                    

@Client.on_message(filters.command('channel') & filters.user(ADMINS))
async def channel_info(bot, message):
           
    """Send basic information of channel"""
    if isinstance(CHANNELS, (int, str)):
        channels = [CHANNELS]
    elif isinstance(CHANNELS, list):
        channels = CHANNELS
    else:
        raise ValueError("Unexpected type of CHANNELS")

    text = 'ğŸ“‘ **Indexed channels/groups**\n'
    for channel in channels:
        chat = await bot.get_chat(channel)
        if chat.username:
            text += '\n@' + chat.username
        else:
            text += '\n' + chat.title or chat.first_name

    text += f'\n\n**Total:** {len(CHANNELS)}'

    if len(text) < 4096:
        await message.reply(text)
    else:
        file = 'Indexed channels.txt'
        with open(file, 'w') as f:
            f.write(text)
        await message.reply_document(file)
        os.remove(file)


@Client.on_message(filters.command('logs') & filters.user(ADMINS))
async def log_file(bot, message):
    """Send log file"""
    try:
        await message.reply_document('Logs.txt')
    except Exception as e:
        await message.reply(str(e))

@Client.on_message(filters.command('delete') & filters.user(ADMINS))
async def delete(bot, message):
    """Delete file from database"""
    reply = message.reply_to_message
    if reply and reply.media:
        msg = await message.reply("Processing...â�³", quote=True)
    else:
        await message.reply('Reply to file with /delete which you want to delete', quote=True)
        return

    for file_type in ("document", "video", "audio"):
        media = getattr(reply, file_type, None)
        if media is not None:
            break
    else:
        await msg.edit('This is not supported file format')
        return
    
    file_id, file_ref = unpack_new_file_id(media.file_id)

    result = await Media.collection.delete_one({
        '_id': file_id,
    })
    if result.deleted_count:
        await msg.edit('File is successfully deleted from database')
    else:
        file_name = re.sub(r"(_|\-|\.|\+)", " ", str(media.file_name))
        result = await Media.collection.delete_many({
            'file_name': file_name,
            'file_size': media.file_size,
            'mime_type': media.mime_type
            })
        if result.deleted_count:
            await msg.edit('File is successfully deleted from database')
        else:
            result = await Media.collection.delete_many({
                'file_name': media.file_name,
                'file_size': media.file_size,
                'mime_type': media.mime_type
            })
            if result.deleted_count:
                await msg.edit('File is successfully deleted from database')
            else:
                await msg.edit('File not found in database')


@Client.on_message(filters.command('deleteall') & filters.user(ADMINS))
async def delete_all_index(bot, message):
    await message.reply_text(
        'This will delete all indexed files.\nDo you want to continue??',
        reply_markup=InlineKeyboardMarkup(
            [
                [
                    InlineKeyboardButton(
                        text="YES", callback_data="autofilter_delete"
                    )
                ],
                [
                    InlineKeyboardButton(
                        text="CANCEL", callback_data="close_data"
                    )
                ],
            ]
        ),
        quote=True,
    )


@Client.on_callback_query(filters.regex(r'^autofilter_delete'))
async def delete_all_index_confirm(bot, message):
    await Media.collection.drop()
    await message.answer('Piracy Is Crime')
    await message.message.edit('Succesfully Deleted All The Indexed Files.')


@Client.on_message(filters.command('settings'))
async def settings(client, message):
    userid = message.from_user.id if message.from_user else None
    if not userid:
        return await message.reply(f"You are anonymous admin. Use /connect {message.chat.id} in PM")
    chat_type = message.chat.type

    if chat_type == enums.ChatType.PRIVATE:
        grpid = await active_connection(str(userid))
        if grpid is not None:
            grp_id = grpid
            try:
                chat = await client.get_chat(grpid)
                title = chat.title
            except:
                await message.reply_text("Make sure I'm present in your group!!", quote=True)
                return
        else:
            await message.reply_text("I'm not connected to any groups!", quote=True)
            return

    elif chat_type in [enums.ChatType.GROUP, enums.ChatType.SUPERGROUP]:
        grp_id = message.chat.id
        title = message.chat.title

    else:
        return

    st = await client.get_chat_member(grp_id, userid)
    if (
            st.status != enums.ChatMemberStatus.ADMINISTRATOR
            and st.status != enums.ChatMemberStatus.OWNER
            and str(userid) not in ADMINS
    ):
        return
    
    settings = await get_settings(grp_id)

    try:
        if settings['max_btn']:
            settings = await get_settings(grp_id)
    except KeyError:
        await save_group_settings(grp_id, 'max_btn', False)
        settings = await get_settings(grp_id)

    if settings is not None:
        buttons = [
            [
                InlineKeyboardButton(
                    'ğ�–¥ğ�—‚ğ�—…ğ�—�ğ�–¾ğ�—‹ ğ�–¡ğ�—�ğ�—�ğ�—�ğ�—ˆğ�—‡',
                    callback_data=f'setgs#button#{settings["button"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'ğ�–²ğ�—‚ğ�—‡ğ�—€ğ�—…ğ�–¾ ğ�–¡ğ�—�ğ�—�ğ�—�ğ�—ˆğ�—‡' if settings["button"] else 'ğ�–£ğ�—ˆğ�—�ğ�–»ğ�—…ğ�–¾',
                    callback_data=f'setgs#button#{settings["button"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–¥ğ�—‚ğ�—…ğ�–¾ ğ�–²ğ�–¾ğ�—‡ğ�–½ ğ�–¬ğ�—ˆğ�–½ğ�–¾',
                    callback_data=f'setgs#botpm#{settings["botpm"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'ğ�–¬ğ�–ºğ�—‡ğ�—�ğ�–ºğ�—… ğ�–²ğ�—�ğ�–ºğ�—‹ğ�—�' if settings["botpm"] else 'ğ�– ğ�—�ğ�—�ğ�—ˆ ğ�–²ğ�–¾ğ�—‡ğ�–½',
                    callback_data=f'setgs#botpm#{settings["botpm"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–¯ğ�—‹ğ�—ˆğ�—�ğ�–¾ğ�–¼ğ�—� ğ�–¢ğ�—ˆğ�—‡ğ�—�ğ�–¾ğ�—‡ğ�—�',
                    callback_data=f'setgs#file_secure#{settings["file_secure"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'âœ… ğ�–®ğ�—‡' if settings["file_secure"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#file_secure#{settings["file_secure"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–¨ğ�–¬ğ�–£ğ�–»',
                    callback_data=f'setgs#imdb#{settings["imdb"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'âœ… ğ�–®ğ�—‡' if settings["imdb"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#imdb#{settings["imdb"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–²ğ�—‰ğ�–¾ğ�—…ğ�—… ğ�–¢ğ�—�ğ�–¾ğ�–¼ğ�—„',
                    callback_data=f'setgs#spell_check#{settings["spell_check"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'âœ… ğ�–®ğ�—‡' if settings["spell_check"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#spell_check#{settings["spell_check"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–¶ğ�–¾ğ�—…ğ�–¼ğ�—ˆğ�—†ğ�–¾ ğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾',
                    callback_data=f'setgs#welcome#{settings["welcome"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'âœ… ğ�–®ğ�—‡' if settings["welcome"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#welcome#{settings["welcome"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�– ğ�—�ğ�—�ğ�—ˆ ğ�–£ğ�–¾ğ�—…ğ�–¾ğ�—�ğ�–¾',
                    callback_data=f'setgs#auto_delete#{settings["auto_delete"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    '5 ğ�–¬ğ�—‚ğ�—‡' if settings["auto_delete"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#auto_delete#{settings["auto_delete"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�– ğ�—�ğ�—�ğ�—ˆ-ğ�–¥ğ�—‚ğ�—…ğ�—�ğ�–¾ğ�—‹',
                    callback_data=f'setgs#auto_ffilter#{settings["auto_ffilter"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    'âœ… ğ�–®ğ�—‡' if settings["auto_ffilter"] else 'â�Œ ğ�–®ğ�–¿ğ�–¿',
                    callback_data=f'setgs#auto_ffilter#{settings["auto_ffilter"]}#{grp_id}',
                ),
            ],
            [
                InlineKeyboardButton(
                    'ğ�–¬ğ�–ºğ�—‘ ğ�–¡ğ�—�ğ�—�ğ�—�ğ�—ˆğ�—‡ğ�—Œ',
                    callback_data=f'setgs#max_btn#{settings["max_btn"]}#{grp_id}',
                ),
                InlineKeyboardButton(
                    '10' if settings["max_btn"] else f'{MAX_B_TN}',
                    callback_data=f'setgs#max_btn#{settings["max_btn"]}#{grp_id}',
                ),
            ],
        ]

        btn = [[
                InlineKeyboardButton("â¬‡ ğ�–®ğ�—‰ğ�–¾ğ�—‡ ğ�–§ğ�–¾ğ�—‹ğ�–¾ â¬‡", callback_data=f"opnsetgrp#{grp_id}"),
                InlineKeyboardButton("â�¡ ğ�–®ğ�—‰ğ�–¾ğ�—‡ ğ�—‚ğ�—‡ ğ�–¯ğ�–¬ â�¡", callback_data=f"opnsetpm#{grp_id}")
              ]]

        reply_markup = InlineKeyboardMarkup(buttons)
        if chat_type in [enums.ChatType.GROUP, enums.ChatType.SUPERGROUP]:
            await message.reply_text(
                text="<b>ğ�–£ğ�—ˆ ğ�–¸ğ�—ˆğ�—� ğ�–¶ğ�–ºğ�—‡ğ�—� ğ�–³ğ�—ˆ ğ�–®ğ�—‰ğ�–¾ğ�—‡ ğ�–²ğ�–¾ğ�—�ğ�—�ğ�—‚ğ�—‡ğ�—€ğ�—Œ ğ�–§ğ�–¾ğ�—‹ğ�–¾ ?</b>",
                reply_markup=InlineKeyboardMarkup(btn),
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.HTML,
                reply_to_message_id=message.id
            )
        else:
            await message.reply_text(
                text=f"<b>ğ�–¢ğ�—�ğ�–ºğ�—‡ğ�—€ğ�–¾ ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–²ğ�–¾ğ�—�ğ�—�ğ�—‚ğ�—‡ğ�—€ğ�—Œ ğ�–¥ğ�—ˆğ�—‹ {title} ğ�– ğ�—Œ ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–¶ğ�—‚ğ�—Œğ�—�</b>",
                reply_markup=reply_markup,
                disable_web_page_preview=True,
                parse_mode=enums.ParseMode.HTML,
                reply_to_message_id=message.id
            )

@Client.on_message(filters.command("send") & filters.user(ADMINS))
async def send_msg(bot, message):
    if message.reply_to_message:
        target_id = message.text
        command = ["/send"]
        out = "Users Saved In DB Are:\n\n"
        for cmd in command:
            if cmd in target_id:
                target_id = target_id.replace(cmd, "")
        success = False
        try:
            user = await bot.get_users(target_id)
            users = await db.get_all_users()
            async for usr in users:
                out += f"{usr['id']}"
                out += '\n'
            if str(user.id) in str(out):
                await message.reply_to_message.copy(int(user.id))
                success = True
            else:
                success = False
            if success:
                await message.reply_text(f"<b>Your message has been successfully send to {user.mention}.</b>")
            else:
                await message.reply_text("<b>This user didn't started this bot yet !</b>")
        except Exception as e:
            await message.reply_text(f"<b>Error: {e}</b>")
    else:
        await message.reply_text("<b>Use this command as a reply to any message using the target chat id. For eg: /send userid</b>")

@Client.on_message(filters.command('set_template'))
async def save_template(client, message):
    sts = await message.reply("Checking template")
    userid = message.from_user.id if message.from_user else None
    if not userid:
        return await message.reply(f"You are anonymous admin. Use /connect {message.chat.id} in PM")
    chat_type = message.chat.type

    if chat_type == enums.ChatType.PRIVATE:
        grpid = await active_connection(str(userid))
        if grpid is not None:
            grp_id = grpid
            try:
                chat = await client.get_chat(grpid)
                title = chat.title
            except:
                await message.reply_text("Make sure I'm present in your group!!", quote=True)
                return
        else:
            await message.reply_text("I'm not connected to any groups!", quote=True)
            return

    elif chat_type in [enums.ChatType.GROUP, enums.ChatType.SUPERGROUP]:
        grp_id = message.chat.id
        title = message.chat.title

    else:
        return

    st = await client.get_chat_member(grp_id, userid)
    if (
            st.status != enums.ChatMemberStatus.ADMINISTRATOR
            and st.status != enums.ChatMemberStatus.OWNER
            and str(userid) not in ADMINS
    ):
        return

    if len(message.command) < 2:
        return await sts.edit("No Input!!")
    template = message.text.split(" ", 1)[1]
    await save_group_settings(grp_id, 'template', template)
    await sts.edit(f"Successfully changed template for {title} to\n\n{template}")


@Client.on_message((filters.command(["request", "Request"]) | filters.regex("#request") | filters.regex("#Request")) & filters.group)
async def requests(bot, message):
    if REQST_CHANNEL is None or SUPPORT_CHAT_ID is None: return # Must add REQST_CHANNEL and SUPPORT_CHAT_ID to use this feature
    if message.reply_to_message and SUPPORT_CHAT_ID == message.chat.id:
        chat_id = message.chat.id
        reporter = str(message.from_user.id)
        mention = message.from_user.mention
        success = True
        content = message.reply_to_message.text
        try:
            if REQST_CHANNEL is not None:
                btn = [[
                        InlineKeyboardButton('ğŸ“¥ ğ�–µğ�—‚ğ�–¾ğ�—� ğ�–±ğ�–¾ğ�—Šğ�—�ğ�–¾ğ�—Œğ�—� ğŸ“¥', url=f"{message.reply_to_message.link}"),
                        InlineKeyboardButton('ğŸ“� ğ�–²ğ�—�ğ�—ˆğ�—� ğ�–®ğ�—‰ğ�—�ğ�—‚ğ�—ˆğ�—‡ğ�—Œ ğŸ“�', callback_data=f'show_option#{reporter}')
                      ]]
                reported_post = await bot.send_message(chat_id=REQST_CHANNEL, text=f"<b>ğ�–±ğ�–¾ğ�—‰ğ�—ˆğ�—‹ğ�—�ğ�–¾ğ�—‹ : {mention} ({reporter})\n\nğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾ : {content}</b>", reply_markup=InlineKeyboardMarkup(btn))
                success = True
            elif len(content) >= 3:
                for admin in ADMINS:
                    btn = [[
                        InlineKeyboardButton('ğŸ“¥ ğ�–µğ�—‚ğ�–¾ğ�—� ğ�–±ğ�–¾ğ�—Šğ�—�ğ�–¾ğ�—Œğ�—� ğŸ“¥', url=f"{message.reply_to_message.link}"),
                        InlineKeyboardButton('ğŸ“� ğ�–²ğ�—�ğ�—ˆğ�—� ğ�–®ğ�—‰ğ�—�ğ�—‚ğ�—ˆğ�—‡ğ�—Œ ğŸ“�', callback_data=f'show_option#{reporter}')
                      ]]
                    reported_post = await bot.send_message(chat_id=admin, text=f"<b>ğ�–±ğ�–¾ğ�—‰ğ�—ˆğ�—‹ğ�—�ğ�–¾ğ�—‹ : {mention} ({reporter})\n\nğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾ : {content}</b>", reply_markup=InlineKeyboardMarkup(btn))
                    success = True
            else:
                if len(content) < 3:
                    await message.reply_text("<b>You must type about your request [Minimum 3 Characters]. Requests can't be empty.</b>")
            if len(content) < 3:
                success = False
        except Exception as e:
            await message.reply_text(f"Error: {e}")
            pass
        
    elif SUPPORT_CHAT_ID == message.chat.id:
        chat_id = message.chat.id
        reporter = str(message.from_user.id)
        mention = message.from_user.mention
        success = True
        content = message.text
        keywords = ["#request", "/request", "#Request", "/Request"]
        for keyword in keywords:
            if keyword in content:
                content = content.replace(keyword, "")
        try:
            if REQST_CHANNEL is not None and len(content) >= 3:
                btn = [[
                        InlineKeyboardButton('ğŸ“¥ ğ�–µğ�—‚ğ�–¾ğ�—� ğ�–±ğ�–¾ğ�—Šğ�—�ğ�–¾ğ�—Œğ�—� ğŸ“¥', url=f"{message.link}"),
                        InlineKeyboardButton('ğŸ“� ğ�–²ğ�—�ğ�—ˆğ�—� ğ�–®ğ�—‰ğ�—�ğ�—‚ğ�—ˆğ�—‡ğ�—Œ ğŸ“�', callback_data=f'show_option#{reporter}')
                      ]]
                reported_post = await bot.send_message(chat_id=REQST_CHANNEL, text=f"<b>ğ�–±ğ�–¾ğ�—‰ğ�—ˆğ�—‹ğ�—�ğ�–¾ğ�—‹ : {mention} ({reporter})\n\nğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾ : {content}</b>", reply_markup=InlineKeyboardMarkup(btn))
                success = True
            elif len(content) >= 3:
                for admin in ADMINS:
                    btn = [[
                        InlineKeyboardButton('ğŸ“¥ ğ�–µğ�—‚ğ�–¾ğ�—� ğ�–±ğ�–¾ğ�—Šğ�—�ğ�–¾ğ�—Œğ�—� ğŸ“¥', url=f"{message.link}"),
                        InlineKeyboardButton('ğŸ“� ğ�–²ğ�—�ğ�—ˆğ�—� ğ�–®ğ�—‰ğ�—�ğ�—‚ğ�—ˆğ�—‡ğ�—Œ ğŸ“�', callback_data=f'show_option#{reporter}')
                      ]]
                    reported_post = await bot.send_message(chat_id=admin, text=f"<b>ğ�–±ğ�–¾ğ�—‰ğ�—ˆğ�—‹ğ�—�ğ�–¾ğ�—‹ : {mention} ({reporter})\n\nğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾ : {content}</b>", reply_markup=InlineKeyboardMarkup(btn))
                    success = True
            else:
                if len(content) < 3:
                    await message.reply_text("<b>You must type about your request [Minimum 3 Characters]. Requests can't be empty.</b>")
            if len(content) < 3:
                success = False
        except Exception as e:
            await message.reply_text(f"Error: {e}")
            pass

    else:
        success = False
    
    if success:
        btn = [[
                InlineKeyboardButton('ğŸ“¥ ğ�–µğ�—‚ğ�–¾ğ�—� ğ�–±ğ�–¾ğ�—Šğ�—�ğ�–¾ğ�—Œğ�—� ğŸ“¥', url=f"{reported_post.link}")
              ]]
        await message.reply_text("<b>Your request has been added! Please wait for some time.</b>", reply_markup=InlineKeyboardMarkup(btn))

@Client.on_message(filters.command("usend") & filters.user(ADMINS))
async def send_msg(bot, message):
    if message.reply_to_message:
        target_id = message.text.split(" ", 1)[1]
        out = "Users Saved In DB Are:\n\n"
        success = False
        try:
            user = await bot.get_users(target_id)
            users = await db.get_all_users()
            async for usr in users:
                out += f"{usr['id']}"
                out += '\n'
            if str(user.id) in str(out):
                await message.reply_to_message.copy(int(user.id))
                success = True
            else:
                success = False
            if success:
                await message.reply_text(f"<b>ğ�–¸ğ�—ˆğ�—�ğ�—‹ ğ�–¬ğ�–¾ğ�—Œğ�—Œğ�–ºğ�—€ğ�–¾ ğ�–§ğ�–ºğ�—Œ ğ�–²ğ�—�ğ�–¼ğ�–¼ğ�–¾ğ�—Œğ�—Œğ�–¿ğ�—�ğ�—…ğ�—…ğ�—’ ğ�–²ğ�–¾ğ�—‡ğ�—� ğ�–³ğ�—ˆ {user.mention}.</b>")
            else:
                await message.reply_text("<b>An Error Occured !</b>")
        except Exception as e:
            await message.reply_text(f"<b>Error :- <code>{e}</code></b>")
    else:
        await message.reply_text("<b>Errorğ�–¢ğ�—ˆğ�—†ğ�—†ğ�–ºğ�—‡ğ�–½ ğ�–¨ğ�—‡ğ�–¼ğ�—ˆğ�—†ğ�—‰ğ�—…ğ�–¾ğ�—�ğ�–¾ !</b>")
        
@Client.on_message(filters.command("send") & filters.user(ADMINS))
async def send_msg(bot, message):
    if message.reply_to_message:
        target_id = message.text.split(" ", 1)[1]
        out = "Users Saved In DB Are:\n\n"
        success = False
        try:
            user = await bot.get_users(target_id)
            users = await db.get_all_users()
            async for usr in users:
                out += f"{usr['id']}"
                out += '\n'
            if str(user.id) in str(out):
                await message.reply_to_message.copy(int(user.id))
                success = True
            else:
                success = False
            if success:
                await message.reply_text(f"<b>Your message has been successfully send to {user.mention}.</b>")
            else:
                await message.reply_text("<b>This user didn't started this bot yet !</b>")
        except Exception as e:
            await message.reply_text(f"<b>Error: {e}</b>")
    else:
        await message.reply_text("<b>Use this command as a reply to any message using the target chat id. For eg: /send userid</b>")

@Client.on_message(filters.command("gsend") & filters.user(ADMINS))
async def send_chatmsg(bot, message):
    if message.reply_to_message:
        target_id = message.text.split(" ", 1)[1]
        out = "Chats Saved In DB Are:\n\n"
        success = False
        try:
            chat = await bot.get_chat(target_id)
            chats = await db.get_all_chats()
            async for cht in chats:
                out += f"{cht['id']}"
                out += '\n'
            if str(chat.id) in str(out):
                await message.reply_to_message.copy(int(chat.id))
                success = True
            else:
                success = False
            if success:
                await message.reply_text(f"<b>Your message has been successfully send to <code>{chat.id}</code>.</b>")
            else:
                await message.reply_text("<b>An Error Occured !</b>")
        except Exception as e:
            await message.reply_text(f"<b>Error :- <code>{e}</code></b>")
    else:
        await message.reply_text("<b>Errorğ�–¢ğ�—ˆğ�—†ğ�—†ğ�–ºğ�—‡ğ�–½ ğ�–¨ğ�—‡ğ�–¼ğ�—ˆğ�—†ğ�—‰ğ�—…ğ�–¾ğ�—�ğ�–¾ !</b>")

@Client.on_message(filters.command("deletefiles") & filters.user(ADMINS))
async def deletemultiplefiles(bot, message):
    chat_type = message.chat.type
    if chat_type != enums.ChatType.PRIVATE:
        return await message.reply_text(f"<b>Hey {message.from_user.mention}, This command won't work in groups. It only works on my PM !</b>")
    else:
        pass
    try:
        keyword = message.text.split(" ", 1)[1]
    except:
        return await message.reply_text(f"<b>Hey {message.from_user.mention}, Give me a keyword along with the command to delete files.</b>")
    k = await bot.send_message(chat_id=message.chat.id, text=f"<b>Fetching Files for your query {keyword} on DB... Please wait...</b>")
    files, total = await get_bad_files(keyword)
    await k.edit_text(f"<b>Found {total} files for your query {keyword} !\n\nFile deletion process will start in 5 seconds !</b>")
    await asyncio.sleep(5)
    deleted = 0
    for file in files:
        await k.edit_text(f"<b>Process started for deleting files from DB. Successfully deleted {str(deleted)} files from DB for your query {keyword} !\n\nPlease wait...</b>")
        file_ids = file.file_id
        file_name = file.file_name
        result = await Media.collection.delete_one({
            '_id': file_ids,
        })
        if result.deleted_count:
            logger.info(f'File Found for your query {keyword}! Successfully deleted {file_name} from database.')
        deleted += 1
    await k.edit_text(text=f"<b>Process Completed for file deletion !\n\nSuccessfully deleted {str(deleted)} files from database for your query {keyword}.</b>")
