Skip to content

Commit de6bf3a

Browse files
authored
Merge pull request #24 from Mooling0602/dev
dev -> main
2 parents 7a3e930 + d9a15b0 commit de6bf3a

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
| **homeserver** | 机器人账号所属的根服务器 |
2727
| **user_id** | 机器人的账号ID,格式为@<用户名>:<根服务器>,如@mcchatbot:example.com |
2828
| **password** | 机器人账号的密码,在初次登录和重新生成token时使用 |
29-
| **room_id** | 需要接收游戏消息的房间的ID,使用管理员权限在房间设置中查看 |
30-
| **room_name** | 需要转发消息到游戏内的房间的显示名称(必须准确无误,若发生更新也需要同步修改,否则你将看不到任何消息) |
29+
| **room_id** | 需要同步游戏消息的房间的ID,使用管理员权限在房间设置中查看 |
3130
| **device_id** | 登录用的设备名,一般无需修改,可自定义 |
3231

3332
> 只支持单账号和单聊天房间(相当于QQ群),计划在v3版本以后开发多配置管理。

README_en_us.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ If there is any issue with message forwarding in any direction during the messag
2828
| **homeserver** | The homeserver address used to log in to the bot account |
2929
| **user_id** | The bot account ID, formatted as @username:example.com |
3030
| **password** | The password of the bot account, generally only used for the initial login |
31-
| **room_id** | The ID of the room to receive game messages |
32-
| **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) |
31+
| **room_id** | The ID of the room to sync game messages |
3332
| **device_id** | The device name used for login, generally no need to modify, can be customized |
3433

3534
Multi-config is not supported, will add API support for developers from v3.

config.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Do not edit this file!
22
# 不要修改这个文件!
33
[framework]
4-
ver=3
4+
ver=1
55
[main]
6-
ver=2.4.1
6+
ver=2.4.2
77
[release]
88
test=0
99
[ci]

matrix_sync/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55

66
from .utils import psi, plgSelf, tr
77
from .utils.token import getToken
8+
from .utils.globals import clientStatus
89
from .reporter import sendMsg
910
from mcdreforged.api.decorator import new_thread
1011
from nio import AsyncClient, LoginResponse
1112

1213

13-
clientStatus = False
14-
1514
# Cache Token.
1615
def cache_token(resp: LoginResponse):
1716
from .config import TOKEN_FILE, user_id
@@ -58,11 +57,13 @@ async def init_client() -> None:
5857
psi.logger.info(tr("init_tips.do_unload"))
5958
psi.unload_plugin(plgSelf.id)
6059
else:
60+
psi.logger.info("MatrixClient inited.")
6161
await test_client()
6262

6363
# Send test messages.
6464
async def test_client():
6565
global clientStatus
6666
message = tr("sync_tips.reporter_status")
67+
psi.logger.info("Reporting on_load tip to matrix...")
6768
await sendMsg(message)
6869
clientStatus = True

matrix_sync/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"user_id": "@username:matrix.example.org",
99
"password": "your_password",
1010
"room_id": "!your-room_id:matrix.example.org",
11-
"room_name": "your-room-display-name",
1211
"device_id": "mcdr"
1312
}
1413

@@ -24,14 +23,14 @@
2423
}
2524

2625
def load_config():
27-
global config, user_id, password, room_id, room_name, settings, use_token, DATA_FOLDR, TOKEN_FILE, device_id, homeserver, load_tip, sync_old_msg
26+
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
2827
config = psi.load_config_simple("config.json", account_config)
2928
user_id = config["user_id"]
3029
password = config["password"]
3130
room_id = config["room_id"]
32-
room_name = config["room_name"]
3331
settings = psi.load_config_simple("settings.json", bot_config)
3432
sync_old_msg = settings["sync_old_msg"]
33+
allow_all_rooms_msg = settings["allow_all_rooms_msg"]
3534
DATA_FOLDER = psi.get_data_folder()
3635
tip_path = psi.rtr("matrix_sync.init_tips.config_path")
3736
load_tip = f"{tip_path}: {DATA_FOLDER}"

matrix_sync/entry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import time
2+
import os
23

34
from typing import Optional
45
from .utils import tr
5-
from .client import init, clientStatus
6+
from .client import init
67
from .config import load_config, check_config
78
from .utils import psi, globals
89
from .utils.commands import *
910
from .reporter import send_matrix
1011
from mcdreforged.api.all import *
1112

