hikka_info.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import git
  7. from hikkatl.tl.types import Message
  8. from hikkatl.utils import get_display_name
  9. from .. import loader, utils, version
  10. from ..inline.types import InlineQuery
  11. @loader.tds
  12. class HikkaInfoMod(loader.Module):
  13. """Show userbot info"""
  14. strings = {"name": "HikkaInfo"}
  15. def __init__(self):
  16. self.config = loader.ModuleConfig(
  17. loader.ConfigValue(
  18. "custom_message",
  19. doc=lambda: self.strings("_cfg_cst_msg"),
  20. ),
  21. loader.ConfigValue(
  22. "custom_button",
  23. ["🌘 Support chat", "https://t.me/hikka_talks"],
  24. lambda: self.strings("_cfg_cst_btn"),
  25. validator=loader.validators.Union(
  26. loader.validators.Series(fixed_len=2),
  27. loader.validators.NoneType(),
  28. ),
  29. ),
  30. loader.ConfigValue(
  31. "banner_url",
  32. "https://github.com/hikariatama/assets/raw/master/hikka_banner.mp4",
  33. lambda: self.strings("_cfg_banner"),
  34. validator=loader.validators.Link(),
  35. ),
  36. )
  37. def _render_info(self, inline: bool) -> str:
  38. try:
  39. repo = git.Repo(search_parent_directories=True)
  40. diff = repo.git.log([f"HEAD..origin/{version.branch}", "--oneline"])
  41. upd = (
  42. self.strings("update_required") if diff else self.strings("up-to-date")
  43. )
  44. except Exception:
  45. upd = ""
  46. me = '<b><a href="tg://user?id={}">{}</a></b>'.format(
  47. self._client.hikka_me.id,
  48. utils.escape_html(get_display_name(self._client.hikka_me)),
  49. )
  50. build = utils.get_commit_url()
  51. _version = f'<i>{".".join(list(map(str, list(version.__version__))))}</i>'
  52. prefix = f"«<code>{utils.escape_html(self.get_prefix())}</code>»"
  53. platform = utils.get_named_platform()
  54. for emoji, icon in [
  55. ("🍊", "<emoji document_id=5449599833973203438>🧡</emoji>"),
  56. ("🍇", "<emoji document_id=5449468596952507859>💜</emoji>"),
  57. ("❓", "<emoji document_id=5407025283456835913>📱</emoji>"),
  58. ("🍀", "<emoji document_id=5395325195542078574>🍀</emoji>"),
  59. ("🦾", "<emoji document_id=5386766919154016047>🦾</emoji>"),
  60. ("🚂", "<emoji document_id=5359595190807962128>🚂</emoji>"),
  61. ("🐳", "<emoji document_id=5431815452437257407>🐳</emoji>"),
  62. ("🕶", "<emoji document_id=5407025283456835913>📱</emoji>"),
  63. ("🐈‍⬛", "<emoji document_id=6334750507294262724>🐈‍⬛</emoji>"),
  64. ("✌️", "<emoji document_id=5469986291380657759>✌️</emoji>"),
  65. ("📻", "<emoji document_id=5471952986970267163>💎</emoji>"),
  66. ]:
  67. platform = platform.replace(emoji, icon)
  68. return (
  69. (
  70. "<b>🌘 Hikka</b>\n"
  71. if "hikka" not in self.config["custom_message"].lower()
  72. else ""
  73. )
  74. + self.config["custom_message"].format(
  75. me=me,
  76. version=_version,
  77. build=build,
  78. prefix=prefix,
  79. platform=platform,
  80. upd=upd,
  81. uptime=utils.formatted_uptime(),
  82. cpu_usage=utils.get_cpu_usage(),
  83. ram_usage=f"{utils.get_ram_usage()} MB",
  84. branch=version.branch,
  85. )
  86. if self.config["custom_message"]
  87. else (
  88. f'<b>{{}}</b>\n\n<b>{{}} {self.strings("owner")}:</b> {me}\n\n<b>{{}}'
  89. f' {self.strings("version")}:</b> {_version} {build}\n<b>{{}}'
  90. f' {self.strings("branch")}:'
  91. f"</b> <code>{version.branch}</code>\n{upd}\n\n<b>{{}}"
  92. f' {self.strings("prefix")}:</b> {prefix}\n<b>{{}}'
  93. f' {self.strings("uptime")}:'
  94. f"</b> {utils.formatted_uptime()}\n\n<b>{{}}"
  95. f' {self.strings("cpu_usage")}:'
  96. f"</b> <i>~{utils.get_cpu_usage()} %</i>\n<b>{{}}"
  97. f' {self.strings("ram_usage")}:'
  98. f"</b> <i>~{utils.get_ram_usage()} MB</i>\n<b>{{}}</b>"
  99. ).format(
  100. *map(
  101. lambda x: utils.remove_html(x) if inline else x,
  102. (
  103. (
  104. utils.get_platform_emoji()
  105. if self._client.hikka_me.premium and not inline
  106. else "🌘 Hikka"
  107. ),
  108. "<emoji document_id=5373141891321699086>😎</emoji>",
  109. "<emoji document_id=5469741319330996757>💫</emoji>",
  110. "<emoji document_id=5449918202718985124>🌳</emoji>",
  111. "<emoji document_id=5472111548572900003>⌨️</emoji>",
  112. "<emoji document_id=5451646226975955576>⌛️</emoji>",
  113. "<emoji document_id=5431449001532594346>⚡️</emoji>",
  114. "<emoji document_id=5359785904535774578>💼</emoji>",
  115. platform,
  116. ),
  117. )
  118. )
  119. )
  120. def _get_mark(self):
  121. return (
  122. {
  123. "text": self.config["custom_button"][0],
  124. "url": self.config["custom_button"][1],
  125. }
  126. if self.config["custom_button"]
  127. else None
  128. )
  129. @loader.inline_handler(
  130. thumb_url="https://img.icons8.com/external-others-inmotus-design/344/external-Moon-round-icons-others-inmotus-design-2.png"
  131. )
  132. @loader.inline_everyone
  133. async def info(self, _: InlineQuery) -> dict:
  134. """Send userbot info"""
  135. return {
  136. "title": self.strings("send_info"),
  137. "description": self.strings("description"),
  138. **(
  139. {"photo": self.config["banner_url"], "caption": self._render_info(True)}
  140. if self.config["banner_url"]
  141. else {"message": self._render_info(True)}
  142. ),
  143. "thumb": (
  144. "https://github.com/hikariatama/Hikka/raw/master/assets/hikka_pfp.png"
  145. ),
  146. "reply_markup": self._get_mark(),
  147. }
  148. @loader.command()
  149. async def infocmd(self, message: Message):
  150. if self.config["custom_button"]:
  151. await self.inline.form(
  152. message=message,
  153. text=self._render_info(True),
  154. reply_markup=self._get_mark(),
  155. **(
  156. {"photo": self.config["banner_url"]}
  157. if self.config["banner_url"]
  158. else {}
  159. ),
  160. )
  161. else:
  162. await utils.answer_file(
  163. message,
  164. self.config["banner_url"],
  165. self._render_info(False),
  166. )
  167. @loader.command()
  168. async def hikkainfo(self, message: Message):
  169. await utils.answer(message, self.strings("desc"))
  170. @loader.command()
  171. async def setinfo(self, message: Message):
  172. if not (args := utils.get_args_html(message)):
  173. return await utils.answer(message, self.strings("setinfo_no_args"))
  174. self.config["custom_message"] = args
  175. await utils.answer(message, self.strings("setinfo_success"))