text: # Copyright (C) 2019 The Raphielscape Company LLC.
  #
  # Licensed under the Raphielscape Public License, Version 1.c (the "License");
  # you may not use this file except in compliance with the License.
  #
  """ Userbot module for getting information about the server. """
  
  import time
  from asyncio import create_subprocess_exec as asyncrunapp
  from asyncio.subprocess import PIPE as asyncPIPE
  from os import remove
  from platform import python_version, uname
  from shutil import which
  
  from git import Repo
  from telethon import version
  from telethon.errors.rpcerrorlist import MediaEmptyError
  
  from userbot import ALIVE_LOGO, ALIVE_NAME, CMD_HELP, StartTime, bot
  from userbot.events import register
  
  # ================= CONSTANT =================
  DEFAULTUSER = str(ALIVE_NAME) if ALIVE_NAME else uname().node
  repo = Repo()
  # ============================================
  
  
  async def get_readable_time(seconds: int) -> str:
      count = 0
      up_time = ""
      time_list = []
      time_suffix_list = ["s", "m", "h", "days"]
  
      while count < 4:
          count += 1
          if count < 3:
              remainder, result = divmod(seconds, 60)
          else:
              remainder, result = divmod(seconds, 24)
          if seconds == 0 and remainder == 0:
              break
          time_list.append(int(result))
          seconds = int(remainder)
  
      for x in range(len(time_list)):
          time_list[x] = str(time_list[x]) + time_suffix_list[x]
      if len(time_list) == 4:
          up_time += time_list.pop() + ", "
  
      time_list.reverse()
      up_time += ":".join(time_list)
  
      return up_time
  
  
  @register(outgoing=True, pattern="^.sysd$")
  async def sysdetails(sysd):
      """For .sysd command, get system info using neofetch."""
      if not sysd.text[0].isalpha() and sysd.text[0] not in ("/", "#", "@", "!"):
          try:
              fetch = await asyncrunapp(
                  "neofetch",
                  "--stdout",
                  stdout=asyncPIPE,
                  stderr=asyncPIPE,
              )
  
              stdout, stderr = await fetch.communicate()
              result = str(stdout.decode().strip()) + str(stderr.decode().strip())
  
              await sysd.edit("`" + result + "`")
          except FileNotFoundError:
              await sysd.edit("`Install neofetch first !!`")
  
  
  @register(outgoing=True, pattern=r"^\.botver$")
  async def bot_ver(event):
      """For .botver command, get the bot version."""
      if not event.text[0].isalpha() and event.text[0] not in ("/", "#", "@", "!"):
          if which("git") is not None:
              ver = await asyncrunapp(
                  "git",
                  "describe",
                  "--all",
                  "--long",
                  stdout=asyncPIPE,
                  stderr=asyncPIPE,
              )
              stdout, stderr = await ver.communicate()
              verout = str(stdout.decode().strip()) + str(stderr.decode().strip())
  
              rev = await asyncrunapp(
                  "git",
                  "rev-list",
                  "--all",
                  "--count",
                  stdout=asyncPIPE,
                  stderr=asyncPIPE,
              )
              stdout, stderr = await rev.communicate()
              revout = str(stdout.decode().strip()) + str(stderr.decode().strip())
  
              await event.edit(
                  "`Userbot Version: " f"{verout}" "` \n" "`Revision: " f"{revout}" "`"
              )
          else:
              await event.edit(
                  "Shame that you don't have git, you're running - 'v1.beta.4' anyway!"
              )
  
  
  @register(outgoing=True, pattern=r"^\.pip(?: |$)(.*)")
  async def pipcheck(pip):
      """For .pip command, do a pip search."""
      if not pip.text[0].isalpha() and pip.text[0] not in ("/", "#", "@", "!"):
          pipmodule = pip.pattern_match.group(1)
          if pipmodule:
              await pip.edit("`Searching . . .`")
              pipc = await asyncrunapp(
                  "pip3",
                  "search",
                  pipmodule,
                  stdout=asyncPIPE,
                  stderr=asyncPIPE,
              )
  
              stdout, stderr = await pipc.communicate()
              pipout = str(stdout.decode().strip()) + str(stderr.decode().strip())
  
              if pipout:
                  if len(pipout) > 4096:
                      await pip.edit("`Output too large, sending as file`")
                      file = open("output.txt", "w+")
                      file.write(pipout)
                      file.close()
                      await pip.client.send_file(
                          pip.chat_id,
                          "output.txt",
                          reply_to=pip.id,
                      )
                      remove("output.txt")
                      return
                  await pip.edit(
                      "**Query: **\n`"
                      f"pip3 search {pipmodule}"
                      "`\n**Result: **\n`"
                      f"{pipout}"
                      "`"
                  )
              else:
                  await pip.edit(
                      "**Query: **\n`"
                      f"pip3 search {pipmodule}"
                      "`\n**Result: **\n`No Result Returned/False`"
                  )
          else:
              await pip.edit("`Use .help pip to see an example`")
  
  
  @register(outgoing=True, pattern=r"^\.(alive|on)$")
  async def amireallyalive(alive):
      """For .alive command, check if the bot is running."""
      uptime = await get_readable_time((time.time() - StartTime))
      logo = ALIVE_LOGO
      output = (
          f"**⏱ Uptime** : `{uptime}`\n"
          f"**💡 Version** : `{repo.active_branch.name}`\n"
          f"**👤 User** : `{DEFAULTUSER}`\n"
          f"                           \n"
          f"**__Python__**: `{python_version()}`\n"
          f"**__Telethon__**: `{version.__version__}`\n"
          f"                           \n"
          f"🎖**[GNU GPL v3.0](https://github.com/ythm00/Cyber/blob/master/LICENSE)** | 👤 **[Cyber](https://github.com/ythm00)** |  🧪 **[Repo](https://github.com/ythm00/Cyber)**"
      )
      if ALIVE_LOGO:
          try:
              logo = ALIVE_LOGO
              await bot.send_file(alive.chat_id, logo, caption=output)
              await alive.delete()
          except MediaEmptyError:
              await alive.edit(
                  output + "\n\n *`The provided logo is invalid."
                  "\nMake sure the link is directed to the logo picture`"
              )
      else:
          await alive.edit(output)
  
  
  @register(outgoing=True, pattern="^.aliveu")
  async def amireallyaliveuser(username):
      """For .aliveu command, change the username in the .alive command."""
      message = username.text
      output = ".aliveu [new user without brackets] nor can it be empty"
      if not (message == ".aliveu" or message[7:8] != " "):
          newuser = message[8:]
          global DEFAULTUSER
          DEFAULTUSER = newuser
          output = "Successfully changed user to " + newuser + "!"
      await username.edit("`" f"{output}" "`")
  
  
  @register(outgoing=True, pattern=r"^\.resetalive$")
  async def amireallyalivereset(ureset):
      """For .resetalive command, reset the username in the .alive command."""
      global DEFAULTUSER
      DEFAULTUSER = str(ALIVE_NAME) if ALIVE_NAME else uname().node
      await ureset.edit("`" "Successfully reset user for alive!" "`")
  
  
  CMD_HELP.update(
      {
          "sysd": ">`.sysd`" "\nUsage: Shows system information using neofetch.",
          "botver": ">`.botver`" "\nUsage: Shows the userbot version.",
          "pip": ">`.pip <module(s)>`" "\nUsage: Does a search of pip modules(s).",
          "alive": ">`.alive`"
          "\nUsage: Type .alive to see wether your bot is working or not."
          "\n\n>`.aliveu <text>`"
          "\nUsage: Changes the 'user' in alive to the text you want."
          "\n\n>`.resetalive`"
          "\nUsage: Resets the user to default.",
      }
  )
  