Fix olm instnciation ordering

This commit is contained in:
Astra Logical 2026-07-23 13:58:32 -04:00
parent 7ece0b2222
commit e393b0b084

View file

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