diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md index a3de8bc..aceeb07 100644 --- a/.github/CODE_OF_CONDUCT.md +++ b/.github/CODE_OF_CONDUCT.md @@ -55,7 +55,7 @@ further defined and clarified by project maintainers. ### Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting Ollie on discord at `Ollie#0175`. All +reported by contacting Ollie on discord at `ollieee_`. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. diff --git a/rustplus/api/base_rust_api.py b/rustplus/api/base_rust_api.py index f2159af..2f09f72 100644 --- a/rustplus/api/base_rust_api.py +++ b/rustplus/api/base_rust_api.py @@ -108,11 +108,17 @@ def _generate_protobuf(self) -> AppRequest: return app_request async def connect( - self, retries: int = float("inf"), delay: int = 20, on_failure=None + self, retries: int = float("inf"), delay: int = 20, on_failure=None, on_success=None ) -> None: """ Attempts to open a connection to the rust game server specified in the constructor + :param retries: The number of times to attempt reconnecting. Defaults to infinite. + :param delay: The delay (in seconds) between reconnection attempts. + :param on_failure: Optional function to be called when connecting fails. + :param on_success: Optional function to be called when connecting succeeds. + + :return: None """ EventLoopManager.set_loop( @@ -138,6 +144,7 @@ async def connect( retries=retries, delay=delay, on_failure=on_failure, + on_success=on_success ) await self.heartbeat.start_beat() except ConnectionRefusedError: diff --git a/rustplus/api/remote/rust_remote_interface.py b/rustplus/api/remote/rust_remote_interface.py index 56ba52a..d58e7a4 100644 --- a/rustplus/api/remote/rust_remote_interface.py +++ b/rustplus/api/remote/rust_remote_interface.py @@ -66,7 +66,7 @@ def __init__( self.pending_entity_subscriptions = [] self.camera_manager: Union[CameraManager, None] = None - async def connect(self, retries, delay, on_failure=None) -> None: + async def connect(self, retries, delay, on_failure=None, on_success=None) -> None: self.ws = RustWebsocket( server_id=self.server_id, remote=self, @@ -74,6 +74,7 @@ async def connect(self, retries, delay, on_failure=None) -> None: magic_value=self.magic_value, use_test_server=self.use_test_server, on_failure=on_failure, + on_success=on_success, delay=delay, ) await self.ws.connect(retries=retries) diff --git a/rustplus/api/remote/rustws.py b/rustplus/api/remote/rustws.py index 38ef64f..2cbaa50 100644 --- a/rustplus/api/remote/rustws.py +++ b/rustplus/api/remote/rustws.py @@ -32,6 +32,7 @@ def __init__( magic_value, use_test_server, on_failure, + on_success, delay, ): self.connection: Union[WebSocketClientProtocol, None] = None @@ -46,6 +47,7 @@ def __init__( self.use_test_server = use_test_server self.outgoing_conversation_messages = [] self.on_failure = on_failure + self.on_success = on_success self.delay = delay async def connect( @@ -79,14 +81,24 @@ async def connect( address += f"?v={str(self.magic_value)}" self.connection = await connect(address, close_timeout=0, ping_interval=None) self.connected_time = time.time() + + if self.on_success is not None: + try: + if asyncio.iscoroutinefunction(self.on_success): + await self.on_success() + else: + self.on_success() + except Exception as e: + self.logger.warning(e) break + except Exception as exception: print_error = True if not isinstance(exception, KeyboardInterrupt): # Run the failure callback - try: - if self.on_failure is not None: + if self.on_failure is not None: + try: if asyncio.iscoroutinefunction(self.on_failure): val = await self.on_failure() else: @@ -95,8 +107,8 @@ async def connect( if val is not None: print_error = val - except Exception as e: - self.logger.warning(e) + except Exception as e: + self.logger.warning(e) if print_error: self.logger.warning(