diff --git a/src/treepuncher/__main__.py b/src/treepuncher/__main__.py index 6114c45..1b9c6af 100644 --- a/src/treepuncher/__main__.py +++ b/src/treepuncher/__main__.py @@ -13,7 +13,7 @@ from dataclasses import dataclass, MISSING, fields from setproctitle import setproctitle -from .treepuncher import Treepuncher, Addon, ConfigObject +from .treepuncher import Treepuncher, Addon, ConfigObject, MissingParameterError from .helpers import configure_logging def main(): @@ -79,12 +79,15 @@ def main(): if args.server: kwargs["server"] = args.server - client = Treepuncher( - args.name, - args.server, - legacy=args.mojang, - use_packet_whitelist=args.use_packet_whitelist, - ) + try: + client = Treepuncher( + args.name, + args.server, + legacy=args.mojang, + use_packet_whitelist=args.use_packet_whitelist, + ) + except MissingParameterError as e: + return logging.error(e.args[0]) enabled_addons = set( a.lower() for a in ( diff --git a/src/treepuncher/treepuncher.py b/src/treepuncher/treepuncher.py index ebc3dab..62d2c6e 100644 --- a/src/treepuncher/treepuncher.py +++ b/src/treepuncher/treepuncher.py @@ -88,6 +88,8 @@ class Addon: async def cleanup(self): pass +class MissingParameterError(Exception): + pass class Treepuncher( GameState, @@ -125,23 +127,26 @@ class Treepuncher( authenticator : AuthInterface - def opt(k:str) -> Any: - return kwargs.get(k) or self.config['Treepuncher'].get(k) + def opt(k:str, required=False, default=None) -> Any: + v = kwargs.get(k) or self.config['Treepuncher'].get(k) or default + if not v and required: + raise MissingParameterError(f"Missing configuration parameter '{k}'") + return v if not online_mode: authenticator = OfflineAuthenticator(self.name) elif legacy: authenticator = MojangAuthenticator( - username= opt('username') or name, + username= opt('username', default=name, required=True), password= opt('password') ) if opt('legacy_token'): authenticator.deserialize(json.loads(opt('legacy_token'))) else: authenticator = MicrosoftAuthenticator( - client_id= opt('client_id'), - client_secret= opt('client_secret'), - redirect_uri= opt('redirect_uri'), + client_id= opt('client_id', required=True), + client_secret= opt('client_secret', required=True), + redirect_uri= opt('redirect_uri', required=True), code= opt('code'), ) @@ -161,7 +166,7 @@ class Treepuncher( logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy self.scheduler.start(paused=True) - super().__init__(opt('server'), online_mode=online_mode, authenticator=authenticator) + super().__init__(opt('server', required=True), online_mode=online_mode, authenticator=authenticator) @property