settings.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 hikkapyro
  7. import hikkatl
  8. from hikkatl.extensions.html import CUSTOM_EMOJIS
  9. from hikkatl.tl.types import Message
  10. from .. import loader, main, utils, version
  11. from ..compat.dragon import DRAGON_EMOJI
  12. from ..inline.types import InlineCall
  13. @loader.tds
  14. class CoreMod(loader.Module):
  15. """Control core userbot settings"""
  16. strings = {"name": "Settings"}
  17. async def blacklistcommon(self, message: Message):
  18. args = utils.get_args(message)
  19. if len(args) > 2:
  20. await utils.answer(message, self.strings("too_many_args"))
  21. return
  22. chatid = None
  23. module = None
  24. if args:
  25. try:
  26. chatid = int(args[0])
  27. except ValueError:
  28. module = args[0]
  29. if len(args) == 2:
  30. module = args[1]
  31. if chatid is None:
  32. chatid = utils.get_chat_id(message)
  33. module = self.allmodules.get_classname(module)
  34. return f"{str(chatid)}.{module}" if module else chatid
  35. @loader.command()
  36. async def hikkacmd(self, message: Message):
  37. await utils.answer_file(
  38. message,
  39. "https://github.com/hikariatama/assets/raw/master/hikka_cat_banner.mp4",
  40. self.strings("hikka").format(
  41. (
  42. utils.get_platform_emoji()
  43. if self._client.hikka_me.premium and CUSTOM_EMOJIS
  44. else "🌘 <b>Hikka userbot</b>"
  45. ),
  46. *version.__version__,
  47. utils.get_commit_url(),
  48. f"{hikkatl.__version__} #{hikkatl.tl.alltlobjects.LAYER}",
  49. (
  50. "<emoji document_id=5377399247589088543>🔥</emoji>"
  51. if self._client.pyro_proxy
  52. else "<emoji document_id=5418308381586759720>📴</emoji>"
  53. ),
  54. f"{hikkapyro.__version__} #{hikkapyro.raw.all.layer}",
  55. )
  56. + (
  57. ""
  58. if version.branch == "master"
  59. else self.strings("unstable").format(version.branch)
  60. ),
  61. )
  62. @loader.command()
  63. async def blacklist(self, message: Message):
  64. chatid = await self.blacklistcommon(message)
  65. self._db.set(
  66. main.__name__,
  67. "blacklist_chats",
  68. self._db.get(main.__name__, "blacklist_chats", []) + [chatid],
  69. )
  70. await utils.answer(message, self.strings("blacklisted").format(chatid))
  71. @loader.command()
  72. async def unblacklist(self, message: Message):
  73. chatid = await self.blacklistcommon(message)
  74. self._db.set(
  75. main.__name__,
  76. "blacklist_chats",
  77. list(set(self._db.get(main.__name__, "blacklist_chats", [])) - {chatid}),
  78. )
  79. await utils.answer(message, self.strings("unblacklisted").format(chatid))
  80. async def getuser(self, message: Message):
  81. try:
  82. return int(utils.get_args(message)[0])
  83. except (ValueError, IndexError):
  84. if reply := await message.get_reply_message():
  85. return reply.sender_id
  86. return message.to_id.user_id if message.is_private else False
  87. @loader.command()
  88. async def blacklistuser(self, message: Message):
  89. if not (user := await self.getuser(message)):
  90. await utils.answer(message, self.strings("who_to_blacklist"))
  91. return
  92. self._db.set(
  93. main.__name__,
  94. "blacklist_users",
  95. self._db.get(main.__name__, "blacklist_users", []) + [user],
  96. )
  97. await utils.answer(message, self.strings("user_blacklisted").format(user))
  98. @loader.command()
  99. async def unblacklistuser(self, message: Message):
  100. if not (user := await self.getuser(message)):
  101. await utils.answer(message, self.strings("who_to_unblacklist"))
  102. return
  103. self._db.set(
  104. main.__name__,
  105. "blacklist_users",
  106. list(set(self._db.get(main.__name__, "blacklist_users", [])) - {user}),
  107. )
  108. await utils.answer(
  109. message,
  110. self.strings("user_unblacklisted").format(user),
  111. )
  112. @loader.command()
  113. async def setprefix(self, message: Message):
  114. if not (args := utils.get_args_raw(message)):
  115. await utils.answer(message, self.strings("what_prefix"))
  116. return
  117. if len(args.split()) == 2 and args.split()[0] == "dragon":
  118. args = args.split()[1]
  119. is_dragon = True
  120. else:
  121. is_dragon = False
  122. if len(args) != 1:
  123. await utils.answer(message, self.strings("prefix_incorrect"))
  124. return
  125. if args == "s":
  126. await utils.answer(message, self.strings("prefix_incorrect"))
  127. return
  128. if (
  129. not is_dragon
  130. and args[0] == self._db.get("dragon.prefix", "command_prefix", ",")
  131. or is_dragon
  132. and args[0] == self._db.get(main.__name__, "command_prefix", ".")
  133. ):
  134. await utils.answer(message, self.strings("prefix_collision"))
  135. return
  136. oldprefix = (
  137. f"dragon {utils.escape_html(self.get_prefix('dragon'))}"
  138. if is_dragon
  139. else utils.escape_html(self.get_prefix())
  140. )
  141. self._db.set(
  142. "dragon.prefix" if is_dragon else main.__name__,
  143. "command_prefix",
  144. args,
  145. )
  146. await utils.answer(
  147. message,
  148. self.strings("prefix_set").format(
  149. (
  150. DRAGON_EMOJI
  151. if is_dragon
  152. else "<emoji document_id=5197474765387864959>👍</emoji>"
  153. ),
  154. newprefix=utils.escape_html(
  155. self.get_prefix() if is_dragon else args[0]
  156. ),
  157. oldprefix=utils.escape_html(oldprefix),
  158. ),
  159. )
  160. @loader.command()
  161. async def aliases(self, message: Message):
  162. await utils.answer(
  163. message,
  164. self.strings("aliases")
  165. + "\n".join(
  166. [
  167. f"▫️ <code>{i}</code> &lt;- {y}"
  168. for i, y in self.allmodules.aliases.items()
  169. ]
  170. ),
  171. )
  172. @loader.command()
  173. async def addalias(self, message: Message):
  174. if len(args := utils.get_args(message)) != 2:
  175. await utils.answer(message, self.strings("alias_args"))
  176. return
  177. alias, cmd = args
  178. if self.allmodules.add_alias(alias, cmd):
  179. self.set(
  180. "aliases",
  181. {
  182. **self.get("aliases", {}),
  183. alias: cmd,
  184. },
  185. )
  186. await utils.answer(
  187. message,
  188. self.strings("alias_created").format(utils.escape_html(alias)),
  189. )
  190. else:
  191. await utils.answer(
  192. message,
  193. self.strings("no_command").format(utils.escape_html(cmd)),
  194. )
  195. @loader.command()
  196. async def delalias(self, message: Message):
  197. args = utils.get_args(message)
  198. if len(args) != 1:
  199. await utils.answer(message, self.strings("delalias_args"))
  200. return
  201. alias = args[0]
  202. if not self.allmodules.remove_alias(alias):
  203. await utils.answer(
  204. message,
  205. self.strings("no_alias").format(utils.escape_html(alias)),
  206. )
  207. return
  208. current = self.get("aliases", {})
  209. del current[alias]
  210. self.set("aliases", current)
  211. await utils.answer(
  212. message,
  213. self.strings("alias_removed").format(utils.escape_html(alias)),
  214. )
  215. @loader.command()
  216. async def cleardb(self, message: Message):
  217. await self.inline.form(
  218. self.strings("confirm_cleardb"),
  219. message,
  220. reply_markup=[
  221. {
  222. "text": self.strings("cleardb_confirm"),
  223. "callback": self._inline__cleardb,
  224. },
  225. {
  226. "text": self.strings("cancel"),
  227. "action": "close",
  228. },
  229. ],
  230. )
  231. async def _inline__cleardb(self, call: InlineCall):
  232. self._db.clear()
  233. self._db.save()
  234. await utils.answer(call, self.strings("db_cleared"))