From 09a5916c500811140df6f81c1071563aa60a3f13 Mon Sep 17 00:00:00 2001 From: Mooling0602 Date: Tue, 17 Dec 2024 13:05:25 +0800 Subject: [PATCH] Update to v2.4.2 with lots of error fixed, no loner need config.room_name --- README.md | 3 +-- README_en_us.md | 3 +-- config.ini | 4 ++-- matrix_sync/client.py | 5 +++-- matrix_sync/config.py | 5 ++--- matrix_sync/entry.py | 9 +++++---- matrix_sync/receiver.py | 24 ++++++++++++++++-------- matrix_sync/utils/globals.py | 3 ++- mcdreforged.plugin.json | 2 +- 9 files changed, 33 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 76638ad..626862a 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,7 @@ | **homeserver** | 机器人账号所属的根服务器 | | **user_id** | 机器人的账号ID,格式为@<用户名>:<根服务器>,如@mcchatbot:example.com | | **password** | 机器人账号的密码,在初次登录和重新生成token时使用 | -| **room_id** | 需要接收游戏消息的房间的ID,使用管理员权限在房间设置中查看 | -| **room_name** | 需要转发消息到游戏内的房间的显示名称(必须准确无误,若发生更新也需要同步修改,否则你将看不到任何消息) | +| **room_id** | 需要同步游戏消息的房间的ID,使用管理员权限在房间设置中查看 | | **device_id** | 登录用的设备名,一般无需修改,可自定义 | > 只支持单账号和单聊天房间(相当于QQ群),计划在v3版本以后开发多配置管理。 diff --git a/README_en_us.md b/README_en_us.md index 87c9dde..1c52cf0 100644 --- a/README_en_us.md +++ b/README_en_us.md @@ -28,8 +28,7 @@ If there is any issue with message forwarding in any direction during the messag | **homeserver** | The homeserver address used to log in to the bot account | | **user_id** | The bot account ID, formatted as @username:example.com | | **password** | The password of the bot account, generally only used for the initial login | -| **room_id** | The ID of the room to receive game messages | -| **room_name** | The display name of the room to forward messages to the game (must be accurate, if updated, it also needs to be modified synchronously, otherwise you will not see any messages) | +| **room_id** | The ID of the room to sync game messages | | **device_id** | The device name used for login, generally no need to modify, can be customized | Multi-config is not supported, will add API support for developers from v3. diff --git a/config.ini b/config.ini index db81378..703af78 100644 --- a/config.ini +++ b/config.ini @@ -1,9 +1,9 @@ # Do not edit this file! # 不要修改这个文件! [framework] -ver=3 +ver=1 [main] -ver=2.4.1 +ver=2.4.2 [release] test=0 [ci] diff --git a/matrix_sync/client.py b/matrix_sync/client.py index c0760b5..fac3d4e 100644 --- a/matrix_sync/client.py +++ b/matrix_sync/client.py @@ -5,13 +5,12 @@ from .utils import psi, plgSelf, tr from .utils.token import getToken +from .utils.globals import clientStatus from .reporter import sendMsg from mcdreforged.api.decorator import new_thread from nio import AsyncClient, LoginResponse -clientStatus = False - # Cache Token. def cache_token(resp: LoginResponse): from .config import TOKEN_FILE, user_id @@ -58,11 +57,13 @@ async def init_client() -> None: psi.logger.info(tr("init_tips.do_unload")) psi.unload_plugin(plgSelf.id) else: + psi.logger.info("MatrixClient inited.") await test_client() # Send test messages. async def test_client(): global clientStatus message = tr("sync_tips.reporter_status") + psi.logger.info("Reporting on_load tip to matrix...") await sendMsg(message) clientStatus = True diff --git a/matrix_sync/config.py b/matrix_sync/config.py index 7ea9920..002759d 100644 --- a/matrix_sync/config.py +++ b/matrix_sync/config.py @@ -8,7 +8,6 @@ "user_id": "@username:matrix.example.org", "password": "your_password", "room_id": "!your-room_id:matrix.example.org", - "room_name": "your-room-display-name", "device_id": "mcdr" } @@ -24,14 +23,14 @@ } def load_config(): - global config, user_id, password, room_id, room_name, settings, use_token, DATA_FOLDR, TOKEN_FILE, device_id, homeserver, load_tip, sync_old_msg + global config, user_id, password, room_id, room_name, settings, use_token, DATA_FOLDR, TOKEN_FILE, device_id, homeserver, load_tip, sync_old_msg, allow_all_rooms_msg config = psi.load_config_simple("config.json", account_config) user_id = config["user_id"] password = config["password"] room_id = config["room_id"] - room_name = config["room_name"] settings = psi.load_config_simple("settings.json", bot_config) sync_old_msg = settings["sync_old_msg"] + allow_all_rooms_msg = settings["allow_all_rooms_msg"] DATA_FOLDER = psi.get_data_folder() tip_path = psi.rtr("matrix_sync.init_tips.config_path") load_tip = f"{tip_path}: {DATA_FOLDER}" diff --git a/matrix_sync/entry.py b/matrix_sync/entry.py index d1f6a84..94f3f04 100644 --- a/matrix_sync/entry.py +++ b/matrix_sync/entry.py @@ -1,8 +1,9 @@ import time +import os from typing import Optional from .utils import tr -from .client import init, clientStatus +from .client import init from .config import load_config, check_config from .utils import psi, globals from .utils.commands import * @@ -10,7 +11,7 @@ from mcdreforged.api.all import * -# Framwork ver: 2.4.1 +# Framwork ver: 2.4.2 def on_load(server: PluginServerInterface, prev_module): load_config() from .config import load_tip @@ -22,7 +23,8 @@ def on_load(server: PluginServerInterface, prev_module): else: init() plugin_command(server) - if server.is_server_startup() and clientStatus: + from .config import TOKEN_FILE + if server.is_server_startup() and os.path.exists(TOKEN_FILE): start_sync() # Automatically run sync processes. @@ -30,7 +32,6 @@ def start_sync(on_reload: Optional[bool] = True): if not globals.tLock.locked(): start_room_msg() if not on_reload: - time.sleep(1) message = tr("sync_tips.server_started") send_matrix(message) else: diff --git a/matrix_sync/receiver.py b/matrix_sync/receiver.py index fd2b153..b08e98d 100644 --- a/matrix_sync/receiver.py +++ b/matrix_sync/receiver.py @@ -5,12 +5,16 @@ from .utils.token import getToken from .utils import psi, plgSelf, tr from mcdreforged.api.event import PluginEvent -from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncError +from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncError, RoomMessagesResponse from typing import Optional homeserver_online = True +def load_msg_filter(room_id: str): + msg_filter = {"room_id": room_id} + return msg_filter + class RoomMessageEvent(PluginEvent): def __init__(self, message: str, sender: str, room: Optional[str] = None): super().__init__('MatrixRoomMessage') # 使用固定的事件ID @@ -20,18 +24,18 @@ def __init__(self, message: str, sender: str, room: Optional[str] = None): async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None: transfer = False - from .config import user_id, room_name, settings + from .config import user_id, settings msg_format = settings["room_msg_format"]["multi_room"] roomMsg = msg_format.replace('%room_display_name%', room.display_name).replace('%sender%', room.user_name(event.sender)).replace('%message%', event.body) # Avoid echo messages. if not event.sender == user_id: # Apply settings config - if not matrix_sync.config.settings["allow_all_rooms_msg"]: - msg_format = matrix_sync.config.settings["room_msg_format"]["single_room"] + if not settings["allow_all_rooms_msg"]: + msg_format = settings["room_msg_format"]["single_room"] roomMsg = msg_format.replace('%sender%', room.user_name(event.sender)).replace('%message%', event.body) - if room.display_name == room_name: - transfer = True - psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender)), (event.body, room.user_name(event.sender))) + # if response.room_id == room_id: + transfer = True + psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender)), (event.body, room.user_name(event.sender))) else: psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender), room.display_name), (event.body, room.user_name(event.sender), room.display_name)) if transfer: @@ -45,7 +49,8 @@ def on_sync_error(response: SyncError): async def getMsg() -> None: global next_batch, msg_callback - from .config import homeserver, device_id, user_id, sync_old_msg + from .config import homeserver, device_id, user_id, sync_old_msg, allow_all_rooms_msg + from .config import room_id as cfg_room_id client = AsyncClient(f"{homeserver}") user, token = await getToken() client.access_token = token @@ -63,6 +68,9 @@ async def getMsg() -> None: client.user_id = user_id client.device_id = device_id + if not allow_all_rooms_msg: + print("ok.") + await client.upload_filter(room={"rooms": [cfg_room_id]}) client.add_response_callback(on_sync_error, SyncError) try: diff --git a/matrix_sync/utils/globals.py b/matrix_sync/utils/globals.py index ac2fbc6..c60af13 100644 --- a/matrix_sync/utils/globals.py +++ b/matrix_sync/utils/globals.py @@ -3,4 +3,5 @@ tLock = threading.Lock() report_matrix = False cleaned = False -sync_task = None \ No newline at end of file +sync_task = None +clientStatus = None \ No newline at end of file diff --git a/mcdreforged.plugin.json b/mcdreforged.plugin.json index e456fa0..1534411 100644 --- a/mcdreforged.plugin.json +++ b/mcdreforged.plugin.json @@ -1,6 +1,6 @@ { "id": "matrix_sync", - "version": "2.4.1", + "version": "2.4.2", "name": "MatrixSync", "description": { "en_us": "Sync messages between online game and Matrix groups.",