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

View File

@@ -23,7 +23,7 @@ if __name__ == "__main__":
import os import os
if not os.path.exists("config.json"): if not os.path.exists("config.json"):
exit exit()
config = load_file_json("config.json") config = load_file_json("config.json")
print("Config data type: " + str(type(config))) print("Config data type: " + str(type(config)))
print("Config data content: " + str(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" readme = "README.md"
requires-python = ">=3.11" requires-python = ">=3.11"
dependencies = [ dependencies = [
"chardet>=5.2.0",
"matrix-nio>=0.25.2", "matrix-nio>=0.25.2",
"mcdreforged>=2.15.2", "mcdreforged>=2.15.2",
] ]

View File

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