From 1d77e321740e5e484bc714d79e3e80c421f441b7 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 9 Jan 2023 01:18:45 +0400 Subject: [PATCH] Use config instead of forwarding all the variables --- mirrortea/__main__.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 8f6bf6e..a19c595 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -31,17 +31,8 @@ class Application: def __init__(self, config): self.config = config self.sqlite_adapter = SqliteAdapter(self, config.db_path) - self.matrix_loop = MatrixLoop( - self, - config.matrix_bot_id, - config.matrix_homeserver_url, - config.matrix_owner_id, - config.matrix_password, - ) - self.telegram_loop = TelegramLoop( - self, - config.telegram_bot_token, - ) + self.matrix_loop = MatrixLoop(self) + self.telegram_loop = TelegramLoop(self) async def run(self): try: @@ -55,18 +46,16 @@ class Application: await self.matrix_loop.finish() class MatrixLoop: - def __init__(self, app, bot_id, homeserver_url, owner_id, password): - self.app = app - self.bot_id = bot_id - self.homeserver_url = homeserver_url - self.owner_id = owner_id - self.password = password - - self.client = matrix.AsyncClient(homeserver_url, bot_id) + def __init__(self, app): + self.app = app + self.client = matrix.AsyncClient( + app.config.matrix_homeserver_url, + app.config.matrix_bot_id, + ) self.client.add_event_callback(self.on_message, matrix.RoomMessage) async def prepare(self): - await self.client.login(self.password) + await self.client.login(self.app.config.matrix_password) async def finish(self): await self.client.close() @@ -78,9 +67,9 @@ class MatrixLoop: print(room, event, file=sys.stderr) class TelegramLoop: - def __init__(self, app, bot_token): + def __init__(self, app): self.app = app - self.bot = telegram.Bot(token=bot_token) + self.bot = telegram.Bot(token=app.config.telegram_bot_token) self.dispatcher = telegram.Dispatcher(bot=self.bot) self.dispatcher.register_message_handler(self.on_message)