fix: improve codes

This commit is contained in:
2025-09-18 22:50:50 +08:00
parent aacecdedce
commit 0833cd586f
4 changed files with 15 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
from typing import Optional
from typing import Callable, Union, Optional
from logging import Logger
from nio import (
AsyncClient,
@@ -16,9 +16,9 @@ class MatrixClient:
user_id: str,
token: str,
device_id: Optional[str] = "mcdr",
logger: Optional[Logger, callable] = print,
logger: Union[Logger, Callable, None] = print,
):
self.connected: bool = False
self.connected: bool = True
self.client = AsyncClient(homeserver=homeserver)
self.client.user_id = user_id
self.client.access_token = token
@@ -47,25 +47,22 @@ class MatrixClient:
if hasattr(self.logger, "debug"):
self.logger.debug(f"MatrixClient response: {response}")
def on_sync_error(response: SyncError):
async def on_sync_error(response: SyncError):
self.network_status = False
message = (
"Sync error with matrix homeserver: "
f"{response.status_code}"
)
RED = "\033[31m"
RESET = "\033[0m"
_message = f"Sync error with matrix homeserver: {response.status_code}"
_ansi_color_red = "\033[31m"
_ansi_reset_color = "\033[0m"
if not self.logger:
print(RED + message + RESET)
print(_ansi_color_red + _message + _ansi_reset_color)
if hasattr(self.logger, "error"):
self.logger.error(message)
self.logger.error(_message)
else:
self.logger(RED + message + RESET)
self.logger(_ansi_color_red + _message + _ansi_reset_color)
client.add_response_callback(on_sync_response, SyncResponse)
client.add_response_callback(on_sync_error, SyncError)
async def message_callback(
room: MatrixRoom, event: RoomMessageText
) -> None:
raise NotImplementedError()
) -> None: # 处理接受到的消息回调
raise NotImplementedError() # 咕咕咕……

View File

@@ -23,7 +23,7 @@ if __name__ == "__main__":
import os
if not os.path.exists("config.json"):
exit
exit()
config = load_file_json("config.json")
print("Config data type: " + str(type(config)))
print("Config data content: " + str(config))

View File

@@ -5,6 +5,7 @@ description = "A python plugin to sync messages between Minecraft server and mat
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"chardet>=5.2.0",
"matrix-nio>=0.25.2",
"mcdreforged>=2.15.2",
]

View File

@@ -1,2 +1,3 @@
chardet
matrix-nio
mcdreforged