From c2af3a6c3c88d146437419dcc609a230590ded9c Mon Sep 17 00:00:00 2001 From: Tristan Gosselin-Hane Date: Sat, 13 Oct 2018 04:04:41 -0400 Subject: [PATCH] Add mcstatus library and only re/connect to server if it is online. --- Pipfile | 1 + Pipfile.lock | 29 ++++++++++++++++++++++++++++- webhook-bridge.py | 24 +++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index c3a81bb..dc52b7b 100644 --- a/Pipfile +++ b/Pipfile @@ -13,6 +13,7 @@ cryptography = "*" future = "*" "discord.py" = {git = "https://github.com/Rapptz/discord.py.git"} sqlalchemy = "*" +mcstatus = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index a37ca8d..0a92f12 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "d86ac26059caa9dc880cea720fdbc3726de055c74589031e68afa03b55a0500c" + "sha256": "b74ed57bc94dedc5e0cb39283d2d16b952c17d6db29c6a0c51956b5607f54017" }, "pipfile-spec": 6, "requires": { @@ -74,6 +74,13 @@ ], "version": "==3.0.4" }, + "click": { + "hashes": [ + "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", + "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" + ], + "version": "==7.0" + }, "cryptography": { "hashes": [ "sha256:02602e1672b62e803e08617ec286041cc453e8d43f093a5f4162095506bc0beb", @@ -102,6 +109,19 @@ "discord.py": { "git": "https://github.com/Rapptz/discord.py.git" }, + "dnspython": { + "hashes": [ + "sha256:40f563e1f7a7b80dc5a4e76ad75c23da53d62f1e15e6e517293b04e1f84ead7c", + "sha256:861e6e58faa730f9845aaaa9c6c832851fbf89382ac52915a51f89c71accdd31" + ], + "version": "==1.15.0" + }, + "dnspython3": { + "hashes": [ + "sha256:6eb9504abafb91cb67ed9dc3d3289a3ccc438533b460eccbf77e36c5323100f4" + ], + "version": "==1.15.0" + }, "future": { "hashes": [ "sha256:e39ced1ab767b5936646cedba8bcce582398233d6a627067d4c6a454c90cfedb" @@ -116,6 +136,13 @@ ], "version": "==2.7" }, + "mcstatus": { + "hashes": [ + "sha256:dc0e9fe79d8e3d4c7f3e218921b82b50ab31c991e88cb86831377bd5d2789c12" + ], + "index": "pypi", + "version": "==2.2" + }, "minecraft": { "git": "https://github.com/ammaraskar/pyCraft.git" }, diff --git a/webhook-bridge.py b/webhook-bridge.py index 14c451e..e9edc4d 100755 --- a/webhook-bridge.py +++ b/webhook-bridge.py @@ -23,6 +23,8 @@ from minecraft.compat import input import discord import asyncio +from mcstatus import MinecraftServer + UUID_CACHE = {} def setup_logging(level): @@ -47,12 +49,29 @@ def main(): def handle_disconnect(join_game_packet): logging.info('Disconnected.') connection.disconnect(immediate=True) - time.sleep(5) + while not is_server_online(): + logging.info('Not reconnecting to server because it appears to be offline.') + time.sleep(5) logging.info('Reconnecting.') connection.connect() + def is_server_online(): + server = MinecraftServer.lookup("{}:{}".format(config.mc_server, config.mc_port)) + try: + status = server.status() + del status + return True + except: + # The server is offline + return False + + logging.debug("Checking if the server {} is online before connecting.") + if not config.mc_online: logging.info("Connecting in offline mode...") + if not is_server_online(): + logging.info('Not connecting to server because it appears to be offline.') + sys.exit(1) connection = Connection( config.mc_server, config.mc_port, username=config.mc_username, handle_exception=handle_disconnect) @@ -64,6 +83,9 @@ def main(): logging.info(e) sys.exit() logging.info("Logged in as %s..." % auth_token.username) + if not is_server_online(): + logging.info('Not connecting to server because it appears to be offline.') + sys.exit(1) connection = Connection( config.mc_server, config.mc_port, auth_token=auth_token, handle_exception=handle_disconnect)