From e393b0b084d7cf889a1549fe6f37ea05a6d9c526 Mon Sep 17 00:00:00 2001 From: Astra Logical Date: Thu, 23 Jul 2026 13:58:32 -0400 Subject: [PATCH] Fix olm instnciation ordering --- matrix_keycloak_bot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/matrix_keycloak_bot.py b/matrix_keycloak_bot.py index f7a2030..878ff95 100644 --- a/matrix_keycloak_bot.py +++ b/matrix_keycloak_bot.py @@ -49,6 +49,7 @@ class PickleCryptoStore(MemoryCryptoStore): def __init__(self, filepath: str, account_id: str = "bot", pickle_key: str = ""): super().__init__(account_id=account_id, pickle_key=pickle_key) self.filepath = filepath + self.account = None # Ensure attribute always exists self.load() def load(self): @@ -410,8 +411,7 @@ async def main(): crypto_store = PickleCryptoStore(CRYPTO_STORE_FILE, account_id=client.mxid) client.state_store = state_store - client.crypto = OlmMachine(client, crypto_store, state_store) - + # Authenticate if access token is missing if not client.api.token: log.info("Logging in to Matrix homeserver...") @@ -430,13 +430,16 @@ async def main(): "device_id": client.device_id }, f) - # Ensure an Olm crypto account exists in the store + # Ensure an Olm crypto account exists in the store BEFORE initializing OlmMachine account = await crypto_store.get_account() if not account: log.info("Creating new Olm account...") account = OlmAccount() await crypto_store.put_account(account) + # Initialize OlmMachine after the account is guaranteed to exist + client.crypto = OlmMachine(client, crypto_store, state_store) + # Register Event Handlers client.add_event_handler(EventType.ROOM_MEMBER, on_invite) client.add_event_handler(EventType.ROOM_MESSAGE, on_message)