From f1882912d1cdcbd1732813d199ce1eba616dc7f2 Mon Sep 17 00:00:00 2001 From: Nick80835 Date: Fri, 2 Aug 2024 19:38:55 -0400 Subject: [PATCH] initialize aiohttp from coroutine --- ubot/__init__.py | 1 + ubot/loader.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ubot/__init__.py b/ubot/__init__.py index cd4872a..3d5027b 100644 --- a/ubot/__init__.py +++ b/ubot/__init__.py @@ -57,6 +57,7 @@ class MicroBot(): self.me = await self.client.get_me() self.loader = Loader(self) + await self.loader._initialize_loader() client = self.client ldr = self.loader self.loader.load_all_modules() diff --git a/ubot/loader.py b/ubot/loader.py index 252a658..d71c6f2 100644 --- a/ubot/loader.py +++ b/ubot/loader.py @@ -2,23 +2,34 @@ import glob from concurrent.futures import ThreadPoolExecutor from functools import partial from importlib import import_module, reload +from logging import Logger from os.path import basename, dirname, isfile from traceback import print_exc +from typing import TYPE_CHECKING from aiohttp import ClientSession +from ubot.settings import Settings + from .cache import Cache from .command import (CallbackQueryCommand, Command, InlineArticleCommand, InlinePhotoCommand) from .command_handler import CommandHandler from .database import Database +if TYPE_CHECKING: + from ubot import MicroBot + class Loader(): - aioclient = ClientSession() - thread_pool = ThreadPoolExecutor() - cache = Cache(aioclient) - db = Database() + thread_pool: ThreadPoolExecutor = ThreadPoolExecutor() + db: Database = Database() + micro_bot: "MicroBot" + settings: Settings + logger: Logger + command_handler: CommandHandler + aioclient: ClientSession + cache: Cache loaded_modules = [] all_modules = [] @@ -27,6 +38,10 @@ class Loader(): self.micro_bot = micro_bot self.settings = micro_bot.settings self.logger = micro_bot.logger + + async def _initialize_loader(self): + self.aioclient = ClientSession() + self.cache = Cache(self.aioclient) self.command_handler = CommandHandler(self) def load_all_modules(self):