1213

13-
# Framwork ver: 2.4.1
14+
# Framwork ver: 2.4.2
1415
def on_load(server: PluginServerInterface, prev_module):
1516
load_config()
1617
from .config import load_tip
@@ -22,15 +23,15 @@ def on_load(server: PluginServerInterface, prev_module):
2223
else:
2324
init()
2425
plugin_command(server)
25-
if server.is_server_startup() and clientStatus:
26+
from .config import TOKEN_FILE
27+
if server.is_server_startup() and os.path.exists(TOKEN_FILE):
2628
start_sync()
2729

2830
# Automatically run sync processes.
2931
def start_sync(on_reload: Optional[bool] = True):
3032
if not globals.tLock.locked():
3133
start_room_msg()
3234
if not on_reload:
33-
time.sleep(1)
3435
message = tr("sync_tips.server_started")
3536
send_matrix(message)
3637
else:

matrix_sync/receiver.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
from .utils.token import getToken
66
from .utils import psi, plgSelf, tr
77
from mcdreforged.api.event import PluginEvent
8-
from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncError
8+
from nio import AsyncClient, MatrixRoom, RoomMessageText, SyncError, RoomMessagesResponse
99
from typing import Optional
1010

1111

1212
homeserver_online = True
1313

14+
def load_msg_filter(room_id: str):
15+
msg_filter = {"room_id": room_id}
16+
return msg_filter
17+
1418
class RoomMessageEvent(PluginEvent):
1519
def __init__(self, message: str, sender: str, room: Optional[str] = None):
1620
super().__init__('MatrixRoomMessage') # 使用固定的事件ID
@@ -20,18 +24,18 @@ def __init__(self, message: str, sender: str, room: Optional[str] = None):
2024

2125
async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None:
2226
transfer = False
23-
from .config import user_id, room_name, settings
27+
from .config import user_id, settings
2428
msg_format = settings["room_msg_format"]["multi_room"]
2529
roomMsg = msg_format.replace('%room_display_name%', room.display_name).replace('%sender%', room.user_name(event.sender)).replace('%message%', event.body)
2630
# Avoid echo messages.
2731
if not event.sender == user_id:
2832
# Apply settings config
29-
if not matrix_sync.config.settings["allow_all_rooms_msg"]:
30-
msg_format = matrix_sync.config.settings["room_msg_format"]["single_room"]
33+
if not settings["allow_all_rooms_msg"]:
34+
msg_format = settings["room_msg_format"]["single_room"]
3135
roomMsg = msg_format.replace('%sender%', room.user_name(event.sender)).replace('%message%', event.body)
32-
if room.display_name == room_name:
33-
transfer = True
34-
psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender)), (event.body, room.user_name(event.sender)))
36+
# if response.room_id == room_id:
37+
transfer = True
38+
psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender)), (event.body, room.user_name(event.sender)))
3539
else:
3640
psi.dispatch_event(RoomMessageEvent(event.body, room.user_name(event.sender), room.display_name), (event.body, room.user_name(event.sender), room.display_name))
3741
if transfer:
@@ -45,7 +49,8 @@ def on_sync_error(response: SyncError):
4549

4650
async def getMsg() -> None:
4751
global next_batch, msg_callback
48-
from .config import homeserver, device_id, user_id, sync_old_msg
52+
from .config import homeserver, device_id, user_id, sync_old_msg, allow_all_rooms_msg
53+
from .config import room_id as cfg_room_id
4954
client = AsyncClient(f"{homeserver}")
5055
user, token = await getToken()
5156
client.access_token = token
@@ -63,6 +68,9 @@ async def getMsg() -> None:
6368
client.user_id = user_id
6469
client.device_id = device_id
6570

71+
if not allow_all_rooms_msg:
72+
print("ok.")
73+
await client.upload_filter(room={"rooms": [cfg_room_id]})
6674
client.add_response_callback(on_sync_error, SyncError)
6775

6876
try:

matrix_sync/utils/globals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
tLock = threading.Lock()
44
report_matrix = False
55
cleaned = False
6-
sync_task = None
6+
sync_task = None
7+
clientStatus = None

mcdreforged.plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "matrix_sync",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"name": "MatrixSync",
55
"description": {
66
"en_us": "Sync messages between online game and Matrix groups.",

0 commit comments

Comments
 (0)