Handle empty credentials file
This commit is contained in:
parent
8ad5f56d86
commit
d4b9e808e4
1 changed files with 14 additions and 9 deletions
|
|
@ -462,18 +462,23 @@ async def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
# 1. Attempt to restore an existing session
|
# 1. Attempt to restore an existing session
|
||||||
|
session_restored = False
|
||||||
if os.path.exists(CREDENTIALS_FILE):
|
if os.path.exists(CREDENTIALS_FILE):
|
||||||
print("Restoring existing Matrix session...")
|
print("Restoring existing Matrix session...")
|
||||||
|
try:
|
||||||
with open(CREDENTIALS_FILE, "r") as f:
|
with open(CREDENTIALS_FILE, "r") as f:
|
||||||
creds = json.load(f)
|
creds = json.load(f)
|
||||||
|
|
||||||
client.access_token = creds["access_token"]
|
client.access_token = creds["access_token"]
|
||||||
client.user_id = creds["user_id"]
|
client.user_id = creds["user_id"]
|
||||||
client.device_id = creds["device_id"]
|
client.device_id = creds["device_id"]
|
||||||
|
session_restored = True
|
||||||
|
except (json.JSONDecodeError, KeyError) as e:
|
||||||
|
print(f"⚠️ Credentials file is empty or corrupted ({e}). Discarding and forcing new login...")
|
||||||
|
|
||||||
# 2. If no session exists, log in and save the credentials
|
# 2. If no session exists or restoration failed, log in and save the credentials
|
||||||
else:
|
if not session_restored:
|
||||||
print(f"No existing session found. Logging in to {MATRIX_HOMESERVER}...")
|
print(f"No valid session found. Logging in to {MATRIX_HOMESERVER}...")
|
||||||
resp = await client.login(MATRIX_BOT_PASSWORD)
|
resp = await client.login(MATRIX_BOT_PASSWORD)
|
||||||
|
|
||||||
# Create the store directory if it doesn't exist
|
# Create the store directory if it doesn't exist
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue