make connection and login more robust

This commit is contained in:
Lukas 2021-09-27 13:46:06 +02:00
parent 82053e8131
commit ed9f9acb11
1 changed files with 25 additions and 12 deletions

View File

@ -48,18 +48,27 @@ class TSbot:
self.pipeOut(f"Trying to connect to: {self.host}:{self.port}") self.pipeOut(f"Trying to connect to: {self.host}:{self.port}")
try:
with ts3.query.TS3Connection(self.host, self.port) as self.bot: with ts3.query.TS3Connection(self.host, self.port) as self.bot:
try:
self.bot.login(client_login_name=self.user, client_login_password=self.pwd) self.bot.login(client_login_name=self.user, client_login_password=self.pwd)
self.bot.use(sid=self.sid) self.bot.use(sid=self.sid)
except ts3.query.TS3QueryError:
self.pipeOut("Invalid login credentials, please check your '.toml' file.", lvl="CRITICAL")
try: try:
self.bot.clientupdate(client_nickname=self.nickname) self.bot.clientupdate(client_nickname=self.nickname)
except ts3.query.TS3QueryError: except ts3.query.TS3QueryError:
self.pipeOut("Nickname already in use", lvl="WARNING")
pass pass
self.pipeOut(f"Successfully connected as: {self.nickname}") self.pipeOut(f"Successfully connected as: {self.nickname}")
# Start the Bot # Start the Bot
self.loop() self.loop()
except ts3.query.TS3QueryError:
self.pipeOut("Connection can't be established, check your '.toml' file.", lvl="CRITICAL")
def loop(self): def loop(self):
""" """
Subscribe to event types, ping bot admins and start the event loop. Subscribe to event types, ping bot admins and start the event loop.
@ -181,11 +190,15 @@ class TSbot:
or run in silent log mode. [set reprint to False] or run in silent log mode. [set reprint to False]
""" """
lvln = int(getattr(util.logging, lvl.upper())) lvl = lvl.upper()
lvln = int(getattr(util.logging, lvl))
self.log.log(lvln, msg) self.log.log(lvln, msg)
if reprint: if reprint:
print(f"[{time.strftime('%H:%M:%S')}][{lvl.upper()}] {msg}") print(f"[{time.strftime('%H:%M:%S')}][{lvl}] {msg}")
if lvl == "CRITICAL" or lvl == "ERROR":
exit(1)
def stop(self, invkr_id): def stop(self, invkr_id):
""" """