diff --git a/config.example.json b/config.example.json index 4dd9296..a072b2c 100644 --- a/config.example.json +++ b/config.example.json @@ -1,8 +1,14 @@ { "MAIN": { - "WEBHOOK_URL": "" + "WEBHOOK_URL": "", + "MC_USERNAME": "", + "MC_PASSWORD": "", + "MC_SERVER": "", + "MC_PORT": 25565, + "MC_ONLINE": true, + "DISCORD_APP_TOKEN": "" }, "DATABASE": { - "CONNECTION_STRING": "" + "CONNECTION_STRING": "sqlite:////data/db.sqlite" } } diff --git a/config.py b/config.py index b2a7057..2e1ea3f 100644 --- a/config.py +++ b/config.py @@ -7,6 +7,12 @@ class Configuration(object): self._config = json.load(f) if self._config: self.webhook_url = self._config["MAIN"]["WEBHOOK_URL"] + self.mc_username = self._config["MAIN"]["MC_USERNAME"] + self.mc_password = self._config["MAIN"]["MC_PASSWORD"] + self.mc_server = self._config["MAIN"]["MC_SERVER"] + self.mc_port = self._config["MAIN"]["MC_PORT"] + self.mc_online = self._config["MAIN"]["MC_ONLINE"] + self.discord_token = self._config["MAIN"]["DISCORD_APP_TOKEN"] self.database_connection_string = self._config["DATABASE"]["CONNECTION_STRING"] else: print("error reading config") diff --git a/webhook-bridge.py b/webhook-bridge.py index 8d4ddfc..613b8a8 100755 --- a/webhook-bridge.py +++ b/webhook-bridge.py @@ -24,53 +24,7 @@ import asyncio UUID_CACHE = {} -def get_options(): - parser = OptionParser() - - parser.add_option("-u", "--username", dest="username", default=None, - help="username to log in with") - - parser.add_option("-p", "--password", dest="password", default=None, - help="password to log in with") - - parser.add_option("-s", "--server", dest="server", default=None, - help="server host or host:port " - "(enclose IPv6 addresses in square brackets)") - - parser.add_option("-o", "--offline", dest="offline", action="store_true", - help="connect to a server in offline mode " - "(no password required)") - - parser.add_option("-t", "--token", dest="discord_token", default=None, - help="discord token to log the bot in with") - - (options, args) = parser.parse_args() - - if not options.username: - options.username = input("Enter your username: ") - - if not options.password and not options.offline: - options.password = getpass.getpass("Enter your password (leave " - "blank for offline mode): ") - options.offline = options.offline or (options.password == "") - - if not options.server: - options.server = input("Enter server host or host:port " - "(enclose IPv6 addresses in square brackets): ") - # Try to split out port and address - match = re.match(r"((?P[^\[\]:]+)|\[(?P[^\[\]]+)\])" - r"(:(?P\d+))?$", options.server) - if match is None: - raise ValueError("Invalid server address: '%s'." % options.server) - options.address = match.group("host") or match.group("addr") - options.port = int(match.group("port") or 25565) - - return options - - def main(): - options = get_options() - config = Configuration("config.json") WEBHOOK_URL = config.webhook_url @@ -83,40 +37,40 @@ def main(): connection.disconnect(immediate=True) time.sleep(5) print('Reconnecting.') - if options.offline: + if not config.mc_online: print("Connecting in offline mode...") connection = Connection( - options.address, options.port, username=options.username, + config.mc_server, config.mc_port, username=config.mc_username, handle_exception=handle_disconnect) else: auth_token = authentication.AuthenticationToken() try: - auth_token.authenticate(options.username, options.password) + auth_token.authenticate(config.mc_username, config.mc_password) except YggdrasilError as e: print(e) sys.exit() print("Logged in as %s..." % auth_token.username) connection = Connection( - options.address, options.port, auth_token=auth_token, + config.mc_server, config.mc_port, auth_token=auth_token, handle_exception=handle_disconnect) register_handlers(connection) connection.connect() - if options.offline: + if not config.mc_online: print("Connecting in offline mode...") connection = Connection( - options.address, options.port, username=options.username, + config.mc_server, config.mc_port, username=config.mc_username, handle_exception=handle_disconnect) else: auth_token = authentication.AuthenticationToken() try: - auth_token.authenticate(options.username, options.password) + auth_token.authenticate(config.mc_username, config.mc_password) except YggdrasilError as e: print(e) sys.exit() print("Logged in as %s..." % auth_token.username) connection = Connection( - options.address, options.port, auth_token=auth_token, + config.mc_server, config.mc_port, auth_token=auth_token, handle_exception=handle_disconnect) #Initialize the discord part @@ -124,16 +78,16 @@ def main(): def register_handlers(connection): connection.register_packet_listener( - handle_join_game, clientbound.play.JoinGamePacket) + handle_join_game, clientbound.play.JoinGamePacket) connection.register_packet_listener( - print_chat, clientbound.play.ChatMessagePacket) + print_chat, clientbound.play.ChatMessagePacket) connection.register_packet_listener( - handle_health_update, clientbound.play.UpdateHealthPacket) + handle_health_update, clientbound.play.UpdateHealthPacket) connection.register_packet_listener( - handle_disconnect, clientbound.play.DisconnectPacket) + handle_disconnect, clientbound.play.DisconnectPacket) def handle_join_game(join_game_packet): @@ -243,7 +197,7 @@ def main(): packet.message = "{}: {}".format(message.author.name, message.content) connection.write_packet(packet) - discord_bot.run(options.discord_token) + discord_bot.run(config.discord_token) while True: try: