loader.py 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. """Registers modules"""
  2. # Friendly Telegram (telegram userbot)
  3. # Copyright (C) 2018-2021 The Authors
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  15. # █▀█ █ █ █ █▀█ █▀▄ █
  16. # © Copyright 2022
  17. # https://t.me/hikariatama
  18. #
  19. # 🔒 Licensed under the GNU AGPLv3
  20. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  21. import asyncio
  22. import contextlib
  23. import inspect
  24. import logging
  25. import os
  26. import re
  27. import sys
  28. import requests
  29. import copy
  30. import importlib
  31. import importlib.util
  32. import importlib.machinery
  33. from functools import partial, wraps
  34. from telethon.tl.types import Message, InputPeerNotifySettings, Channel
  35. from telethon.tl.functions.account import UpdateNotifySettingsRequest
  36. from telethon.hints import EntityLike
  37. from types import FunctionType
  38. from typing import Any, Optional, Union, List
  39. from . import security, utils, validators, version
  40. from .types import (
  41. ConfigValue, # skipcq
  42. LoadError,
  43. Module,
  44. Library,
  45. ModuleConfig, # skipcq
  46. LibraryConfig,
  47. SelfUnload,
  48. SelfSuspend,
  49. StopLoop,
  50. InlineMessage,
  51. CoreOverwriteError,
  52. CoreUnloadError,
  53. StringLoader,
  54. get_commands,
  55. get_inline_handlers,
  56. JSONSerializable,
  57. )
  58. from .inline.core import InlineManager
  59. from .inline.types import InlineCall
  60. from .translations import Strings, Translator
  61. from .database import Database
  62. import gc as _gc
  63. import types as _types
  64. logger = logging.getLogger(__name__)
  65. owner = security.owner
  66. sudo = security.sudo
  67. support = security.support
  68. group_owner = security.group_owner
  69. group_admin_add_admins = security.group_admin_add_admins
  70. group_admin_change_info = security.group_admin_change_info
  71. group_admin_ban_users = security.group_admin_ban_users
  72. group_admin_delete_messages = security.group_admin_delete_messages
  73. group_admin_pin_messages = security.group_admin_pin_messages
  74. group_admin_invite_users = security.group_admin_invite_users
  75. group_admin = security.group_admin
  76. group_member = security.group_member
  77. pm = security.pm
  78. unrestricted = security.unrestricted
  79. inline_everyone = security.inline_everyone
  80. def proxy0(data):
  81. def proxy1():
  82. return data
  83. return proxy1
  84. _CELLTYPE = type(proxy0(None).__closure__[0])
  85. def replace_all_refs(replace_from: Any, replace_to: Any) -> Any:
  86. """
  87. :summary: Uses the :mod:`gc` module to replace all references to obj
  88. :attr:`replace_from` with :attr:`replace_to` (it tries it's best,
  89. anyway).
  90. :param replace_from: The obj you want to replace.
  91. :param replace_to: The new objject you want in place of the old one.
  92. :returns: The replace_from
  93. """
  94. # https://github.com/cart0113/pyjack/blob/dd1f9b70b71f48335d72f53ee0264cf70dbf4e28/pyjack.py
  95. _gc.collect()
  96. hit = False
  97. for referrer in _gc.get_referrers(replace_from):
  98. # FRAMES -- PASS THEM UP
  99. if isinstance(referrer, _types.FrameType):
  100. continue
  101. # DICTS
  102. if isinstance(referrer, dict):
  103. cls = None
  104. # THIS CODE HERE IS TO DEAL WITH DICTPROXY TYPES
  105. if "__dict__" in referrer and "__weakref__" in referrer:
  106. for cls in _gc.get_referrers(referrer):
  107. if inspect.isclass(cls) and cls.__dict__ == referrer:
  108. break
  109. for key, value in referrer.items():
  110. # REMEMBER TO REPLACE VALUES ...
  111. if value is replace_from:
  112. hit = True
  113. value = replace_to
  114. referrer[key] = value
  115. if cls: # AGAIN, CLEANUP DICTPROXY PROBLEM
  116. setattr(cls, key, replace_to)
  117. # AND KEYS.
  118. if key is replace_from:
  119. hit = True
  120. del referrer[key]
  121. referrer[replace_to] = value
  122. elif isinstance(referrer, list):
  123. for i, value in enumerate(referrer):
  124. if value is replace_from:
  125. hit = True
  126. referrer[i] = replace_to
  127. elif isinstance(referrer, set):
  128. referrer.remove(replace_from)
  129. referrer.add(replace_to)
  130. hit = True
  131. elif isinstance(
  132. referrer,
  133. (
  134. tuple,
  135. frozenset,
  136. ),
  137. ):
  138. new_tuple = []
  139. for obj in referrer:
  140. if obj is replace_from:
  141. new_tuple.append(replace_to)
  142. else:
  143. new_tuple.append(obj)
  144. replace_all_refs(referrer, type(referrer)(new_tuple))
  145. elif isinstance(referrer, _CELLTYPE):
  146. def _proxy0(data):
  147. def proxy1():
  148. return data
  149. return proxy1
  150. proxy = _proxy0(replace_to)
  151. newcell = proxy.__closure__[0]
  152. replace_all_refs(referrer, newcell)
  153. elif isinstance(referrer, _types.FunctionType):
  154. localsmap = {}
  155. for key in ["code", "globals", "name", "defaults", "closure"]:
  156. orgattr = getattr(referrer, f"__{key}__")
  157. localsmap[key] = replace_to if orgattr is replace_from else orgattr
  158. localsmap["argdefs"] = localsmap["defaults"]
  159. del localsmap["defaults"]
  160. newfn = _types.FunctionType(**localsmap)
  161. replace_all_refs(referrer, newfn)
  162. else:
  163. logging.debug(f"{referrer} is not supported.")
  164. if hit is False:
  165. raise AttributeError(f"Object '{replace_from}' not found")
  166. return replace_from
  167. async def stop_placeholder() -> bool:
  168. return True
  169. class Placeholder:
  170. """Placeholder"""
  171. VALID_PIP_PACKAGES = re.compile(
  172. r"^\s*# ?requires:(?: ?)((?:{url} )*(?:{url}))\s*$".format(
  173. url=r"[-[\]_.~:/?#@!$&'()*+,;%<=>a-zA-Z0-9]+"
  174. ),
  175. re.MULTILINE,
  176. )
  177. USER_INSTALL = "PIP_TARGET" not in os.environ and "VIRTUAL_ENV" not in os.environ
  178. class InfiniteLoop:
  179. _task = None
  180. status = False
  181. module_instance = None # Will be passed later
  182. def __init__(
  183. self,
  184. func: FunctionType,
  185. interval: int,
  186. autostart: bool,
  187. wait_before: bool,
  188. stop_clause: Union[str, None],
  189. ):
  190. self.func = func
  191. self.interval = interval
  192. self._wait_before = wait_before
  193. self._stop_clause = stop_clause
  194. self.autostart = autostart
  195. def _stop(self, *args, **kwargs):
  196. self._wait_for_stop.set()
  197. def stop(self, *args, **kwargs):
  198. with contextlib.suppress(AttributeError):
  199. _hikka_client_id_logging_tag = copy.copy(
  200. self.module_instance.allmodules.client.tg_id
  201. )
  202. if self._task:
  203. logger.debug(f"Stopped loop for {self.func}")
  204. self._wait_for_stop = asyncio.Event()
  205. self.status = False
  206. self._task.add_done_callback(self._stop)
  207. self._task.cancel()
  208. return asyncio.ensure_future(self._wait_for_stop.wait())
  209. logger.debug("Loop is not running")
  210. return asyncio.ensure_future(stop_placeholder())
  211. def start(self, *args, **kwargs):
  212. with contextlib.suppress(AttributeError):
  213. _hikka_client_id_logging_tag = copy.copy(
  214. self.module_instance.allmodules.client.tg_id
  215. )
  216. if not self._task:
  217. logger.debug(f"Started loop for {self.func}")
  218. self._task = asyncio.ensure_future(self.actual_loop(*args, **kwargs))
  219. else:
  220. logger.debug("Attempted to start already running loop")
  221. async def actual_loop(self, *args, **kwargs):
  222. # Wait for loader to set attribute
  223. while not self.module_instance:
  224. await asyncio.sleep(0.01)
  225. if isinstance(self._stop_clause, str) and self._stop_clause:
  226. self.module_instance.set(self._stop_clause, True)
  227. self.status = True
  228. while self.status:
  229. if self._wait_before:
  230. await asyncio.sleep(self.interval)
  231. if (
  232. isinstance(self._stop_clause, str)
  233. and self._stop_clause
  234. and not self.module_instance.get(self._stop_clause, False)
  235. ):
  236. break
  237. try:
  238. await self.func(self.module_instance, *args, **kwargs)
  239. except StopLoop:
  240. break
  241. except Exception:
  242. logger.exception("Error running loop!")
  243. if not self._wait_before:
  244. await asyncio.sleep(self.interval)
  245. self._wait_for_stop.set()
  246. self.status = False
  247. def __del__(self):
  248. self.stop()
  249. def loop(
  250. interval: int = 5,
  251. autostart: Optional[bool] = False,
  252. wait_before: Optional[bool] = False,
  253. stop_clause: Optional[str] = None,
  254. ) -> FunctionType:
  255. """
  256. Create new infinite loop from class method
  257. :param interval: Loop iterations delay
  258. :param autostart: Start loop once module is loaded
  259. :param wait_before: Insert delay before actual iteration, rather than after
  260. :param stop_clause: Database key, based on which the loop will run.
  261. This key will be set to `True` once loop is started,
  262. and will stop after key resets to `False`
  263. :attr status: Boolean, describing whether the loop is running
  264. """
  265. def wrapped(func):
  266. return InfiniteLoop(func, interval, autostart, wait_before, stop_clause)
  267. return wrapped
  268. MODULES_NAME = "modules"
  269. ru_keys = 'ёйцукенгшщзхъфывапролджэячсмитьбю.Ё"№;%:?ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/ЯЧСМИТЬБЮ,'
  270. en_keys = "`qwertyuiop[]asdfghjkl;'zxcvbnm,./~@#$%^&QWERTYUIOP{}ASDFGHJKL:\"|ZXCVBNM<>?"
  271. BASE_DIR = (
  272. os.path.normpath(os.path.join(utils.get_base_dir(), ".."))
  273. if "OKTETO" not in os.environ and "DOCKER" not in os.environ
  274. else "/data"
  275. )
  276. LOADED_MODULES_DIR = os.path.join(BASE_DIR, "loaded_modules")
  277. if not os.path.isdir(LOADED_MODULES_DIR) and "DYNO" not in os.environ:
  278. os.mkdir(LOADED_MODULES_DIR, mode=0o755)
  279. def translatable_docstring(cls):
  280. """Decorator that makes triple-quote docstrings translatable"""
  281. @wraps(cls.config_complete)
  282. def config_complete(self, *args, **kwargs):
  283. def proccess_decorators(mark: str, obj: str):
  284. nonlocal self
  285. for attr in dir(func_):
  286. if (
  287. attr.endswith("_doc")
  288. and len(attr) == 6
  289. and isinstance(getattr(func_, attr), str)
  290. ):
  291. var = f"strings_{attr.split('_')[0]}"
  292. if not hasattr(self, var):
  293. setattr(self, var, {})
  294. getattr(self, var).setdefault(f"{mark}{obj}", getattr(func_, attr))
  295. for command_, func_ in get_commands(cls).items():
  296. proccess_decorators("_cmd_doc_", command_)
  297. try:
  298. func_.__doc__ = self.strings[f"_cmd_doc_{command_}"]
  299. except AttributeError:
  300. func_.__func__.__doc__ = self.strings[f"_cmd_doc_{command_}"]
  301. for inline_handler_, func_ in get_inline_handlers(cls).items():
  302. proccess_decorators("_ihandle_doc_", inline_handler_)
  303. try:
  304. func_.__doc__ = self.strings[f"_ihandle_doc_{inline_handler_}"]
  305. except AttributeError:
  306. func_.__func__.__doc__ = self.strings[f"_ihandle_doc_{inline_handler_}"]
  307. self.__doc__ = self.strings["_cls_doc"]
  308. return self.config_complete._old_(self, *args, **kwargs)
  309. config_complete._old_ = cls.config_complete
  310. cls.config_complete = config_complete
  311. for command_, func in get_commands(cls).items():
  312. cls.strings[f"_cmd_doc_{command_}"] = inspect.getdoc(func)
  313. for inline_handler_, func in get_inline_handlers(cls).items():
  314. cls.strings[f"_ihandle_doc_{inline_handler_}"] = inspect.getdoc(func)
  315. cls.strings["_cls_doc"] = inspect.getdoc(cls)
  316. return cls
  317. tds = translatable_docstring # Shorter name for modules to use
  318. def ratelimit(func: callable):
  319. """Decorator that causes ratelimiting for this command to be enforced more strictly"""
  320. func.ratelimit = True
  321. return func
  322. def tag(*tags, **kwarg_tags):
  323. """
  324. Tag function (esp. watchers) with some tags
  325. Currently available tags:
  326. • `no_commands` - Ignore all userbot commands in watcher
  327. • `only_commands` - Capture only userbot commands in watcher
  328. • `out` - Capture only outgoing events
  329. • `in` - Capture only incoming events
  330. • `only_messages` - Capture only messages (not join events)
  331. • `editable` - Capture only messages, which can be edited (no forwards etc.)
  332. • `no_media` - Capture only messages without media and files
  333. • `only_media` - Capture only messages with media and files
  334. • `only_photos` - Capture only messages with photos
  335. • `only_videos` - Capture only messages with videos
  336. • `only_audios` - Capture only messages with audios
  337. • `only_docs` - Capture only messages with documents
  338. • `only_stickers` - Capture only messages with stickers
  339. • `only_inline` - Capture only messages with inline queries
  340. • `only_channels` - Capture only messages with channels
  341. • `only_groups` - Capture only messages with groups
  342. • `only_pm` - Capture only messages with private chats
  343. • `startswith` - Capture only messages that start with given text
  344. • `endswith` - Capture only messages that end with given text
  345. • `contains` - Capture only messages that contain given text
  346. • `regex` - Capture only messages that match given regex
  347. • `filter` - Capture only messages that pass given function
  348. • `from_id` - Capture only messages from given user
  349. • `chat_id` - Capture only messages from given chat
  350. Usage example:
  351. @loader.tag("no_commands", "out")
  352. @loader.tag("no_commands", out=True)
  353. @loader.tag(only_messages=True)
  354. @loader.tag("only_messages", "only_pm", regex=r"^[.] ?hikka$", from_id=659800858)
  355. 💡 These tags can be used directly in `@loader.watcher`:
  356. @loader.watcher("no_commands", out=True)
  357. """
  358. def inner(func: callable):
  359. for _tag in tags:
  360. setattr(func, _tag, True)
  361. for _tag, value in kwarg_tags.items():
  362. setattr(func, _tag, value)
  363. return func
  364. return inner
  365. def _mark_method(mark: str, *args, **kwargs) -> callable:
  366. """
  367. Mark method as a method of a class
  368. """
  369. def decorator(func: callable) -> callable:
  370. setattr(func, mark, True)
  371. for arg in args:
  372. setattr(func, arg, True)
  373. for kwarg, value in kwargs.items():
  374. setattr(func, kwarg, value)
  375. return func
  376. return decorator
  377. def command(*args, **kwargs):
  378. """
  379. Decorator that marks function as userbot command
  380. """
  381. return _mark_method("is_command", *args, **kwargs)
  382. def inline_handler(*args, **kwargs):
  383. """
  384. Decorator that marks function as inline handler
  385. """
  386. return _mark_method("is_inline_handler", *args, **kwargs)
  387. def watcher(*args, **kwargs):
  388. """
  389. Decorator that marks function as watcher
  390. """
  391. return _mark_method("is_watcher", *args, **kwargs)
  392. def callback_handler(*args, **kwargs):
  393. """
  394. Decorator that marks function as callback handler
  395. """
  396. return _mark_method("is_callback_handler", *args, **kwargs)
  397. class Modules:
  398. """Stores all registered modules"""
  399. def __init__(
  400. self,
  401. client: "CustomTelegramClient", # type: ignore
  402. db: Database,
  403. allclients: list,
  404. translator: Translator,
  405. ):
  406. self._initial_registration = True
  407. self.commands = {}
  408. self.inline_handlers = {}
  409. self.callback_handlers = {}
  410. self.aliases = {}
  411. self.modules = [] # skipcq: PTC-W0052
  412. self.libraries = []
  413. self.watchers = []
  414. self._log_handlers = []
  415. self._core_commands = []
  416. self.__approve = []
  417. self.allclients = allclients
  418. self.client = client
  419. self._db = db
  420. self._translator = translator
  421. self.secure_boot = False
  422. asyncio.ensure_future(self._junk_collector())
  423. async def _junk_collector(self):
  424. """
  425. Periodically reloads commands, inline handlers, callback handlers and watchers from loaded
  426. modules to prevent zombie handlers
  427. """
  428. while True:
  429. await asyncio.sleep(30)
  430. commands = {}
  431. inline_handlers = {}
  432. callback_handlers = {}
  433. watchers = []
  434. for module in self.modules:
  435. commands.update(module.hikka_commands)
  436. inline_handlers.update(module.hikka_inline_handlers)
  437. callback_handlers.update(module.hikka_callback_handlers)
  438. watchers.extend(module.hikka_watchers.values())
  439. self.commands = commands
  440. self.inline_handlers = inline_handlers
  441. self.callback_handlers = callback_handlers
  442. self.watchers = watchers
  443. logger.debug(
  444. f"Reloaded {len(self.commands)} commands,"
  445. f" {len(self.inline_handlers)} inline handlers,"
  446. f" {len(self.callback_handlers)} callback handlers and"
  447. f" {len(self.watchers)} watchers"
  448. )
  449. async def register_all(self, mods: list = None):
  450. """Load all modules in the module directory"""
  451. external_mods = []
  452. if not mods:
  453. mods = [
  454. os.path.join(utils.get_base_dir(), MODULES_NAME, mod)
  455. for mod in filter(
  456. lambda x: (x.endswith(".py") and not x.startswith("_")),
  457. os.listdir(os.path.join(utils.get_base_dir(), MODULES_NAME)),
  458. )
  459. ]
  460. self.secure_boot = self._db.get(__name__, "secure_boot", False)
  461. if "DYNO" not in os.environ and not self.secure_boot:
  462. external_mods = [
  463. os.path.join(LOADED_MODULES_DIR, mod)
  464. for mod in filter(
  465. lambda x: (
  466. x.endswith(f"{self.client.tg_id}.py")
  467. and not x.startswith("_")
  468. ),
  469. os.listdir(LOADED_MODULES_DIR),
  470. )
  471. ]
  472. else:
  473. external_mods = []
  474. await self._register_modules(mods)
  475. await self._register_modules(external_mods, "<file>")
  476. async def _register_modules(self, modules: list, origin: str = "<core>"):
  477. with contextlib.suppress(AttributeError):
  478. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  479. for mod in modules:
  480. try:
  481. mod_shortname = (
  482. os.path.basename(mod)
  483. .rsplit(".py", maxsplit=1)[0]
  484. .rsplit("_", maxsplit=1)[0]
  485. )
  486. module_name = f"{__package__}.{MODULES_NAME}.{mod_shortname}"
  487. user_friendly_origin = (
  488. "<core {}>" if origin == "<core>" else "<file {}>"
  489. ).format(mod_shortname)
  490. logger.debug(f"Loading {module_name} from filesystem")
  491. with open(mod, "r") as file:
  492. spec = importlib.machinery.ModuleSpec(
  493. module_name,
  494. StringLoader(file.read(), user_friendly_origin),
  495. origin=user_friendly_origin,
  496. )
  497. await self.register_module(spec, module_name, origin)
  498. except BaseException as e:
  499. logger.exception(f"Failed to load module {mod} due to {e}:")
  500. async def register_module(
  501. self,
  502. spec: importlib.machinery.ModuleSpec,
  503. module_name: str,
  504. origin: str = "<core>",
  505. save_fs: bool = False,
  506. ) -> Module:
  507. """Register single module from importlib spec"""
  508. with contextlib.suppress(AttributeError):
  509. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  510. module = importlib.util.module_from_spec(spec)
  511. sys.modules[module_name] = module
  512. spec.loader.exec_module(module)
  513. ret = None
  514. ret = next(
  515. (
  516. value()
  517. for value in vars(module).values()
  518. if inspect.isclass(value) and issubclass(value, Module)
  519. ),
  520. None,
  521. )
  522. if hasattr(module, "__version__"):
  523. ret.__version__ = module.__version__
  524. if ret is None:
  525. ret = module.register(module_name)
  526. if not isinstance(ret, Module):
  527. raise TypeError(f"Instance is not a Module, it is {type(ret)}")
  528. await self.complete_registration(ret)
  529. ret.__origin__ = origin
  530. cls_name = ret.__class__.__name__
  531. if save_fs and "DYNO" not in os.environ:
  532. path = os.path.join(
  533. LOADED_MODULES_DIR,
  534. f"{cls_name}_{self.client.tg_id}.py",
  535. )
  536. if origin == "<string>":
  537. with open(path, "w") as f:
  538. f.write(spec.loader.data.decode("utf-8"))
  539. logger.debug(f"Saved {cls_name=} to {path=}")
  540. return ret
  541. def add_aliases(self, aliases: dict):
  542. """Saves aliases and applies them to <core>/<file> modules"""
  543. self.aliases.update(aliases)
  544. for alias, cmd in aliases.items():
  545. self.add_alias(alias, cmd)
  546. def register_commands(self, instance: Module):
  547. """Register commands from instance"""
  548. with contextlib.suppress(AttributeError):
  549. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  550. if getattr(instance, "__origin__", "") == "<core>":
  551. self._core_commands += list(
  552. map(lambda x: x.lower(), list(instance.hikka_commands))
  553. )
  554. for name, cmd in self.commands.copy().items():
  555. if cmd.__self__.__class__.__name__ == instance.__class__.__name__:
  556. logger.debug(f"Removing command {name} for update")
  557. del self.commands[name]
  558. for _command, cmd in instance.hikka_commands.items():
  559. # Restrict overwriting core modules' commands
  560. if (
  561. _command.lower() in self._core_commands
  562. and getattr(instance, "__origin__", "") != "<core>"
  563. ):
  564. with contextlib.suppress(Exception):
  565. self.modules.remove(instance)
  566. raise CoreOverwriteError(command=_command)
  567. self.commands.update({_command.lower(): cmd})
  568. for alias, cmd in self.aliases.copy().items():
  569. if cmd in instance.hikka_commands:
  570. self.add_alias(alias, cmd)
  571. for name, func in instance.hikka_inline_handlers.copy().items():
  572. if name.lower() in self.inline_handlers:
  573. if (
  574. hasattr(func, "__self__")
  575. and hasattr(self.inline_handlers[name], "__self__")
  576. and func.__self__.__class__.__name__
  577. != self.inline_handlers[name].__self__.__class__.__name__
  578. ):
  579. logger.debug(f"Duplicate inline_handler {name}")
  580. logger.debug(
  581. f"Replacing inline_handler for {self.inline_handlers[name]}"
  582. )
  583. if not func.__doc__:
  584. logger.debug(f"Missing docs for {name}")
  585. self.inline_handlers.update({name.lower(): func})
  586. for name, func in instance.hikka_callback_handlers.copy().items():
  587. if name.lower() in self.callback_handlers and (
  588. hasattr(func, "__self__")
  589. and hasattr(self.callback_handlers[name], "__self__")
  590. and func.__self__.__class__.__name__
  591. != self.callback_handlers[name].__self__.__class__.__name__
  592. ):
  593. logger.debug(f"Duplicate callback_handler {name}")
  594. self.callback_handlers.update({name.lower(): func})
  595. def register_watcher(self, instance: Module):
  596. """Register watcher from instance"""
  597. with contextlib.suppress(AttributeError):
  598. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  599. for _watcher in self.watchers:
  600. if _watcher.__self__.__class__.__name__ == instance.__class__.__name__:
  601. logger.debug(f"Removing watcher {_watcher} for update")
  602. self.watchers.remove(_watcher)
  603. for _watcher in instance.hikka_watchers.values():
  604. self.watchers += [_watcher]
  605. def _lookup(self, modname: str):
  606. return next(
  607. (lib for lib in self.libraries if lib.name.lower() == modname.lower()),
  608. False,
  609. ) or next(
  610. (
  611. mod
  612. for mod in self.modules
  613. if mod.__class__.__name__.lower() == modname.lower()
  614. or mod.name.lower() == modname.lower()
  615. ),
  616. False,
  617. )
  618. @property
  619. def get_approved_channel(self):
  620. return self.__approve.pop(0) if self.__approve else None
  621. async def _approve(
  622. self,
  623. call: InlineCall,
  624. channel: EntityLike,
  625. event: asyncio.Event,
  626. ):
  627. local_event = asyncio.Event()
  628. self.__approve += [(channel, local_event)]
  629. await local_event.wait()
  630. event.status = local_event.status
  631. event.set()
  632. await call.edit(
  633. "💫 <b>Joined <a"
  634. f' href="https://t.me/{channel.username}">{utils.escape_html(channel.title)}</a></b>',
  635. gif="https://static.hikari.gay/0d32cbaa959e755ac8eef610f01ba0bd.gif",
  636. )
  637. async def _decline(
  638. self,
  639. call: InlineCall,
  640. channel: EntityLike,
  641. event: asyncio.Event,
  642. ):
  643. self._db.set(
  644. "hikka.main",
  645. "declined_joins",
  646. list(set(self._db.get("hikka.main", "declined_joins", []) + [channel.id])),
  647. )
  648. event.status = False
  649. event.set()
  650. await call.edit(
  651. "✖️ <b>Declined joining <a"
  652. f' href="https://t.me/{channel.username}">{utils.escape_html(channel.title)}</a></b>',
  653. gif="https://static.hikari.gay/0d32cbaa959e755ac8eef610f01ba0bd.gif",
  654. )
  655. async def _request_join(
  656. self,
  657. peer: EntityLike,
  658. reason: str,
  659. assure_joined: Optional[bool] = False,
  660. _module: Module = None,
  661. ) -> bool:
  662. """
  663. Request to join a channel.
  664. :param peer: The channel to join.
  665. :param reason: The reason for joining.
  666. :param assure_joined: If set, module will not be loaded unless the required channel is joined.
  667. ⚠️ Works only in `client_ready`!
  668. ⚠️ If user declines to join channel, he will not be asked to
  669. join again, so unless he joins it manually, module will not be loaded
  670. ever.
  671. :return: Status of the request.
  672. :rtype: bool
  673. :notice: This method will block module loading until the request is approved or declined.
  674. """
  675. event = asyncio.Event()
  676. await self.client(
  677. UpdateNotifySettingsRequest(
  678. peer=self.inline.bot_username,
  679. settings=InputPeerNotifySettings(show_previews=False, silent=False),
  680. )
  681. )
  682. channel = await self.client.get_entity(peer)
  683. if channel.id in self._db.get("hikka.main", "declined_joins", []):
  684. if assure_joined:
  685. raise LoadError(
  686. f"You need to join @{channel.username} in order to use this module"
  687. )
  688. return False
  689. if not isinstance(channel, Channel):
  690. raise TypeError("`peer` field must be a channel")
  691. if getattr(channel, "left", True):
  692. channel = await self.client.force_get_entity(peer)
  693. if not getattr(channel, "left", True):
  694. return True
  695. _module.strings._base_strings["_hikka_internal_request_join"] = (
  696. f"💫 <b>Module </b><code>{_module.__class__.__name__}</code><b> requested to"
  697. " join channel <a"
  698. f" href='https://t.me/{channel.username}'>{utils.escape_html(channel.title)}</a></b>\n\n<b>❓"
  699. f" Reason: </b><i>{utils.escape_html(reason)}</i>"
  700. )
  701. if not hasattr(_module, "strings_ru"):
  702. _module.strings_ru = {}
  703. _module.strings_ru["_hikka_internal_request_join"] = (
  704. f"💫 <b>Модуль </b><code>{_module.__class__.__name__}</code><b> запросил"
  705. " разрешение на вступление в канал <a"
  706. f" href='https://t.me/{channel.username}'>{utils.escape_html(channel.title)}</a></b>\n\n<b>❓"
  707. f" Причина: </b><i>{utils.escape_html(reason)}</i>"
  708. )
  709. await self.inline.bot.send_animation(
  710. self.client.tg_id,
  711. "https://static.hikari.gay/ab3adf144c94a0883bfe489f4eebc520.gif",
  712. caption=_module.strings("_hikka_internal_request_join"),
  713. reply_markup=self.inline.generate_markup(
  714. [
  715. {
  716. "text": "💫 Approve",
  717. "callback": self._approve,
  718. "args": (channel, event),
  719. },
  720. {
  721. "text": "✖️ Decline",
  722. "callback": self._decline,
  723. "args": (channel, event),
  724. },
  725. ]
  726. ),
  727. )
  728. _module.hikka_wait_channel_approve = (
  729. _module.__class__.__name__,
  730. channel,
  731. reason,
  732. )
  733. await event.wait()
  734. with contextlib.suppress(AttributeError):
  735. delattr(_module, "hikka_wait_channel_approve")
  736. if assure_joined and not event.status:
  737. raise LoadError(
  738. f"You need to join @{channel.username} in order to use this module"
  739. )
  740. return event.status
  741. async def complete_registration(self, instance: Module):
  742. """Complete registration of instance"""
  743. with contextlib.suppress(AttributeError):
  744. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  745. instance.allclients = self.allclients
  746. instance.allmodules = self
  747. instance.hikka = True
  748. instance.get = partial(self._get, _owner=instance.__class__.__name__)
  749. instance.set = partial(self._set, _owner=instance.__class__.__name__)
  750. instance.pointer = partial(self._pointer, _owner=instance.__class__.__name__)
  751. instance.get_prefix = partial(self._db.get, "hikka.main", "command_prefix", ".")
  752. instance.client = self.client
  753. instance._client = self.client
  754. instance.db = self._db
  755. instance._db = self._db
  756. instance.lookup = self._lookup
  757. instance.import_lib = self._mod_import_lib
  758. instance.tg_id = self.client.tg_id
  759. instance._tg_id = self.client.tg_id
  760. instance.request_join = partial(self._request_join, _module=instance)
  761. instance.animate = self._animate
  762. for module in self.modules:
  763. if module.__class__.__name__ == instance.__class__.__name__:
  764. if getattr(module, "__origin__", "") == "<core>":
  765. raise CoreOverwriteError(
  766. module=module.__class__.__name__[:-3]
  767. if module.__class__.__name__.endswith("Mod")
  768. else module.__class__.__name__
  769. )
  770. logger.debug(f"Removing module for update {module}")
  771. await module.on_unload()
  772. self.modules.remove(module)
  773. for method in dir(module):
  774. if isinstance(getattr(module, method), InfiniteLoop):
  775. getattr(module, method).stop()
  776. logger.debug(f"Stopped loop in {module=}, {method=}")
  777. self.modules += [instance]
  778. def _get(
  779. self,
  780. key: str,
  781. default: Optional[JSONSerializable] = None,
  782. _owner: str = None,
  783. ) -> JSONSerializable:
  784. return self._db.get(_owner, key, default)
  785. def _set(self, key: str, value: JSONSerializable, _owner: str = None) -> bool:
  786. return self._db.set(_owner, key, value)
  787. def _pointer(
  788. self,
  789. key: str,
  790. default: Optional[JSONSerializable] = None,
  791. _owner: str = None,
  792. ) -> JSONSerializable:
  793. return self._db.pointer(_owner, key, default)
  794. async def _mod_import_lib(
  795. self,
  796. url: str,
  797. *,
  798. suspend_on_error: Optional[bool] = False,
  799. _did_requirements: bool = False,
  800. ) -> object:
  801. """
  802. Import library from url and register it in :obj:`Modules`
  803. :param url: Url to import
  804. :param suspend_on_error: Will raise :obj:`loader.SelfSuspend` if library can't be loaded
  805. :return: :obj:`Library`
  806. :raise: SelfUnload if :attr:`suspend_on_error` is True and error occurred
  807. :raise: HTTPError if library is not found
  808. :raise: ImportError if library doesn't have any class which is a subclass of :obj:`loader.Library`
  809. :raise: ImportError if library name doesn't end with `Lib`
  810. :raise: RuntimeError if library throws in :method:`init`
  811. :raise: RuntimeError if library classname exists in :obj:`Modules`.libraries
  812. """
  813. def _raise(e: Exception):
  814. if suspend_on_error:
  815. raise SelfSuspend("Required library is not available or is corrupted.")
  816. raise e
  817. if not utils.check_url(url):
  818. _raise(ValueError("Invalid url for library"))
  819. code = await utils.run_sync(requests.get, url)
  820. code.raise_for_status()
  821. code = code.text
  822. if re.search(r"# ?scope: ?hikka_min", code):
  823. ver = tuple(
  824. map(
  825. int,
  826. re.search(r"# ?scope: ?hikka_min ((\d+\.){2}\d+)", code)[1].split(
  827. "."
  828. ),
  829. )
  830. )
  831. if version.__version__ < ver:
  832. _raise(
  833. RuntimeError(
  834. f"Library requires Hikka version {'{}.{}.{}'.format(*ver)}+"
  835. )
  836. )
  837. module = f"hikka.libraries.{url.replace('%', '%%').replace('.', '%d')}"
  838. origin = f"<library {url}>"
  839. spec = importlib.machinery.ModuleSpec(
  840. module, StringLoader(code, origin), origin=origin
  841. )
  842. try:
  843. instance = importlib.util.module_from_spec(spec)
  844. sys.modules[module] = instance
  845. spec.loader.exec_module(instance)
  846. except ImportError as e:
  847. logger.info(
  848. f"Library loading failed, attemping dependency installation ({e.name})"
  849. )
  850. # Let's try to reinstall dependencies
  851. try:
  852. requirements = list(
  853. filter(
  854. lambda x: not x.startswith(("-", "_", ".")),
  855. map(
  856. str.strip,
  857. VALID_PIP_PACKAGES.search(code)[1].split(),
  858. ),
  859. )
  860. )
  861. except TypeError:
  862. logger.warning(
  863. "No valid pip packages specified in code, attemping"
  864. " installation from error"
  865. )
  866. requirements = [e.name]
  867. logger.debug(f"Installing requirements: {requirements}")
  868. if not requirements or _did_requirements:
  869. _raise(e)
  870. pip = await asyncio.create_subprocess_exec(
  871. sys.executable,
  872. "-m",
  873. "pip",
  874. "install",
  875. "--upgrade",
  876. "-q",
  877. "--disable-pip-version-check",
  878. "--no-warn-script-location",
  879. *["--user"] if USER_INSTALL else [],
  880. *requirements,
  881. )
  882. rc = await pip.wait()
  883. if rc != 0:
  884. _raise(e)
  885. importlib.invalidate_caches()
  886. kwargs = utils.get_kwargs()
  887. kwargs["_did_requirements"] = True
  888. return await self._mod_import_lib(**kwargs) # Try again
  889. lib_obj = next(
  890. (
  891. value()
  892. for value in vars(instance).values()
  893. if inspect.isclass(value) and issubclass(value, Library)
  894. ),
  895. None,
  896. )
  897. if not lib_obj:
  898. _raise(ImportError("Invalid library. No class found"))
  899. if not lib_obj.__class__.__name__.endswith("Lib"):
  900. _raise(ImportError("Invalid library. Class name must end with 'Lib'"))
  901. if (
  902. all(
  903. line.replace(" ", "") != "#scope:no_stats" for line in code.splitlines()
  904. )
  905. and self._db.get("hikka.main", "stats", True)
  906. and url is not None
  907. and utils.check_url(url)
  908. ):
  909. with contextlib.suppress(Exception):
  910. await self._lookup("loader")._send_stats(url)
  911. lib_obj.client = self.client
  912. lib_obj._client = self.client # skipcq
  913. lib_obj.db = self._db
  914. lib_obj._db = self._db # skipcq
  915. lib_obj.name = lib_obj.__class__.__name__
  916. lib_obj.source_url = url.strip("/")
  917. lib_obj.lookup = self._lookup
  918. lib_obj.inline = self.inline
  919. lib_obj.tg_id = self.client.tg_id
  920. lib_obj.allmodules = self
  921. lib_obj._lib_get = partial(
  922. self._get, _owner=lib_obj.__class__.__name__
  923. ) # skipcq
  924. lib_obj._lib_set = partial(
  925. self._set, _owner=lib_obj.__class__.__name__
  926. ) # skipcq
  927. lib_obj._lib_pointer = partial(
  928. self._pointer, _owner=lib_obj.__class__.__name__
  929. ) # skipcq
  930. lib_obj.get_prefix = partial(self._db.get, "hikka.main", "command_prefix", ".")
  931. for old_lib in self.libraries:
  932. if old_lib.name == lib_obj.name and (
  933. not isinstance(getattr(old_lib, "version", None), tuple)
  934. and not isinstance(getattr(lib_obj, "version", None), tuple)
  935. or old_lib.version >= lib_obj.version
  936. ):
  937. logging.debug(f"Using existing instance of library {old_lib.name}")
  938. return old_lib
  939. new = True
  940. for old_lib in self.libraries:
  941. if old_lib.name == lib_obj.name:
  942. if hasattr(old_lib, "on_lib_update") and callable(
  943. old_lib.on_lib_update
  944. ):
  945. await old_lib.on_lib_update(lib_obj)
  946. replace_all_refs(old_lib, lib_obj)
  947. new = False
  948. logging.debug(
  949. "Replacing existing instance of library"
  950. f" {lib_obj.name} with updated object"
  951. )
  952. if hasattr(lib_obj, "init") and callable(lib_obj.init):
  953. try:
  954. await lib_obj.init()
  955. except Exception:
  956. _raise(RuntimeError("Library init() failed"))
  957. if hasattr(lib_obj, "config"):
  958. if not isinstance(lib_obj.config, LibraryConfig):
  959. _raise(
  960. RuntimeError("Library config must be a `LibraryConfig` instance")
  961. )
  962. libcfg = lib_obj.db.get(
  963. lib_obj.__class__.__name__,
  964. "__config__",
  965. {},
  966. )
  967. for conf in lib_obj.config:
  968. with contextlib.suppress(Exception):
  969. lib_obj.config.set_no_raise(
  970. conf,
  971. (
  972. libcfg[conf]
  973. if conf in libcfg
  974. else os.environ.get(f"{lib_obj.__class__.__name__}.{conf}")
  975. or lib_obj.config.getdef(conf)
  976. ),
  977. )
  978. if hasattr(lib_obj, "strings"):
  979. lib_obj.strings = Strings(lib_obj, self._translator)
  980. lib_obj.translator = self._translator
  981. if new:
  982. self.libraries += [lib_obj]
  983. return lib_obj
  984. def dispatch(self, _command: str) -> tuple:
  985. """Dispatch command to appropriate module"""
  986. return next(
  987. (
  988. (cmd, self.commands[cmd.lower()])
  989. for cmd in [_command, self.aliases.get(_command.lower())]
  990. if cmd and cmd.lower() in self.commands
  991. ),
  992. (_command, None),
  993. )
  994. def send_config(self, skip_hook: bool = False):
  995. """Configure modules"""
  996. for mod in self.modules:
  997. self.send_config_one(mod, skip_hook)
  998. def send_config_one(self, mod: "Module", skip_hook: bool = False):
  999. """Send config to single instance"""
  1000. with contextlib.suppress(AttributeError):
  1001. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  1002. if hasattr(mod, "config"):
  1003. modcfg = self._db.get(
  1004. mod.__class__.__name__,
  1005. "__config__",
  1006. {},
  1007. )
  1008. try:
  1009. for conf in mod.config:
  1010. with contextlib.suppress(validators.ValidationError):
  1011. mod.config.set_no_raise(
  1012. conf,
  1013. (
  1014. modcfg[conf]
  1015. if conf in modcfg
  1016. else os.environ.get(f"{mod.__class__.__name__}.{conf}")
  1017. or mod.config.getdef(conf)
  1018. ),
  1019. )
  1020. except AttributeError:
  1021. logger.warning(
  1022. "Got invalid config instance. Expected `ModuleConfig`, got"
  1023. f" {type(mod.config)=}, {mod.config=}"
  1024. )
  1025. if skip_hook:
  1026. return
  1027. if not hasattr(mod, "name"):
  1028. mod.name = mod.strings["name"]
  1029. if hasattr(mod, "strings"):
  1030. mod.strings = Strings(mod, self._translator)
  1031. mod.translator = self._translator
  1032. try:
  1033. mod.config_complete()
  1034. except Exception as e:
  1035. logger.exception(f"Failed to send mod config complete signal due to {e}")
  1036. raise
  1037. async def send_ready(self):
  1038. """Send all data to all modules"""
  1039. # Init inline manager anyway, so the modules
  1040. # can access its `init_complete`
  1041. inline_manager = InlineManager(self.client, self._db, self)
  1042. await inline_manager._register_manager()
  1043. # We save it to `Modules` attribute, so not to re-init
  1044. # it everytime module is loaded. Then we can just
  1045. # re-assign it to all modules
  1046. self.inline = inline_manager
  1047. try:
  1048. await asyncio.gather(*[self.send_ready_one(mod) for mod in self.modules])
  1049. except Exception as e:
  1050. logger.exception(f"Failed to send mod init complete signal due to {e}")
  1051. async def _animate(
  1052. self,
  1053. message: Union[Message, InlineMessage],
  1054. frames: List[str],
  1055. interval: Union[float, int],
  1056. *,
  1057. inline: bool = False,
  1058. ) -> None:
  1059. """
  1060. Animate message
  1061. :param message: Message to animate
  1062. :param frames: A List of strings which are the frames of animation
  1063. :param interval: Animation delay
  1064. :param inline: Whether to use inline bot for animation
  1065. :returns message:
  1066. Please, note that if you set `inline=True`, first frame will be shown with an empty
  1067. button due to the limitations of Telegram API
  1068. """
  1069. with contextlib.suppress(AttributeError):
  1070. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  1071. if interval < 0.1:
  1072. logger.warning(
  1073. "Resetting animation interval to 0.1s, because it may get you in"
  1074. " floodwaits bro"
  1075. )
  1076. interval = 0.1
  1077. for frame in frames:
  1078. if isinstance(message, Message):
  1079. if inline:
  1080. message = await self.inline.form(
  1081. message=message,
  1082. text=frame,
  1083. reply_markup={"text": "\u0020\u2800", "data": "empty"},
  1084. )
  1085. else:
  1086. message = await utils.answer(message, frame)
  1087. elif isinstance(message, InlineMessage) and inline:
  1088. await message.edit(frame)
  1089. await asyncio.sleep(interval)
  1090. return message
  1091. async def send_ready_one(
  1092. self,
  1093. mod: Module,
  1094. no_self_unload: bool = False,
  1095. from_dlmod: bool = False,
  1096. ):
  1097. with contextlib.suppress(AttributeError):
  1098. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  1099. mod.inline = self.inline
  1100. for method in dir(mod):
  1101. if isinstance(getattr(mod, method), InfiniteLoop):
  1102. setattr(getattr(mod, method), "module_instance", mod)
  1103. if getattr(mod, method).autostart:
  1104. getattr(mod, method).start()
  1105. logger.debug(f"Added {mod=} to {method=}")
  1106. if from_dlmod:
  1107. try:
  1108. if len(inspect.signature(mod.on_dlmod).parameters) == 2:
  1109. await mod.on_dlmod(self.client, self._db)
  1110. else:
  1111. await mod.on_dlmod()
  1112. except Exception:
  1113. logger.info("Can't process `on_dlmod` hook", exc_info=True)
  1114. try:
  1115. if len(inspect.signature(mod.client_ready).parameters) == 2:
  1116. await mod.client_ready(self.client, self._db)
  1117. else:
  1118. await mod.client_ready()
  1119. except SelfUnload as e:
  1120. if no_self_unload:
  1121. raise e
  1122. logger.debug(f"Unloading {mod}, because it raised SelfUnload")
  1123. self.modules.remove(mod)
  1124. except SelfSuspend as e:
  1125. if no_self_unload:
  1126. raise e
  1127. logger.debug(f"Suspending {mod}, because it raised SelfSuspend")
  1128. return
  1129. except Exception as e:
  1130. logger.exception(
  1131. f"Failed to send mod init complete signal for {mod} due to {e},"
  1132. " attempting unload"
  1133. )
  1134. self.modules.remove(mod)
  1135. raise
  1136. self.register_commands(mod)
  1137. self.register_watcher(mod)
  1138. def get_classname(self, name: str) -> str:
  1139. return next(
  1140. (
  1141. module.__class__.__module__
  1142. for module in reversed(self.modules)
  1143. if name in (module.name, module.__class__.__module__)
  1144. ),
  1145. name,
  1146. )
  1147. async def unload_module(self, classname: str) -> bool:
  1148. """Remove module and all stuff from it"""
  1149. worked = []
  1150. with contextlib.suppress(AttributeError):
  1151. _hikka_client_id_logging_tag = copy.copy(self.client.tg_id)
  1152. for module in self.modules:
  1153. if classname.lower() in (
  1154. module.name.lower(),
  1155. module.__class__.__name__.lower(),
  1156. ):
  1157. if getattr(module, "__origin__", "") == "<core>":
  1158. raise CoreUnloadError(module.__class__.__name__)
  1159. worked += [module.__class__.__name__]
  1160. name = module.__class__.__name__
  1161. if "DYNO" not in os.environ:
  1162. path = os.path.join(
  1163. LOADED_MODULES_DIR,
  1164. f"{name}_{self.client.tg_id}.py",
  1165. )
  1166. if os.path.isfile(path):
  1167. os.remove(path)
  1168. logger.debug(f"Removed {name} file at {path=}")
  1169. logger.debug(f"Removing module for unload {module}")
  1170. self.modules.remove(module)
  1171. await module.on_unload()
  1172. for method in dir(module):
  1173. if isinstance(getattr(module, method), InfiniteLoop):
  1174. getattr(module, method).stop()
  1175. logger.debug(f"Stopped loop in {module=}, {method=}")
  1176. for name, cmd in self.commands.copy().items():
  1177. if cmd.__self__.__class__.__name__ == module.__class__.__name__:
  1178. logger.debug(f"Removing command {name} for unload")
  1179. del self.commands[name]
  1180. for alias, _command in self.aliases.copy().items():
  1181. if _command == name:
  1182. del self.aliases[alias]
  1183. for _watcher in self.watchers.copy():
  1184. if (
  1185. _watcher.__self__.__class__.__name__
  1186. == module.__class__.__name__
  1187. ):
  1188. logger.debug(f"Removing watcher {_watcher} for unload")
  1189. self.watchers.remove(_watcher)
  1190. logger.debug(f"{worked=}")
  1191. return worked
  1192. def add_alias(self, alias: str, cmd: str) -> bool:
  1193. """Make an alias"""
  1194. if cmd not in self.commands:
  1195. return False
  1196. self.aliases[alias.lower().strip()] = cmd
  1197. return True
  1198. def remove_alias(self, alias: str) -> bool:
  1199. """Remove an alias"""
  1200. try:
  1201. del self.aliases[alias.lower().strip()]
  1202. except KeyError:
  1203. return False
  1204. return True
  1205. async def log(self, *args, **kwargs):
  1206. """Unnecessary placeholder for logging"""