diff --git a/hikka/database.py b/hikka/database.py index b3b95ea..4c12d58 100755 --- a/hikka/database.py +++ b/hikka/database.py @@ -16,6 +16,7 @@ from typing import Any, Union from telethon.tl.functions.channels import EditTitleRequest from telethon.tl.types import Message +from telethon.errors.rpcerrorlist import ChannelsTooMuchError from . import utils @@ -28,6 +29,10 @@ DATA_DIR = ( logger = logging.getLogger(__name__) +class NoAssetsChannel(Exception): + """Raised when trying to read/store asset with no asset channel present""" + + class Database(dict): _next_revision_call = 0 _revisions = [] @@ -42,7 +47,7 @@ class Database(dict): return object.__repr__(self) async def init(self): - """Asynchronous initialisation unit""" + """Asynchronous initialization unit""" self._me = await self._client.get_me() self._db_path = os.path.join(DATA_DIR, f"config-{self._me.id}.json") self.read() @@ -72,13 +77,23 @@ class Database(dict): except Exception: pass - self._assets, _ = await utils.asset_channel( - self._client, - "hikka-assets", - "🌆 Your Hikka assets will be stored here", - archive=True, - avatar="https://raw.githubusercontent.com/hikariatama/assets/master/hikka-assets.png", - ) + try: + self._assets, _ = await utils.asset_channel( + self._client, + "hikka-assets", + "🌆 Your Hikka assets will be stored here", + archive=True, + avatar="https://raw.githubusercontent.com/hikariatama/assets/master/hikka-assets.png", + ) + except ChannelsTooMuchError: + self._assets = None + logger.critical( + "Can't find and/or create assets folder\n" + "This may cause several consequences, such as:\n" + "- Non working assets feature (e.g. notes)\n" + "- This error will occur every restart\n\n" + "You can solve this by leaving some channels/groups" + ) def read(self) -> str: """Read database""" @@ -158,6 +173,9 @@ class Database(dict): Save assets returns asset_id as integer """ + if not self._assets: + raise NoAssetsChannel("Tried to save asset to non-existing asset channel") # fmt: skip + return ( (await self._client.send_message(self._assets, message)).id if isinstance(message, Message) @@ -172,6 +190,9 @@ class Database(dict): async def fetch_asset(self, asset_id: int) -> Union[None, Message]: """Fetch previously saved asset by its asset_id""" + if not self._assets: + raise NoAssetsChannel("Tried to fetch asset from non-existing asset channel") # fmt: skip + asset = await self._client.get_messages(self._assets, ids=[asset_id]) if not asset: diff --git a/hikka/modules/updater.py b/hikka/modules/updater.py index f18d68b..1d57231 100755 --- a/hikka/modules/updater.py +++ b/hikka/modules/updater.py @@ -325,61 +325,69 @@ class UpdaterMod(loader.Module): except ValueError: folder_id = 2 - await self._client( - UpdateDialogFilterRequest( - folder_id, - DialogFilter( + try: + await self._client( + UpdateDialogFilterRequest( folder_id, - title="hikka", - pinned_peers=( - [ - await self._client.get_input_entity( - self._client.loader.inline.bot_id + DialogFilter( + folder_id, + title="hikka", + pinned_peers=( + [ + await self._client.get_input_entity( + self._client.loader.inline.bot_id + ) + ] + if self._client.loader.inline.init_complete + else [] + ), + include_peers=[ + await self._client.get_input_entity(dialog.entity) + async for dialog in self._client.iter_dialogs( + None, + ignore_migrated=True, ) - ] - if self._client.loader.inline.init_complete - else [] + if dialog.name + in { + "hikka-logs", + "hikka-onload", + "hikka-assets", + "hikka-backups", + "hikka-acc-switcher", + } + and dialog.is_channel + and ( + dialog.entity.participants_count == 1 + or dialog.entity.participants_count == 2 + and dialog.name == "hikka-logs" + ) + or ( + self._client.loader.inline.init_complete + and dialog.entity.id == self._client.loader.inline.bot_id + ) + or dialog.entity.id + in [1554874075, 1697279580, 1679998924] # official hikka chats + ], + emoticon="🐱", + exclude_peers=[], + contacts=False, + non_contacts=False, + groups=False, + broadcasts=False, + bots=False, + exclude_muted=False, + exclude_read=False, + exclude_archived=False, ), - include_peers=[ - await self._client.get_input_entity(dialog.entity) - async for dialog in self._client.iter_dialogs( - None, - ignore_migrated=True, - ) - if dialog.name - in { - "hikka-logs", - "hikka-onload", - "hikka-assets", - "hikka-backups", - "hikka-acc-switcher", - } - and dialog.is_channel - and ( - dialog.entity.participants_count == 1 - or dialog.entity.participants_count == 2 - and dialog.name == "hikka-logs" - ) - or ( - self._client.loader.inline.init_complete - and dialog.entity.id == self._client.loader.inline.bot_id - ) - or dialog.entity.id - in [1554874075, 1697279580, 1679998924] # official hikka chats - ], - emoticon="🐱", - exclude_peers=[], - contacts=False, - non_contacts=False, - groups=False, - broadcasts=False, - bots=False, - exclude_muted=False, - exclude_read=False, - exclude_archived=False, - ), + ) + ) + except Exception: + logger.critical( + "Can't create Hikka folder. Possible reasons are:\n" + "- User reached the limit of folders in Telegram\n" + "- User got floodwait\n" + "Ignoring error and adding folder addition to ignore list" ) - ) self.set("do_not_create", True)