From 01d5b37dbc4dc5cad0747a095e3376512c1b8ad1 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Sun, 29 Oct 2023 14:47:50 +0800 Subject: [PATCH] !!online command on mcdr client --- chatbridge/impl/mcdr/client.py | 18 +++++++++++++++--- chatbridge/impl/mcdr/config.py | 3 +++ chatbridge/impl/mcdr/mcdr_entry.py | 12 ++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/chatbridge/impl/mcdr/client.py b/chatbridge/impl/mcdr/client.py index a8819f5..ba96ecd 100644 --- a/chatbridge/impl/mcdr/client.py +++ b/chatbridge/impl/mcdr/client.py @@ -5,7 +5,7 @@ from chatbridge.core.client import ChatBridgeClient from chatbridge.core.network.protocol import ChatPayload, CommandPayload from chatbridge.impl.mcdr.config import MCDRClientConfig -from chatbridge.impl.tis.protocol import StatsQueryResult +from chatbridge.impl.tis.protocol import StatsQueryResult, OnlineQueryResult class ChatBridgeMCDRClient(ChatBridgeClient): @@ -38,9 +38,10 @@ def on_chat(self, sender: str, payload: ChatPayload): self.server.say(RText('[{}] {}'.format(sender, payload.formatted_str()), RColor.gray)) def on_command(self, sender: str, payload: CommandPayload): + is_ask = not payload.responded command = payload.command result: Optional[Serializable] = None - if command.startswith('!!stats '): + if command.startswith('!!stats '): # !!stats request try: import stats_helper except (ImportError, ModuleNotFoundError): @@ -69,6 +70,17 @@ def on_command(self, sender: str, payload: CommandPayload): result = StatsQueryResult.create(stats_name, lines[1:-1], total) else: result = StatsQueryResult.unknown_stat() + elif command == '!!online': # !!online response + player = payload.params.get('player') + if player is None: + self.logger.warning('No player in params, params {}'.format(payload.params)) + else: + result: OnlineQueryResult = OnlineQueryResult.deserialize(payload.result) + for line in result.data: + self.server.tell(player, line) - if result is not None: + if is_ask and result is not None: self.reply_command(sender, payload, result) + + def query_online(self, client_to_query_online: str, player: str): + self.send_command(client_to_query_online, '!!online', params={'player': player}) diff --git a/chatbridge/impl/mcdr/config.py b/chatbridge/impl/mcdr/config.py index 1c7cbf0..271346f 100644 --- a/chatbridge/impl/mcdr/config.py +++ b/chatbridge/impl/mcdr/config.py @@ -1,6 +1,9 @@ +from typing import Optional + from chatbridge.core.config import ClientConfig class MCDRClientConfig(ClientConfig): enable: bool = True debug: bool = False + client_to_query_online: Optional[str] = None diff --git a/chatbridge/impl/mcdr/mcdr_entry.py b/chatbridge/impl/mcdr/mcdr_entry.py index b22eeda..1f5f695 100644 --- a/chatbridge/impl/mcdr/mcdr_entry.py +++ b/chatbridge/impl/mcdr/mcdr_entry.py @@ -33,6 +33,17 @@ def display_status(source: CommandSource): source.reply(tr('status.info', client.is_online(), client.get_ping_text())) +def query_online(source: CommandSource): + if config.client_to_query_online is None: + source.reply('client_to_query_online unset') + return + + if client is not None: + client.query_online(config.client_to_query_online, source.player) + else: + source.reply(tr('status.not_init')) + + @new_thread('ChatBridge-restart') def restart_client(source: CommandSource): with cb_lock: @@ -98,6 +109,7 @@ def on_load(server: PluginServerInterface, old_module): then(Literal('status').runs(display_status)). then(Literal('restart').runs(restart_client)) ) + server.register_command(Literal('!!online').runs(query_online)) @new_thread('ChatBridge-start') def start():