Get rid of option parser and load everything from the config file

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent daffd4c5ab
commit 6e9be786d2
  1. 10
      config.example.json
  2. 6
      config.py
  3. 64
      webhook-bridge.py

@ -1,8 +1,14 @@
{ {
"MAIN": { "MAIN": {
"WEBHOOK_URL": "" "WEBHOOK_URL": "",
"MC_USERNAME": "",
"MC_PASSWORD": "",
"MC_SERVER": "",
"MC_PORT": 25565,
"MC_ONLINE": true,
"DISCORD_APP_TOKEN": ""
}, },
"DATABASE": { "DATABASE": {
"CONNECTION_STRING": "" "CONNECTION_STRING": "sqlite:////data/db.sqlite"
} }
} }

@ -7,6 +7,12 @@ class Configuration(object):
self._config = json.load(f) self._config = json.load(f)
if self._config: if self._config:
self.webhook_url = self._config["MAIN"]["WEBHOOK_URL"] 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"] self.database_connection_string = self._config["DATABASE"]["CONNECTION_STRING"]
else: else:
print("error reading config") print("error reading config")

@ -24,53 +24,7 @@ import asyncio
UUID_CACHE = {} 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<host>[^\[\]:]+)|\[(?P<addr>[^\[\]]+)\])"
r"(:(?P<port>\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(): def main():
options = get_options()
config = Configuration("config.json") config = Configuration("config.json")
WEBHOOK_URL = config.webhook_url WEBHOOK_URL = config.webhook_url
@ -83,40 +37,40 @@ def main():
connection.disconnect(immediate=True) connection.disconnect(immediate=True)
time.sleep(5) time.sleep(5)
print('Reconnecting.') print('Reconnecting.')
if options.offline: if not config.mc_online:
print("Connecting in offline mode...") print("Connecting in offline mode...")
connection = Connection( connection = Connection(
options.address, options.port, username=options.username, config.mc_server, config.mc_port, username=config.mc_username,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)
else: else:
auth_token = authentication.AuthenticationToken() auth_token = authentication.AuthenticationToken()
try: try:
auth_token.authenticate(options.username, options.password) auth_token.authenticate(config.mc_username, config.mc_password)
except YggdrasilError as e: except YggdrasilError as e:
print(e) print(e)
sys.exit() sys.exit()
print("Logged in as %s..." % auth_token.username) print("Logged in as %s..." % auth_token.username)
connection = Connection( connection = Connection(
options.address, options.port, auth_token=auth_token, config.mc_server, config.mc_port, auth_token=auth_token,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)
register_handlers(connection) register_handlers(connection)
connection.connect() connection.connect()
if options.offline: if not config.mc_online:
print("Connecting in offline mode...") print("Connecting in offline mode...")
connection = Connection( connection = Connection(
options.address, options.port, username=options.username, config.mc_server, config.mc_port, username=config.mc_username,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)
else: else:
auth_token = authentication.AuthenticationToken() auth_token = authentication.AuthenticationToken()
try: try:
auth_token.authenticate(options.username, options.password) auth_token.authenticate(config.mc_username, config.mc_password)
except YggdrasilError as e: except YggdrasilError as e:
print(e) print(e)
sys.exit() sys.exit()
print("Logged in as %s..." % auth_token.username) print("Logged in as %s..." % auth_token.username)
connection = Connection( connection = Connection(
options.address, options.port, auth_token=auth_token, config.mc_server, config.mc_port, auth_token=auth_token,
handle_exception=handle_disconnect) handle_exception=handle_disconnect)
#Initialize the discord part #Initialize the discord part
@ -243,7 +197,7 @@ def main():
packet.message = "{}: {}".format(message.author.name, message.content) packet.message = "{}: {}".format(message.author.name, message.content)
connection.write_packet(packet) connection.write_packet(packet)
discord_bot.run(options.discord_token) discord_bot.run(config.discord_token)
while True: while True:
try: try:

Loading…
Cancel
Save