Skip to content

Commit

Permalink
Fixed config loader error in first time load plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooling0602 committed Jan 7, 2025
1 parent cd4e969 commit 042a1a0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[framework]
ver=1
[main]
ver=2.5.2
ver=2.5.3
[release]
test=0
[ci]
Expand Down
11 changes: 11 additions & 0 deletions matrix_sync/client/init.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import asyncio
import json
import sys
import aiofiles
from matrix_sync.commands import start_sync
import matrix_sync.logger.get_logger as get_logger
import matrix_sync.plg_globals as plg_globals

from . import *
from ..utils import configDir, tr
from ..utils.token import getToken
from nio import LoginResponse
from mcdreforged.api.decorator import new_thread


async def cache_token(resp: LoginResponse):
Expand All @@ -30,6 +33,7 @@ async def login_by_password():
await cache_token(resp)
plg_globals.token_vaild = True
logger.info(tr("login.save_token"), "FirstLogin")
start_sync()
else:
tip = tr("login.failed")
logger.error(f"{tip}: {resp}", "FirstLogin")
Expand All @@ -38,6 +42,13 @@ async def login_by_password():
logger.error(tr("check_config"), "FirstLogin")
sys.exit(1)

async def add_init_ask():
await login_by_password()

@new_thread()
def first_login():
asyncio.run(add_init_ask())

async def check_token() -> bool:
user, token = await getToken()
if user != plg_globals.config["user_id"]:
Expand Down
2 changes: 1 addition & 1 deletion matrix_sync/client/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None:
def on_sync_error(response: SyncError):
logger = get_logger()
global homeserver_online
logger.error(f"Sync error: {response.status_code}")
logger.error(f"Sync error: {response.status_code}", "Receiver")
if response.status_code >= 500:
homeserver_online = False

Expand Down
9 changes: 7 additions & 2 deletions matrix_sync/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os
import matrix_sync.utils.tr as tr
import matrix_sync.plg_globals as plg_globals
Expand All @@ -8,7 +9,7 @@
from mcdreforged.api.types import PluginServerInterface


async def load_config(server: PluginServerInterface):
def load_config(server: PluginServerInterface):
plg_globals.config = server.load_config_simple('config.json', account_config)
if plg_globals.config == account_config:
server.unload_plugin(plgSelf.id)
Expand All @@ -21,4 +22,8 @@ async def load_config(server: PluginServerInterface):
if not plg_globals.settings["log_style"]["mcdr"]:
psi.logger.info("Plugin MatrixSync will use its logger, different with MCDR.")
if os.path.exists(f"{configDir}/token.json"):
plg_globals.token_vaild = await check_token()
plg_globals.token_vaild = asyncio.run(add_check_task())

async def add_check_task() -> bool:
result = await check_token()
return result
14 changes: 7 additions & 7 deletions matrix_sync/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@


# Framwork ver: 2.5.1-3
async def on_load(server: PluginServerInterface, prev_module):
await load_config(server)
# await check_settings(server)
def on_load(server: PluginServerInterface, prev_module):
load_config(server)
command_register(server)
if not os.path.exists(f"{configDir}/token.json"):
from .client.init import login_by_password
await login_by_password()
listen_message(server)
start_sync()
from .client.init import first_login
first_login()
else:
listen_message(server)
start_sync()

def on_server_start(server: PluginServerInterface):
matrix_reporter(tr("server_status.starting"))
Expand Down
2 changes: 1 addition & 1 deletion mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "matrix_sync",
"version": "2.5.2",
"version": "2.5.3",
"name": "MatrixSync",
"description": {
"en_us": "Sync messages between online game and Matrix groups.",
Expand Down

0 comments on commit 042a1a0

Please sign in to comment.