Add mcstatus library and only re/connect to server if it is online.

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent 6a5c7746b4
commit c2af3a6c3c
  1. 1
      Pipfile
  2. 29
      Pipfile.lock
  3. 24
      webhook-bridge.py

@ -13,6 +13,7 @@ cryptography = "*"
future = "*" future = "*"
"discord.py" = {git = "https://github.com/Rapptz/discord.py.git"} "discord.py" = {git = "https://github.com/Rapptz/discord.py.git"}
sqlalchemy = "*" sqlalchemy = "*"
mcstatus = "*"
[requires] [requires]
python_version = "3.6" python_version = "3.6"

29
Pipfile.lock generated

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "d86ac26059caa9dc880cea720fdbc3726de055c74589031e68afa03b55a0500c" "sha256": "b74ed57bc94dedc5e0cb39283d2d16b952c17d6db29c6a0c51956b5607f54017"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -74,6 +74,13 @@
], ],
"version": "==3.0.4" "version": "==3.0.4"
}, },
"click": {
"hashes": [
"sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13",
"sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
],
"version": "==7.0"
},
"cryptography": { "cryptography": {
"hashes": [ "hashes": [
"sha256:02602e1672b62e803e08617ec286041cc453e8d43f093a5f4162095506bc0beb", "sha256:02602e1672b62e803e08617ec286041cc453e8d43f093a5f4162095506bc0beb",
@ -102,6 +109,19 @@
"discord.py": { "discord.py": {
"git": "https://github.com/Rapptz/discord.py.git" "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": { "future": {
"hashes": [ "hashes": [
"sha256:e39ced1ab767b5936646cedba8bcce582398233d6a627067d4c6a454c90cfedb" "sha256:e39ced1ab767b5936646cedba8bcce582398233d6a627067d4c6a454c90cfedb"
@ -116,6 +136,13 @@
], ],
"version": "==2.7" "version": "==2.7"
}, },
"mcstatus": {
"hashes": [
"sha256:dc0e9fe79d8e3d4c7f3e218921b82b50ab31c991e88cb86831377bd5d2789c12"
],
"index": "pypi",
"version": "==2.2"
},
"minecraft": { "minecraft": {
"git": "https://github.com/ammaraskar/pyCraft.git" "git": "https://github.com/ammaraskar/pyCraft.git"
}, },

@ -23,6 +23,8 @@ from minecraft.compat import input
import discord import discord
import asyncio import asyncio
from mcstatus import MinecraftServer
UUID_CACHE = {} UUID_CACHE = {}
def setup_logging(level): def setup_logging(level):
@ -47,12 +49,29 @@ def main():
def handle_disconnect(join_game_packet): def handle_disconnect(join_game_packet):
logging.info('Disconnected.') logging.info('Disconnected.')
connection.disconnect(immediate=True) 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.') logging.info('Reconnecting.')
connection.connect() 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: if not config.mc_online:
logging.info("Connecting in offline mode...") 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( connection = Connection(
config.mc_server, config.mc_port, username=config.mc_username, config.mc_server, config.mc_port, username=config.mc_username,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)
@ -64,6 +83,9 @@ def main():
logging.info(e) logging.info(e)
sys.exit() sys.exit()
logging.info("Logged in as %s..." % auth_token.username) 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( connection = Connection(
config.mc_server, config.mc_port, auth_token=auth_token, config.mc_server, config.mc_port, auth_token=auth_token,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)

Loading…
Cancel
Save