Use config instead of forwarding all the variables

This commit is contained in:
Alex Kotov 2023-01-09 01:18:45 +04:00
parent ca1af360a4
commit 1d77e32174
No known key found for this signature in database
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 11 additions and 22 deletions

View File

@ -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)