|
1 | 1 | import asyncio |
2 | 2 | import threading |
3 | | -import matrix_sync.logger.get_logger as get_logger |
4 | 3 | import matrix_sync.plg_globals as plg_globals |
5 | 4 |
|
6 | 5 | from mcdreforged.api.all import * |
| 6 | +from ..utils.get_logger import console_logger, reply_logger |
7 | 7 | from ..client.reporter import send_to_matrix |
8 | 8 | from ..client.receiver import get_messages, stop_sync |
9 | | -from ..client import * |
10 | | -from ..utils import * |
| 9 | +from ..utils import psi, plgSelf, tr |
11 | 10 | from .help import * |
12 | 11 |
|
13 | 12 |
|
|
16 | 15 | plg_globals.tLock = threading.Lock() |
17 | 16 |
|
18 | 17 | def start_sync(): |
19 | | - logger = get_logger() |
| 18 | + logger = console_logger() |
20 | 19 | if not plg_globals.tLock.locked(): |
21 | 20 | run_sync_task() |
22 | 21 | else: |
23 | 22 | logger.warning(tr("on_sync_running")) |
24 | 23 |
|
25 | 24 | @new_thread('MatrixReceiver') |
26 | 25 | def run_sync_task(): |
27 | | - logger = get_logger() |
| 26 | + logger = console_logger() |
28 | 27 | plg_globals.sync = True |
29 | 28 | if plg_globals.token_vaild: |
30 | 29 | with plg_globals.tLock: |
@@ -52,42 +51,72 @@ def command_register(server: PluginServerInterface): |
52 | 51 | builder.register(server) |
53 | 52 |
|
54 | 53 | @builder.command("!!msync start") |
55 | | -def on_command_start(): |
56 | | - start_sync() |
| 54 | +def on_command_start(src: CommandSource): |
| 55 | + if src.has_permission_higher_than(2): |
| 56 | + start_sync() |
| 57 | + else: |
| 58 | + src.reply(tr("no_permission")) |
57 | 59 |
|
58 | 60 | @builder.command("!!msync stop") |
59 | | -async def on_command_stop(): |
60 | | - await stop_sync() |
| 61 | +async def on_command_stop(src: CommandSource): |
| 62 | + if src.has_permission_higher_than(2): |
| 63 | + await stop_sync() |
| 64 | + else: |
| 65 | + src.reply(tr("no_permission")) |
61 | 66 |
|
62 | 67 | @builder.command("!!msync status") |
63 | | -def show_status(): |
64 | | - logger = get_logger() |
65 | | - logger.info(f"Receiver: {plg_globals.sync}") |
66 | | - if plg_globals.sync: |
67 | | - logger.info(tr("sync_status.running")) |
68 | | - else: |
69 | | - logger.info(tr("sync_status.not_running")) |
| 68 | +def show_status(src: CommandSource): |
| 69 | + logger = console_logger() |
| 70 | + def return_result(): |
| 71 | + if plg_globals.sync: |
| 72 | + logger.info(tr("sync_status.running")) |
| 73 | + else: |
| 74 | + logger.info(tr("sync_status.not_running")) |
| 75 | + if src.is_console: |
| 76 | + logger = console_logger() |
| 77 | + logger.info(f"Receiver: {plg_globals.sync}") |
| 78 | + return_result() |
| 79 | + if src.is_player: |
| 80 | + reply = reply_logger() |
| 81 | + reply.log(src, f"Receiver: {plg_globals.sync}", logger) |
| 82 | + if plg_globals.sync: |
| 83 | + reply.log(src, tr("sync_status.running"), logger) |
| 84 | + else: |
| 85 | + reply.log(src, tr("sync_status.not_running"), logger) |
70 | 86 |
|
71 | 87 | @builder.command("!!msync send <message>") |
72 | 88 | def on_command_send(src: CommandSource, ctx: CommandContext): |
73 | | - if plg_globals.token_vaild: |
74 | | - matrix_reporter(ctx["message"]) |
75 | | - src.reply(tr("on_send_command.sending")) |
| 89 | + if src.has_permission_higher_than(2): |
| 90 | + if plg_globals.token_vaild: |
| 91 | + matrix_reporter(ctx["message"]) |
| 92 | + src.reply(tr("on_send_command.sending")) |
| 93 | + else: |
| 94 | + src.reply(tr("on_send_command.failed") + ": " + tr("token_mismatch")) |
76 | 95 | else: |
77 | | - src.reply(tr("on_send_command.failed") + ": " + tr("token_mismatch")) |
| 96 | + src.reply(tr("no_permission")) |
78 | 97 |
|
79 | 98 | @builder.command("!!msync reload") |
80 | | -def on_command_reload(): |
81 | | - psi.reload_plugin(plgSelf.id) |
| 99 | +def on_command_reload(src: CommandSource): |
| 100 | + if src.has_permission_higher_than(2): |
| 101 | + psi.reload_plugin(plgSelf.id) |
| 102 | + else: |
| 103 | + reply = reply_logger() |
| 104 | + logger = console_logger() |
| 105 | + reply.log(src, tr("no_permission"), logger) |
82 | 106 |
|
83 | 107 | @builder.command("!!msync reload <pack_name>") |
84 | 108 | def on_command_reload_subpack(src: CommandSource, ctx: CommandContext): |
85 | | - plugin_list = psi.get_plugin_list() |
86 | | - subpack_id = "msync_" + ctx["pack_name"] |
87 | | - if subpack_id in plugin_list: |
88 | | - psi.reload_plugin(subpack_id) |
| 109 | + if src.has_permission_higher_than(2): |
| 110 | + plugin_list = psi.get_plugin_list() |
| 111 | + subpack_id = ctx["pack_name"] |
| 112 | + if not subpack_id.startswith("msync_"): |
| 113 | + subpack_id = "msync_" + ctx["pack_name"] |
| 114 | + if subpack_id in plugin_list: |
| 115 | + psi.reload_plugin(subpack_id) |
| 116 | + else: |
| 117 | + src.reply("Reload subpack error: Invaild name or target subpack is not loaded!") |
89 | 118 | else: |
90 | | - src.reply("Reload subpack error: Invaild name!") |
| 119 | + src.reply(tr("no_permission")) |
91 | 120 |
|
92 | 121 | @builder.command("!!msync") |
93 | 122 | @builder.command("!!msync help") |
|
0 commit comments