diff --git a/hikka/loader.py b/hikka/loader.py index eaa3d4f..9b2005e 100644 --- a/hikka/loader.py +++ b/hikka/loader.py @@ -501,7 +501,7 @@ class Modules: with contextlib.suppress(AttributeError): _hikka_client_id_logging_tag = copy.copy(self.client._tg_id) - try: + with contextlib.suppress(AttributeError): if instance.watcher: for watcher in self.watchers: if ( @@ -513,8 +513,6 @@ class Modules: self.watchers.remove(watcher) self.watchers += [instance.watcher] - except AttributeError: - pass def _lookup(self, modname: str): return next( @@ -682,7 +680,7 @@ class Modules: ) for conf in class_instance.config.keys(): - try: + with contextlib.suppress(Exception): class_instance.config.set_no_raise( conf, ( @@ -694,13 +692,10 @@ class Modules: or class_instance.config.getdef(conf) ), ) - except Exception: - pass - self.libraries += [class_instance] if len([x.name for x in self.libraries]) != len( - set([x.name for x in self.libraries]) + {x.name for x in self.libraries} ): self.libraries.remove(class_instance) _raise( @@ -755,7 +750,7 @@ class Modules: ) try: for conf in mod.config.keys(): - try: + with contextlib.suppress(validators.ValidationError): mod.config.set_no_raise( conf, ( @@ -765,8 +760,6 @@ class Modules: or mod.config.getdef(conf) ), ) - except validators.ValidationError: - pass except AttributeError: logger.warning( "Got invalid config instance. Expected `ModuleConfig`, got" diff --git a/hikka/modules/help.py b/hikka/modules/help.py index 2b52c9b..655f484 100755 --- a/hikka/modules/help.py +++ b/hikka/modules/help.py @@ -208,7 +208,7 @@ class HelpMod(loader.Module): ) await utils.answer( - message, f"{reply}\n\n{self.strings('not_exact') if not exact else ''}" + message, f"{reply}\n\n{'' if exact else self.strings('not_exact')}" ) @loader.unrestricted @@ -234,10 +234,7 @@ class HelpMod(loader.Module): hidden = self.get("hide", []) - reply = self.strings("all_header").format( - count, - len(hidden) if not force else 0, - ) + reply = self.strings("all_header").format(count, 0 if force else len(hidden)) shown_warn = False plain_ = [] @@ -347,11 +344,12 @@ class HelpMod(loader.Module): no_commands_ = "".join(no_commands_) if force else "" partial_load = ( - f"\n\n{self.strings('partial_load')}" - if not self.lookup("Loader")._fully_loaded - else "" + "" + if self.lookup("Loader")._fully_loaded + else f"\n\n{self.strings('partial_load')}" ) + await utils.answer( message, f"{reply}\n{''.join(core_)}{''.join(plain_)}{''.join(inline_)}{no_commands_}{partial_load}", diff --git a/hikka/modules/hikka_config.py b/hikka/modules/hikka_config.py index ea490c7..f1691d7 100755 --- a/hikka/modules/hikka_config.py +++ b/hikka/modules/hikka_config.py @@ -346,15 +346,6 @@ class HikkaConfigMod(loader.Module): [ *( [ - { - "text": f"✅ {self.strings('set')} `True`", - "callback": self.inline__set_bool, - "args": (mod, option, True), - "kwargs": {"obj_type": obj_type}, - } - ] - if not self.lookup(mod).config[option] - else [ { "text": f"❌ {self.strings('set')} `False`", "callback": self.inline__set_bool, @@ -362,7 +353,16 @@ class HikkaConfigMod(loader.Module): "kwargs": {"obj_type": obj_type}, } ] - ), + if self.lookup(mod).config[option] + else [ + { + "text": f"✅ {self.strings('set')} `True`", + "callback": self.inline__set_bool, + "args": (mod, option, True), + "kwargs": {"obj_type": obj_type}, + } + ] + ) ], [ *( diff --git a/hikka/modules/hikka_settings.py b/hikka/modules/hikka_settings.py index c3a777c..57a927a 100755 --- a/hikka/modules/hikka_settings.py +++ b/hikka/modules/hikka_settings.py @@ -462,7 +462,7 @@ class HikkaSettingsMod(loader.Module): nn = list(set(nn)) # skipcq: PTC-W0018 await utils.answer(message, self.strings("user_nn").format("on")) else: - nn = list(set(nn) - set([u])) # skipcq: PTC-W0018 + nn = list(set(nn) - {u}) await utils.answer(message, self.strings("user_nn").format("off")) self._db.set(main.__name__, "nonickusers", nn) @@ -487,7 +487,7 @@ class HikkaSettingsMod(loader.Module): ), ) else: - nn = list(set(nn) - set([chat])) # skipcq: PTC-W0018 + nn = list(set(nn) - {chat}) await utils.answer( message, self.strings("cmd_nn").format( @@ -521,7 +521,7 @@ class HikkaSettingsMod(loader.Module): ), ) else: - nn = list(set(nn) - set([args])) # skipcq: PTC-W0018 + nn = list(set(nn) - {args}) await utils.answer( message, self.strings("cmd_nn").format( @@ -561,10 +561,13 @@ class HikkaSettingsMod(loader.Module): main.__name__, "nonickusers", list( - set(self._db.get(main.__name__, "nonickusers", [])) - - set([user_id]) + ( + set(self._db.get(main.__name__, "nonickusers", [])) + - {user_id} + ) ), ) + logger.warning( f"User {user_id} removed from nonickusers list", exc_info=True ) @@ -594,10 +597,13 @@ class HikkaSettingsMod(loader.Module): main.__name__, "nonickchats", list( - set(self._db.get(main.__name__, "nonickchats", [])) - - set([chat]) + ( + set(self._db.get(main.__name__, "nonickchats", [])) + - {chat} + ) ), ) + logger.warning(f"Chat {chat} removed from nonickchats list") continue @@ -733,25 +739,23 @@ class HikkaSettingsMod(loader.Module): ), ], [ - ( - { - "text": self.strings("suggest_fs"), - "callback": self.inline__setting, - "args": ( - "disable_modules_fs", - True, - ), - } - if not self._db.get(main.__name__, "disable_modules_fs", False) - else { - "text": self.strings("do_not_suggest_fs"), - "callback": self.inline__setting, - "args": ( - "disable_modules_fs", - False, - ), - } - ), + { + "text": self.strings("do_not_suggest_fs"), + "callback": self.inline__setting, + "args": ( + "disable_modules_fs", + False, + ), + } + if self._db.get(main.__name__, "disable_modules_fs", False) + else { + "text": self.strings("suggest_fs"), + "callback": self.inline__setting, + "args": ( + "disable_modules_fs", + True, + ), + } ], [ ( @@ -800,9 +804,7 @@ class HikkaSettingsMod(loader.Module): { "text": self.strings("disable_stats"), "callback": self.inline__setting, - "args": ( - "stats", False - ), + "args": ("stats", False), } if self._db.get(main.__name__, "stats", True) else { diff --git a/hikka/modules/python.py b/hikka/modules/python.py index 4685567..541dca3 100755 --- a/hikka/modules/python.py +++ b/hikka/modules/python.py @@ -141,12 +141,14 @@ class PythonMod(loader.Module): ) ret = ret.replace(str(self._phone), "📵") - postgre = os.environ.get("DATABASE_URL") or main.get_config_key("postgre_uri") - if postgre: + if postgre := os.environ.get("DATABASE_URL") or main.get_config_key( + "postgre_uri" + ): ret = ret.replace(postgre, "postgre://**************************") - redis = os.environ.get("REDIS_URL") or main.get_config_key("redis_uri") - if redis: + if redis := os.environ.get("REDIS_URL") or main.get_config_key( + "redis_uri" + ): ret = ret.replace(redis, "redis://**************************") if os.environ.get("hikka_session"): diff --git a/hikka/utils.py b/hikka/utils.py index e59bb7d..9b35308 100755 --- a/hikka/utils.py +++ b/hikka/utils.py @@ -146,10 +146,7 @@ def get_args_raw(message: Message) -> str: if not (message := getattr(message, "message", message)): return False - if len(args := message.split(maxsplit=1)) > 1: - return args[1] - - return "" + return args[1] if len(args := message.split(maxsplit=1)) > 1 else "" def get_args_split_by(message: Message, separator: str) -> List[str]: @@ -624,10 +621,7 @@ def get_named_platform() -> str: if "Orange" in model: return f"🍊 {model}" - if "Raspberry" in model: - return f"🍇 {model}" - - return f"❓ {model}" + return f"🍇 {model}" if "Raspberry" in model else f"❓ {model}" except Exception: # In case of weird fs, aka Termux pass @@ -635,7 +629,6 @@ def get_named_platform() -> str: is_termux = "com.termux" in os.environ.get("PREFIX", "") is_okteto = "OKTETO" in os.environ is_docker = "DOCKER" in os.environ - is_lavhost = "LAVHOST" in os.environ is_heroku = "DYNO" in os.environ if is_heroku: @@ -650,10 +643,8 @@ def get_named_platform() -> str: if is_okteto: return "☁️ Okteto" - if is_lavhost: - return f"✌️ lavHost {os.environ['LAVHOST']}" - - return "📻 VDS" + is_lavhost = "LAVHOST" in os.environ + return f"✌️ lavHost {os.environ['LAVHOST']}" if is_lavhost else "📻 VDS" def uptime() -> int: @@ -1012,11 +1003,7 @@ def get_kwargs() -> dict: # https://stackoverflow.com/a/65927265/19170642 frame = inspect.currentframe().f_back keys, _, _, values = inspect.getargvalues(frame) - kwargs = {} - for key in keys: - if key != "self": - kwargs[key] = values[key] - return kwargs + return {key: values[key] for key in keys if key != "self"} init_ts = time.perf_counter()