text: from math import ceil
  from re import compile
  import asyncio
  import html
  import os
  import re
  import sys
  
  from telethon import Button, custom, events, functions
  from telethon.tl.functions.users import GetFullUserRequest
  from telethon.events import InlineQuery, callbackquery
  from telethon.sync import custom
  from telethon.errors.rpcerrorlist import UserNotParticipantError
  from telethon.tl.functions.channels import GetParticipantRequest
  from telethon.tl.functions.channels import JoinChannelRequest
  from telethon.tl.functions.messages import ExportChatInviteRequest
  
  from . import *
  
  speedo_row = Config.BUTTONS_IN_HELP
  speedo_emoji = Config.EMOJI_IN_HELP
  speedo_pic = Config.PMPERMIT_PIC or "https://telegra.ph/file/f46b11140c307df13750d.jpg"
  cstm_pmp = Config.CUSTOM_PMPERMIT
  ALV_PIC = Config.ALIVE_PIC
  help_pic = Config.HELP_PIC or "https://telegra.ph/file/f46b11140c307df13750d.jpg"
  PM_WARNS = {}
  PREV_REPLY_MESSAGE = {}
  
  mybot = Config.BOT_USERNAME
  if mybot.startswith("@"):
      botname = mybot
  else:
      botname = f"@{mybot}"
  LOG_GP = Config.LOGGER_ID
  mssge = (
      str(cstm_pmp)
      if cstm_pmp
      else "**You Have Trespassed To My Master's PM!\nThis Is Illegal And Regarded As Crime.**"
  )
  
  USER_BOT_WARN_ZERO = "Enough Of Your Flooding In My Master's PM!! \n\n**🚫 Blocked and Reported**"
  
  SPEEDO_FIRST = (
      "**🔥 𝒮𝒫𝐸𝐸𝒟𝒪 𝒫𝑅𝐼𝒱𝒜𝒯𝐸 𝒮𝐸𝒞𝒰𝑅𝐼𝒯𝒴 𝒫𝑅𝒪𝒯𝒪𝒞𝒪𝐿 🔥**\n\nThis is to inform you that "
      "{} is currently unavailable.\nThis is an automated message.\n\n"
      "{}\n\n**Please Choose Why You Are Here!!**"
  )
  
  alive_txt = """
  **⚜️ 𝒮𝒫𝐸𝐸𝒟𝒪 ιѕ σиℓιиє ⚜️**
  {}
  **🏅 𝙱𝚘𝚝 𝚂𝚝𝚊𝚝𝚞𝚜 🏅**
  
  **Telethon :**  `{}`
  **SPEEDOBOT  :**  **{}**
  **Abuse    :**  **{}**
  **Sudo      :**  **{}**
  """
  
  def button(page, modules):
      Row = speedo_row
      Column = 3
  
      modules = sorted([modul for modul in modules if not modul.startswith("_")])
      pairs = list(map(list, zip(modules[::2], modules[1::2])))
      if len(modules) % 2 == 1:
          pairs.append([modules[-1]])
      max_pages = ceil(len(pairs) / Row)
      pairs = [pairs[i : i + Row] for i in range(0, len(pairs), Row)]
      buttons = []
      for pairs in pairs[page]:
          buttons.append(
              [
                  custom.Button.inline(f"{speedo_emoji} " + pair + f" {speedo_emoji}", data=f"Information[{page}]({pair})")
                  for pair in pairs
              ]
          )
  
      buttons.append(
          [
              custom.Button.inline(
                 f"◀️ Back {speedo_emoji}", data=f"page({(max_pages - 1) if page == 0 else (page - 1)})"
              ),
              custom.Button.inline(
                 f"• ❌ •", data="close"
              ),
              custom.Button.inline(
                 f"{speedo_emoji} Next ▶️", data=f"page({0 if page == (max_pages - 1) else page + 1})"
              ),
          ]
      )
      return [max_pages, buttons]
  
  
      modules = CMD_HELP
  if Config.BOT_USERNAME is not None and tgbot is not None:
      @tgbot.on(InlineQuery)  # pylint:disable=E0602
      async def inline_handler(event):
          builder = event.builder
          result = None
          query = event.text
          if event.query.user_id == bot.uid and query == "Speedo_help":
              rev_text = query[::-1]
              veriler = button(0, sorted(CMD_HELP))
              apn = []
              for x in CMD_LIST.values():
                  for y in x:
                      apn.append(y)
              help_msg = f"🔰 **{speedo_mention}**\n\n📜 __No.of Plugins__ : `{len(CMD_HELP)}` \n🗂️ __Commands__ : `{len(apn)}`\n🗒️ __Page__ : 1/{veriler[0]}"
              if help_pic and help_pic.endswith((".jpg", ".png")):
                  result = builder.photo(
                      help_pic,
                      text=help_msg,
                      buttons=veriler[1],
                      link_preview=False,
                  )
              elif help_pic:
                  result = builder.document(
                      help_pic,
                      text=help_msg,
                      title="Speedo Alive",
                      buttons=veriler[1],
                      link_preview=False,
                  )
              else:
                  result = builder.article(
                      f"Hey! Only use .help please",
                      text=help_msg,
                      buttons=veriler[1],
                      link_preview=False,
                  )
          elif event.query.user_id == bot.uid and query.startswith("fsub"):
              hunter = event.pattern_match.group(1)
              speedo = hunter.split("+")
              user = await bot.get_entity(int(speedo[0]))
              channel = await bot.get_entity(int(speedo[1]))
              msg = f"**👋 Welcome** [{user.first_name}](tg://user?id={user.id}), \n\n**📍 You need to Join** {channel.title} **to chat in this group.**"
              if not channel.username:
                  link = (await bot(ExportChatInviteRequest(channel))).link
              else:
                  link = "https://t.me/" + channel.username
              result = [
                  await builder.article(
                      title="force_sub",
                      text = msg,
                      buttons=[
                          [Button.url(text="Channel", url=link)],
                          [custom.Button.inline("🔓 Unmute Me", data=unmute)],
                      ],
                  )
              ]
  
          elif event.query.user_id == bot.uid and query == "alive":
              he_ll = alive_txt.format(Config.ALIVE_MSG, tel_ver, speedo_ver, abuse_m, is_sudo)
              alv_btn = [
                  [Button.url(f"{SPEEDO_USER}", f"tg://openmessage?user_id={ForGo10God}")],
                  [Button.url("My Channel", f"https://t.me/{my_channel}"), 
                  Button.url("My Group", f"https://t.me/{my_group}")],
              ]
              if ALV_PIC and ALV_PIC.endswith((".jpg", ".png")):
                  result = builder.photo(
                      ALV_PIC,
                      text=he_ll,
                      buttons=alv_btn,
                      link_preview=False,
                  )
              elif ALV_PIC:
                  result = builder.document(
                      ALV_PIC,
                      text=he_ll,
                      title="Speedo Alive",
                      buttons=alv_btn,
                      link_preview=False,
                  )
              else:
                  result = builder.article(
                      text=he_ll,
                      title="Speedo Alive",
                      buttons=alv_btn,
                      link_preview=False,
                  )
  
          elif event.query.user_id == bot.uid and query == "pm_warn":
              hel_l = SPEEDO_FIRST.format(speedo_mention, mssge)
              result = builder.photo(
                  file=speedo_pic,
                  text=hel_l,
                  buttons=[
                      [
                          custom.Button.inline("📝 Request 📝", data="req"),
                          custom.Button.inline("💬 Chat 💬", data="chat"),
                      ],
                      [custom.Button.inline("🚫 Spam 🚫", data="heheboi")],
                      [custom.Button.inline("Curious ❓", data="pmclick")],
                  ],
              )
  
          elif event.query.user_id == bot.uid and query == "repo":
              result = builder.article(
                  title="Repository",
                  text=f"**⚡ ʟɛɢɛռɖaʀʏ ᴀғ Speedo ⚡**",
                  buttons=[
                      [Button.url("📑 Repo 📑", "https://github.com/The-Speedo/Speedo")],
                      [Button.url("🚀 Deploy 🚀", "https://dashboard.heroku.com/new?button-url=https%3A%2F%2Fgithub.com%2FThe-Speedo%2FSpeedo&template=https%3A%2F%2Fgithub.com%2Fthe-Speedo%2FSpeedo")],
                  ],
              )
  
          elif query.startswith("http"):
              part = query.split(" ")
              result = builder.article(
                  "File uploaded",
                  text=f"**File uploaded successfully to {part[2]} site.\n\nUpload Time : {part[1][:3]} second\n[‏‏‎ ‎]({part[0]})",
                  buttons=[[custom.Button.url("URL", part[0])]],
                  link_preview=True,
              )
  
          else:
              result = builder.article(
                  "@Its_Speedo",
                  text="""**Hey! This is [SPEEDOBOT](https://t.me/its_Speedo) \nYou can know more about me from the links given below 👇**""",
                  buttons=[
                      [
                          custom.Button.url("🔥 CHANNEL 🔥", "https://t.me/Its_Speedo"),
                          custom.Button.url(
                              "⚡ GROUP ⚡", "https://t.me/Speedo_chat"
                          ),
                      ],
                      [
                          custom.Button.url(
                              "✨ REPO ✨", "https://github.com/The-Speedo/Speedo"),
                          custom.Button.url
                      (
                              "🔰 TUTORIAL 🔰", "#todo"
                      )
                      ],
                  ],
                  link_preview=False,
              )
          await event.answer([result] if result else None)
  
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"pmclick")))
      async def on_pm_click(event):
          if event.query.user_id == bot.uid:
              reply_pop_up_alert = "This is for Other Users..."
              await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
          else:
              await event.edit(
                  f"🔰 This is SPEEDOBOT PM Security for {speedo_mention} to keep away unwanted retards from spamming PM..."
              )
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"req")))
      async def on_pm_click(event):
          if event.query.user_id == bot.uid:
              reply_pop_up_alert = "This is for other users!"
              await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
          else:
              await event.edit(
                  f"✅ **𝒟𝑒𝒶𝓇 𝒴𝑜𝓊𝓇 𝑅𝑒𝓆𝓊𝑒𝓈𝓉 𝑅𝑒𝑔𝒾𝓈𝓉𝑒𝓇𝑒𝒹 𝒮𝓊𝒸𝒸𝑒𝓈𝓈𝒻𝓊𝓁** \n\n{speedo_mention} 𝓌𝒾𝓁𝓁 𝓃𝑜𝓌 𝒹𝑒𝒸𝒾𝒹𝑒 𝓉𝑜 𝓁𝑜𝑜𝓀 𝒻𝑜𝓇 𝓎𝑜𝓊𝓇 𝓇𝑒𝓆𝓊𝑒𝓈𝓉 𝑜𝓇 𝓃𝑜𝓉.\n😐 𝒯𝒾𝓁𝓁 𝓉𝒽𝑒𝓃 𝓌𝒶𝒾𝓉 𝓅𝒶𝓉𝒾𝑒𝓃𝓉𝓁𝓎 𝒶𝓃𝒹 𝒹𝑜𝓃'𝓉 𝓈𝓅𝒶𝓂!!"
              )
              target = await event.client(GetFullUserRequest(event.query.user_id))
              first_name = html.escape(target.user.first_name)
              ok = event.query.user_id
              if first_name is not None:
                  first_name = first_name.replace("\u2060", "")
              tosend = f"**👀 Hey {speedo_mention} !!** \n\n⚜️ 𝔜𝔬𝔲 𝔊𝔬𝔱 𝔄 ℜ𝔢𝔮𝔲𝔢𝔰𝔱 𝔉𝔯𝔬𝔪 [{first_name}](tg://user?id={ok}) In PM!!"
              await bot.send_message(LOG_GP, tosend)
  
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"chat")))
      async def on_pm_click(event):
          event.query.user_id
          if event.query.user_id == bot.uid:
              reply_pop_up_alert = "This is for other users!"
              await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
          else:
              await event.edit(
                  f"Ahh!! You here to do chit-chat!!\n\nPlease wait for {speedo_mention} to come. Till then keep patience and don't spam."
              )
              target = await event.client(GetFullUserRequest(event.query.user_id))
              ok = event.query.user_id
              first_name = html.escape(target.user.first_name)
              if first_name is not None:
                  first_name = first_name.replace("\u2060", "")
              tosend = f"**👀 Hey {speedo_mention} !!** \n\n⚜️ You Got A PM from  [{first_name}](tg://user?id={ok})  for random chats!!"
              await bot.send_message(LOG_GP, tosend)
  
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"heheboi")))
      async def on_pm_click(event):
          if event.query.user_id == bot.uid:
              reply_pop_up_alert = "This is for other users!"
              await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
          else:
              await event.edit(
                  f"🥴 **Nikal lawde\nPehli fursat me nikal**"
              )
              await bot(functions.contacts.BlockRequest(event.query.user_id))
              target = await event.client(GetFullUserRequest(event.query.user_id))
              ok = event.query.user_id
              first_name = html.escape(target.user.first_name)
              if first_name is not None:
                  first_name = first_name.replace("\u2060", "")
              first_name = html.escape(target.user.first_name)
              await bot.send_message(
                  LOG_GP,
                  f"**Blocked**  [{first_name}](tg://user?id={ok}) \n\nReason:- Spam",
              )
  
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"unmute")))
      async def on_pm_click(event):
          hunter = (event.data_match.group(1)).decode("UTF-8")
          speedo = hunter.split("+")
          if not event.sender_id == int(speedo[0]):
              return await event.answer("This Ain't For You!!", alert=True)
          try:
              await bot(GetParticipantRequest(int(speedo[1]), int(speedo[0])))
          except UserNotParticipantError:
              return await event.answer(
                  "You need to join the channel first.", alert=True
              )
          await bot.edit_permissions(
              event.chat_id, int(speedo[0]), send_message=True, until_date=None
          )
          await event.edit("Yay! You can chat now !!")
  
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"reopen")))
      async def reopn(event):
              if event.query.user_id == bot.uid or event.query.user_id in Config.SUDO_USERS:
                  current_page_number=0
                  simp = button(current_page_number, CMD_HELP)
                  veriler = button(0, sorted(CMD_HELP))
                  apn = []
                  for x in CMD_LIST.values():
                      for y in x:
                          apn.append(y)
                  await event.edit(
                      f"🔰 **{speedo_mention}**\n\n📜 __No.of Plugins__ : `{len(CMD_HELP)}` \n🗂️ __Commands__ : `{len(apn)}`\n🗒️ __Page__ : 1/{veriler[0]}",
                      buttons=simp[1],
                      link_preview=False,
                  )
              else:
                  reply_pop_up_alert = "𝕲𝖊𝖙 𝖄𝖔𝖚𝖗 𝕺𝖜𝖓 𝕻𝖔𝖜𝖊𝖗𝖋𝖚𝖑 𝕭𝖔𝖙 𝕾𝖕𝖊𝖊𝖉𝖔 . © SPEEDOBOT ™"
                  await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
          
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"close")))
      async def on_plug_in_callback_query_handler(event):
          if event.query.user_id == bot.uid or event.query.user_id in Config.SUDO_USERS:
              veriler = custom.Button.inline(f"{speedo_emoji} Re-Open Menu {speedo_emoji}", data="reopen")
              await event.edit(f"**⚜️ 𝒮𝓅𝑒𝑒𝒹𝑜 𝑀𝑒𝓃𝓊 𝒞𝓁𝑜𝓈𝑒𝒹 ⚜️**\n\n**Bot Of :**  {speedo_mention}\n\n        [©️ SPEEDOBOT ™️]({chnl_link})", buttons=veriler, link_preview=False)
          else:
              reply_pop_up_alert = "𝕲𝖊𝖙 𝖄𝖔𝖚𝖗 𝕺𝖜𝖓 𝕻𝖔𝖜𝖊𝖗𝖋𝖚𝖑 𝕭𝖔𝖙 𝕾𝖕𝖊𝖊𝖉𝖔. © SPEEDOBOT ™"
              await event.answer(reply_pop_up_alert, cache_time=0, alert=True)
     
  
      @tgbot.on(callbackquery.CallbackQuery(data=compile(b"page\((.+?)\)")))
      async def page(event):
          page = int(event.data_match.group(1).decode("UTF-8"))
          veriler = button(page, CMD_HELP)
          apn = []
          for x in CMD_LIST.values():
              for y in x:
                  apn.append(y)
          if event.query.user_id == bot.uid or event.query.user_id in Config.SUDO_USERS:
              await event.edit(
                  f"🔰 **{speedo_mention}**\n\n📜 __No.of Plugins__ : `{len(CMD_HELP)}`\n🗂️ __Commands__ : `{len(apn)}`\n🗒️ __Page__ : {page + 1}/{veriler[0]}",
                  buttons=veriler[1],
                  link_preview=False,
              )
          else:
              return await event.answer(
                  "𝕲𝖊𝖙 𝖄𝖔𝖚𝖗 𝕺𝖜𝖓 𝕻𝖔𝖜𝖊𝖗𝖋𝖚𝖑 𝕭𝖔𝖙 𝕾𝖕𝖊𝖊𝖉𝖔. © SPEEDOBOT ™",
                  cache_time=0,
                  alert=True,
              )
  
  
      @tgbot.on(
          callbackquery.CallbackQuery(data=compile(b"Information\[(\d*)\]\((.*)\)"))
      )
      async def Information(event):
          page = int(event.data_match.group(1).decode("UTF-8"))
          commands = event.data_match.group(2).decode("UTF-8")
          try:
              buttons = [
                  custom.Button.inline(
                      "⚡ " + cmd[0] + " ⚡", data=f"commands[{commands}[{page}]]({cmd[0]})"
                  )
                  for cmd in CMD_HELP_BOT[commands]["commands"].items()
              ]
          except KeyError:
              return await event.answer(
                  "No Description is written for this plugin", cache_time=0, alert=True
              )
  
          buttons = [buttons[i : i + 2] for i in range(0, len(buttons), 2)]
          buttons.append([custom.Button.inline(f"{speedo_emoji} Main Menu {speedo_emoji}", data=f"page({page})")])
          if event.query.user_id == bot.uid or event.query.user_id in Config.SUDO_USERS:
              await event.edit(
                  f"**📗 File :**  `{commands}`\n**🔢 Number of commands :**  `{len(CMD_HELP_BOT[commands]['commands'])}`",
                  buttons=buttons,
                  link_preview=False,
              )
          else:
              return await event.answer(
                  "𝕲𝖊𝖙 𝖄𝖔𝖚𝖗 𝕺𝖜𝖓 𝕻𝖔𝖜𝖊𝖗𝖋𝖚𝖑 𝕭𝖔𝖙 𝕾𝖕𝖊𝖊𝖉𝖔. © SPEEDOBOT ™",
                  cache_time=0,
                  alert=True,
              )
  
  
      @tgbot.on(
          callbackquery.CallbackQuery(data=compile(b"commands\[(.*)\[(\d*)\]\]\((.*)\)"))
      )
      async def commands(event):
          cmd = event.data_match.group(1).decode("UTF-8")
          page = int(event.data_match.group(2).decode("UTF-8"))
          commands = event.data_match.group(3).decode("UTF-8")
          result = f"**📗 File :**  `{cmd}`\n"
          if CMD_HELP_BOT[cmd]["info"]["info"] == "":
              if not CMD_HELP_BOT[cmd]["info"]["warning"] == "":
                  result += f"**⚠️ Warning :**  {CMD_HELP_BOT[cmd]['info']['warning']}\n\n"
          else:
              if not CMD_HELP_BOT[cmd]["info"]["warning"] == "":
                  result += f"**⚠️ Warning :**  {CMD_HELP_BOT[cmd]['info']['warning']}\n"
              result += f"**ℹ️ Info :**  {CMD_HELP_BOT[cmd]['info']['info']}\n\n"
          command = CMD_HELP_BOT[cmd]["commands"][commands]
          if command["params"] is None:
              result += f"**🛠 Commands :**  `{HANDLER[:1]}{command['command']}`\n"
          else:
              result += f"**🛠 Commands :**  `{HANDLER[:1]}{command['command']} {command['params']}`\n"
          if command["example"] is None:
              result += f"**💬 Explanation :**  `{command['usage']}`\n\n"
          else:
              result += f"**💬 Explanation :**  `{command['usage']}`\n"
              result += f"**⌨️ For Example :**  `{HANDLER[:1]}{command['example']}`\n\n"
          if event.query.user_id == bot.uid or event.query.user_id in Config.SUDO_USERS:
              await event.edit(
                  result,
                  buttons=[
                      custom.Button.inline(f"{speedo_emoji} Return {speedo_emoji}", data=f"Information[{page}]({cmd})")
                  ],
                  link_preview=False,
              )
          else:
              return await event.answer(
                  "𝕲𝖊𝖙 𝖄𝖔𝖚𝖗 𝕺𝖜𝖓 𝕻𝖔𝖜𝖊𝖗𝖋𝖚𝖑 𝕭𝖔𝖙 𝕾𝖕𝖊𝖊𝖉𝖔 © SPEEDOBOT ™",
                  cache_time=0,
                  alert=True,
              )
  
  
  # Speedo
  